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
from sklearn.datasets import load_iris | |
from KNearestNeighbors import KNearestNeighbors | |
from sklearn.model_selection import train_test_split | |
from sklearn.neighbors import KNeighborsClassifier | |
import pandas as pd | |
dataset = load_iris() | |
X = dataset.data |
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 numpy as np | |
class KNearestNeighbors(): | |
def __init__(self, X_train, y_train, n_neighbors=5, weights='uniform'): | |
self.X_train = X_train | |
self.y_train = y_train | |
self.n_neighbors = n_neighbors |
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
kneighbors(X_test) |
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
data = np.hstack((X, y[:, np.newaxis])) | |
np.random.shuffle(data) | |
split_rate = 0.7 | |
train, test = np.split(data, [int(split_rate*(data.shape[0]))]) | |
X_train = train[:,:-1] | |
y_train = train[:, -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
from sklearn.datasets import make_classification | |
X, y = make_classification(n_samples = 1000, n_features=2, n_redundant=0, n_informative=2, | |
n_clusters_per_class=1, n_classes=3, random_state=21) | |
mu = np.mean(X, 0) | |
sigma = np.std(X, 0) | |
X = (X - mu ) / sigma |
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
def score(X_test, y_test): | |
y_pred = predict(X_test) | |
return float(sum(y_pred == y_test))/ float(len(y_test)) |
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
def predict(X_test, weights='uniform'): | |
class_num = 3 | |
if weights=='uniform': | |
neighbors = kneighbors(X_test) | |
y_pred = np.array([np.argmax(np.bincount(y_train[neighbor])) for neighbor in neighbors]) | |
return y_pred | |
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
def kneighbors(X_test, return_distance=False): | |
n_neighbors = 5 | |
dist = [] | |
neigh_ind = [] | |
point_dist = [euclidian_distance(x_test, X_train) for x_test in X_test] | |
for row in point_dist: | |
enum_neigh = enumerate(row) |
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
def euclidian_distance(a, b): | |
return np.sqrt(np.sum((a-b)**2, axis=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
import numpy as np | |
from sklearn.datasets import make_classification |
NewerOlder