Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1,456 changes: 1,456 additions & 0 deletions KNN.ipynb

Large diffs are not rendered by default.

141 changes: 141 additions & 0 deletions knn.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
import numpy as np


class KNNClassifier:
"""
K-neariest-neighbor classifier using L1 loss
"""

def __init__(self, k=1):
self.k = k


def fit(self, X, y):
self.train_X = X
self.train_y = y


def predict(self, X, n_loops=0):
"""
Uses the KNN model to predict clases for the data samples provided

Arguments:
X, np array (num_samples, num_features) - samples to run
through the model
num_loops, int - which implementation to use

Returns:
predictions, np array of ints (num_samples) - predicted class
for each sample
"""

if n_loops == 0:
distances = self.compute_distances_no_loops(X)
elif n_loops == 1:
distances = self.compute_distances_one_loops(X)
else:
distances = self.compute_distances_two_loops(X)

if len(np.unique(self.train_y)) == 2:
return self.predict_labels_binary(distances)
else:
return self.predict_labels_multiclass(distances)


def compute_distances_two_loops(self, X):
"""
Computes L1 distance from every sample of X to every training sample
Uses simplest implementation with 2 Python loops

Arguments:
X, np array (num_test_samples, num_features) - samples to run

Returns:
distances, np array (num_test_samples, num_train_samples) - array
with distances between each test and each train sample
"""
distances = np.zeros((X.shape[0], self.train_X.shape[0]))
for i in range(distances.shape[0]):
for j in range(distances.shape[1]):
distances[i, j] = np.sum(np.abs(self.train_X[j] - X[i]))
return distances


def compute_distances_one_loop(self, X):
"""
Computes L1 distance from every sample of X to every training sample
Vectorizes some of the calculations, so only 1 loop is used

Arguments:
X, np array (num_test_samples, num_features) - samples to run

Returns:
distances, np array (num_test_samples, num_train_samples) - array
with distances between each test and each train sample
"""
distances = np.zeros((X.shape[0], self.train_X.shape[0]))
for i in range(distances.shape[0]):
distances[i] = np.sum(np.abs(self.train_X - X[i]), axis = 1)
return distances


def compute_distances_no_loops(self, X):
"""
Computes L1 distance from every sample of X to every training sample
Fully vectorizes the calculations using numpy

Arguments:
X, np array (num_test_samples, num_features) - samples to run

Returns:
distances, np array (num_test_samples, num_train_samples) - array
with distances between each test and each train sample
"""

distances = np.abs(X[:, np.newaxis, :] - self.train_X).sum(axis=2)
return distances


def predict_labels_binary(self, distances):
"""
Returns model predictions for binary classification case

Arguments:
distances, np array (num_test_samples, num_train_samples) - array
with distances between each test and each train sample
Returns:
pred, np array of bool (num_test_samples) - binary predictions
for every test sample
"""

n_train = distances.shape[1]
n_test = distances.shape[0]
prediction = np.zeros(n_test)
#for i in range(n_test):
# neib_idx = np.argsort(distances[i])[0:self.k]
"""
YOUR CODE IS HERE
"""
pass


def predict_labels_multiclass(self, distances):
"""
Returns model predictions for multi-class classification case

Arguments:
distances, np array (num_test_samples, num_train_samples) - array
with distances between each test and each train sample
Returns:
pred, np array of int (num_test_samples) - predicted class index
for every test sample
"""

n_train = distances.shape[0]
n_test = distances.shape[0]
prediction = np.zeros(n_test, np.int)

"""
YOUR CODE IS HERE
"""
pass
79 changes: 79 additions & 0 deletions metrics.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import numpy as np


def binary_classification_metrics(y_pred, y_true):
"""
Computes metrics for binary classification
Arguments:
y_pred, np array (num_samples) - model predictions
y_true, np array (num_samples) - true labels
Returns:
precision, recall, f1, accuracy - classification metrics
"""

# TODO: implement metrics!
# Some helpful links:
# https://en.wikipedia.org/wiki/Precision_and_recall
# https://en.wikipedia.org/wiki/F1_score

"""
YOUR CODE IS HERE
"""
pass


def multiclass_accuracy(y_pred, y_true):
"""
Computes metrics for multiclass classification
Arguments:
y_pred, np array of int (num_samples) - model predictions
y_true, np array of int (num_samples) - true labels
Returns:
accuracy - ratio of accurate predictions to total samples
"""

"""
YOUR CODE IS HERE
"""
pass


def r_squared(y_pred, y_true):
"""
Computes r-squared for regression
Arguments:
y_pred, np array of int (num_samples) - model predictions
y_true, np array of int (num_samples) - true values
Returns:
r2 - r-squared value
"""
y_mean = np.mean(y_true)
return 1 - ((np.square(np.subtract(y_pred, y_true))).sum())/np.square(y_true - y_mean).sum()



def mse(y_pred, y_true):
"""
Computes mean squared error
Arguments:
y_pred, np array of int (num_samples) - model predictions
y_true, np array of int (num_samples) - true values
Returns:
mse - mean squared error
"""
#return np.mean((y_pred - y_true)**2)
return np.square(y_pred - y_true).mean()


def mae(y_pred, y_true):
"""
Computes mean absolut error
Arguments:
y_pred, np array of int (num_samples) - model predictions
y_true, np array of int (num_samples) - true values
Returns:
mae - mean absolut error
"""
return np.absolute(np.subtract(y_pred, y_true)).mean()


5 changes: 5 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
numpy
matplotlib
pandas
scikit-learn
seaborn
Binary file added socialization.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.