Skip to content

Instantly share code, notes, and snippets.

View tfolkman's full-sized avatar

Tyler Folkman tfolkman

View GitHub Profile
@tfolkman
tfolkman / neighbors.py
Created December 5, 2019 06:06
neighbors
from sklearn.datasets import load_breast_cancer
from sklearn.neighbors import KNeighborsClassifier, KNeighborsRegressor
from sklearn.model_selection import train_test_split, GridSearchCV
from sklearn.metrics import f1_score, classification_report, accuracy_score, mean_squared_error
data = load_breast_cancer()
X_train, X_test, y_train, y_test = train_test_split(data.data, data.target, test_size=0.20, random_state=42)
clf = KNeighborsClassifier()
gridsearch = GridSearchCV(clf, {"n_neighbors": [1, 3, 5, 7, 9, 11], "weights": ['uniform', 'distance'],
'p': [1, 2, 3]}, scoring='f1')
@tfolkman
tfolkman / tsne.py
Last active December 1, 2019 06:28
tsne gist
from sklearn.datasets import fetch_mldata
from sklearn.manifold import TSNE
from sklearn.decomposition import PCA
import seaborn as sns
import numpy as np
import matplotlib.pyplot as plt
# get mnist data
mnist = fetch_mldata("MNIST original")
X = mnist.data / 255.0
@tfolkman
tfolkman / sgd.py
Last active November 11, 2019 06:03
sgd
from sklearn.linear_model import SGDRegressor
linear_regression_model = SGDRegressor(tol=.0001, eta0=.01)
coeffs = []
for i, data in enumerate(bootstrap_X):
linear_regression_model.fit(data, bootstrap_y[i])
coeffs.append(linear_regression_model.coef_)
@tfolkman
tfolkman / bootstrap.py
Created November 11, 2019 05:58
bootstrap
from sklearn.utils import resample
n_bootstraps = 1000
bootstrap_X = []
bootstrap_y = []
for _ in range(n_bootstraps):
sample_X, sample_y = resample(scaled_df, target)
bootstrap_X.append(sample_X)
bootstrap_y.append(sample_y)
@tfolkman
tfolkman / swifter.py
Created November 2, 2019 05:36
swifter
import pandas as pd
import swifter
df.swifter.apply(lambda x: x.sum() - x.min())
@tfolkman
tfolkman / defaultdict.py
Last active October 26, 2019 02:31
defaultdict
from collections import defaultdict
my_default_dict = defaultdict(int)
for letter in 'the red fox ran as fast as it could':
my_default_dict[letter] += 1
print(my_default_dict)
@tfolkman
tfolkman / counter.py
Created October 25, 2019 13:48
counter
from collections import Counter
ages = [22, 22, 25, 25, 30, 24, 26, 24, 35, 45, 52, 22, 22, 22, 25, 16, 11, 15, 40, 30]
value_counts = Counter(ages)
print(value_counts.most_common())
@tfolkman
tfolkman / namedtuple.py
Last active October 25, 2019 04:09
NamedTuple
from collections import namedtuple
Features = namedtuple('Features', ['age', 'gender', 'name'])
row = Features(age=22, gender='male', name='Alex')
print(row.age)