Skip to content

Instantly share code, notes, and snippets.

Avatar
🌆
Improving

Will Koehrsen WillKoehrsen

🌆
Improving
View GitHub Profile
View function_parameter.py
def house_price_in_aus_from_model(usd_per_floor,
usd_per_bathroom_squared,
floor_count,
bathroom_count,
usd_to_aus_rate):
"""
Calculate the house price in Australian dollars from
a polynomial regression model
"""
View formatted_loop.py
PIXEL_NORMALIZATION_FACTOR = 12.5
PIXEL_OFFSET_FACTOR = 150
for row_index in range(row_count):
for column_index in range(column_count):
for color_channel_index in range(color_channel_count):
normalized_pixel_value = (
original_pixel_array[row_index][column_index][color_channel_index]
* PIXEL_NORMALIZATION_FACTOR
)
View main.js
define(['base/js/namespace', 'base/js/events'], function (Jupyter, events) {
// Template cells including markdown and imports
var setUp = function () {
Jupyter.notebook.insert_cell_at_index('markdown', 0)
.set_text(`# Introduction
State notebook purpose here`)
Jupyter.notebook.insert_cell_at_index('markdown', 1).set_text(`### Imports
Import libraries and write settings here.`)
// Define imports and settings
Jupyter.notebook.insert_cell_at_index('code', 2)
View start.py
import pandas as pd
import numpy as np
# Pandas options
pd.options.display.max_columns = 30
pd.options.display.max_rows = 20
from IPython import get_ipython
ipython = get_ipython()
View reusable.py
def show_stats_by_tag(tag):
return(df.groupby(f'<tag>{tag}').describe()[['views', 'reads']])
stats = interact(show_stats_by_tag,
tag=widgets.Dropdown(options=['Towards Data Science', 'Education',
'Machine Learning', 'Python', 'Data Science']))
View dependent_widgets.py
# Create widgets
directory = widgets.Dropdown(options=['images', 'nature', 'assorted'])
images = widgets.Dropdown(options=os.listdir(directory.value))
# Updates the image options based on directory value
def update_images(*args):
images.options = os.listdir(directory.value)
# Tie the image options to directory value
directory.observe(update_images, 'value')
View datepicker_stats.py
# Create interactive version of function with DatePickers
interact(stats_for_article_published_between,
start_date=widgets.DatePicker(value=pd.to_datetime('2018-01-01')),
end_date=widgets.DatePicker(value=pd.to_datetime('2019-01-01')))
View scatterplot_interact.py
import cufflinks as cf
@interact
def scatter_plot(x=list(df.select_dtypes('number').columns),
y=list(df.select_dtypes('number').columns)[1:],
theme=list(cf.themes.THEMES.keys()),
colorscale=list(cf.colors._scales_names.keys())):
df.iplot(kind='scatter', x=x, y=y, mode='markers',
xTitle=x.title(), yTitle=y.title(),
View dataframe_widgets.py
# Correlation between two columns with dropdown
@interact
def correlations(column1=list(df.select_dtypes('number').columns),
column2=list(df.select_dtypes('number').columns)):
print(f"Correlation: {df[column1].corr(df[column2])}")
# Stats of a column with dropdown
@interact
def describe(column=list(df.columns)):
print(df[column].describe())
View interact_with_specifications.py
# Interact with specification of arguments
@interact
def show_articles_more_than(column=['claps', 'views', 'fans', 'reads'],
x=(10, 100000, 10)):
return df.loc[df[column] > x]