Skip to content

Instantly share code, notes, and snippets.

View leventbass's full-sized avatar

Levent Baş leventbass

  • Istanbul, Turkey
View GitHub Profile
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
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
kneighbors(X_test)
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]
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
def score(X_test, y_test):
y_pred = predict(X_test)
return float(sum(y_pred == y_test))/ float(len(y_test))
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
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)
def euclidian_distance(a, b):
return np.sqrt(np.sum((a-b)**2, axis=1))
@leventbass
leventbass / knn_import.py
Last active September 16, 2019 11:53
KNN from Scratch
import numpy as np
from sklearn.datasets import make_classification