This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# import time - not the abstract construct of 'time' | |
# but rather a library built into Python for | |
# dealing with time | |
from time import time | |
# ML stuff | |
ada_tuned_clf = AdaBoostClassifier(random_state=1) | |
# some canned params for hypertuning | |
parameters = { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# runtime info based on solution below and fit_time results of the gridsearchcv return object | |
# based on a response on StackExchange Data Science - Naveen Vuppula | |
# https://datascience.stackexchange.com/a/93524/41883 | |
# from time import time | |
def gridsearch_runtime(grid_obj, X_train, y_train): | |
''' | |
Parameters: | |
grid_obj: GridSearchCV return object that has not yet been fit to training data | |
X_train: split training data independent variables | |
y_train: split training data containing dependent variable |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# import libs | |
import pandas as pd | |
import numpy as np | |
import seaborn as sns | |
import matplotlib.pyplot as plt | |
# this function will create a boxplot + histogram plot using Seaborn's Jointgrid | |
# we'll also provide Type annotations to provide hints to future users | |
def dual_plot(series: pd.Series, figsize: tuple = (16,8), | |
bins: int = None, return_plot: bool = False, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# function to iterate over specified variables and view their value counts | |
# add typing to help understand our function if reused elsewhere | |
from typing import List | |
def value_count_rep(columns: List, df: pd.DataFrame) -> None: | |
''' | |
Parameters: List of columns to iterate over | |
Returns: No return value. Prints the value counts of each column(feature) to stdout | |
''' | |
for column in columns: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# https://www.python.org/dev/peps/pep-0634/ | |
# PEP 634 proposed (and was accepted) Structural Pattern Matching (switch/case) | |
# for Python (available in 3.10) - as of this Gist, | |
# prerelease 3.10.0b2 is available | |
import inspect | |
F = [ | |
lambda x,y: x+y, | |
lambda x: x+1, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## handy multi-plot function for showing mode, median, and mean lines in a distplot | |
## (but using histplot since distplot is deprecated) | |
## Author - Abram Flansburg | |
## Intended for use in Jupyter / iPython | |
def skewness_plot(series): | |
""" | |
Plots a series using the histogram plot with kde and plots, mean, mode, and median lines. | |
*** Dependencies *** | |
Series must be a pandas.Series | |
Seaborn must be imported as sns |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// In this scenario - a Cloudwatch Subscription Filter is hooked up to a Node.js lambda's ARN | |
// (see AWS docs around Log Group subscription filters) | |
// Will need a webhook for the appropriate Slack channel | |
const https = require('https'); | |
const zlib = require('zlib'); | |
const options = { | |
hostname: 'hooks.slack.com', | |
path: 'SLACK_WEBHOOK_URL', |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Not my original function - looking for citation | |
def missing_check(df): | |
null_val_sum = df.isnull().sum() | |
total = df.isnull().sum().sort_values(ascending=False) # total null values | |
percent = (null_val_sum/df.isnull().count()).sort_values(ascending=False) | |
missing_data = pd.concat([total, percent], axis=1, keys=['Total', 'Percent']) | |
return missing_data | |
''' | |
Example: | |
missing_data(df_with_nulls) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# imagine this lives at app/lib/complicated_data | |
module ComplicatedData | |
def generate_complicated_plot_data(start_date: 30.days.ago, end_date: Date.today, type: nil) | |
# implementation | |
end | |
# module_function ensures the method cannot be overridden or extended | |
module_function :generate_complicated_plot_data | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# radius of earth in meters | |
R = 6371000; | |
def haversine(coord1:, coord2:) | |
# first add our own radians function since Ruby Math does not have one | |
radians = -> (degrees) { degrees * (Math::PI / 180)} | |
# convert latitude degrees to radians | |
phi_1 = radians.call(coord1[:latitude]); | |
phi_2 = radians.call(coord2[:latitude]); |