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 io import BytesIO | |
from pprint import pprint as pp | |
import requests | |
from PIL import Image | |
import matplotlib | |
matplotlib.use('Agg') | |
import matplotlib.pyplot as plt | |
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
# training loop parameters | |
lr = 1e-3 | |
wd = 1e-5 | |
bs = 2000 | |
n_epochs = 100 | |
patience = 10 | |
no_improvements = 0 | |
best_loss = np.inf | |
best_weights = None | |
history = [] |
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
class EmbeddingNet(nn.Module): | |
""" | |
Creates a dense network with embedding layers. | |
Args: | |
n_users: | |
Number of unique users in the dataset. | |
n_movies: |
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
X, y = reader.dataset | |
X_train, X_test, y_train, y_test = train_test_split(X, y, stratify=y, train_size=0.8) | |
pipeline = make_pipeline( | |
BatchTransformer(StandardScaler()), | |
KMeansQuantization(k=2), | |
RandomForestClassifier(n_estimators=500)) | |
pipeline.fit(X_train, y_train) | |
y_preds = pipeline.predict(X_test) |
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
""" | |
Main repository: https://github.com/devforfu/Blog/tree/master/trees | |
""" | |
import os | |
from os.path import join | |
from pprint import pprint | |
import pandas as pd | |
from sklearn.pipeline import make_pipeline | |
from sklearn.preprocessing import StandardScaler |
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
""" | |
Wrist-Worn Accelerometer Dataset using scikit-learn. | |
""" | |
import re | |
from os import listdir | |
from os.path import exists, join, basename, isdir | |
import numpy as np |
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 os.path import join | |
import numpy as np | |
from sklearn.model_selection import train_test_split | |
from quantization import quantize | |
from decision_tree import learn_tree | |
from decision_tree import predict_tree | |
from ensemble import RandomForestClassifier | |
from utils import train_test_split, encode_labels |
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
class RandomForestClassifier: | |
def __init__(self, tree_funcs, n_trees: int=10, | |
feature_subset_size: str='sqrt', max_depth: int=5, | |
min_split_size: int=10, min_leaf_size: int=None, | |
log=None): | |
if n_trees < 1: | |
raise ValueError(f'cannot build an ensemble of {n_trees:d} trees') |
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
""" | |
Simple decision tree implementation using Numpy. | |
""" | |
from collections import Counter | |
import numpy as np | |
class _Node: | |
""" |
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
def quantize(dataset_path, k): | |
dataset = [] | |
for points in read_files(dataset_path): | |
x = np.asarray(points, dtype=np.float) | |
centroids, _ = kmeans(x, n_clusters=k) | |
feature_vector = centroids.flatten() | |
dataset.append(feature_vector) | |
return np.array(dataset) |