This file contains 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
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') |
This file contains 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
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 |
This file contains 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
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_) |
This file contains 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
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) |
This file contains 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 pandas as pd | |
import swifter | |
df.swifter.apply(lambda x: x.sum() - x.min()) |
This file contains 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
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) |
This file contains 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
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()) |
This file contains 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
from collections import namedtuple | |
Features = namedtuple('Features', ['age', 'gender', 'name']) | |
row = Features(age=22, gender='male', name='Alex') | |
print(row.age) |