<jupyter_start><jupyter_text>K-Nearest Neighbours[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/hmatalonga/data-science-bits/blob/master/notebooks/knn.ipynb)KNN algorithm implementation from scratch.**Acknowledgements:** This code was originally inspired by [MLfromscratch](https://github.com/python-engineer/MLfromscratch) repository.<jupyter_code>import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

from sklearn.metrics import accuracy_score
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split
# Set seed for reproducibility
SEED = 42
np.random.seed(SEED)
def euclidean_distance(x1, x2):
  return np.sqrt(np.sum((x1 - x2)**2))


class KNN:

  def __init__(self, n_neighbors=3):
    self.n_neighbors = n_neighbors

  def fit(self, X, y):
    self.X_train = X
    self.y_train = y

    return self

  def predict(self, X):
    return np.array([self._predict(x) for x in X])

  def _predict(self, x):
    # 1. Compute distances between x and all examples in the training set
    distances = [euclidean_distance(x, x_train) for x_train in self.X_train]
    
    # 2. Sort by distance and return indices of the first k neighbors
    k_idx = np.argsort(distances)[:self.n_neighbors]
    
    # 3. Extract the labels of the k nearest neighbor training samples
    labels = self.y_train[k_idx]  
    
    # 4. Return the most common class label
    labels_count = np.bincount(labels)

    return np.argmax(labels_count)
data = sns.load_dataset('penguins').dropna()
sns.relplot(x='flipper_length_mm', y='bill_length_mm', hue='species', data=data)
data = pd.get_dummies(data, columns=['island', 'sex'])
data['species'], _ = data['species'].factorize()

data.head()
y = data['species'].to_numpy()
X = data.drop('species', axis=1).to_numpy()

feature_names = data.drop('species', axis=1).columns.tolist()
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=SEED)
scaler = StandardScaler()

X_train = scaler.fit_transform(X_train)
X_test = scaler.transform(X_test)
clf = KNN(n_neighbors=3).fit(X_train, y_train)

y_pred = clf.predict(X_test)

print(f"Accuracy: {accuracy_score(y_test, y_pred):.3f}")
from sklearn.neighbors import KNeighborsClassifier

sklearn_knn = KNeighborsClassifier(n_neighbors=3).fit(X_train, y_train)

y_pred = sklearn_knn.predict(X_test)

print(f"Accuracy: {accuracy_score(y_test, y_pred):.3f}")<jupyter_output>Accuracy: 0.985
{'fraction_non_alphanumeric': 0.12342271293375394, 'fraction_numerical': 0.00946372239747634, 'mean_word_length': 5.233415233415234, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 3, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '200187', 'license': 'MIT', 'n_tokens_mistral': 966, 'n_tokens_neox': 916, 'n_words': 198, 'path': 'notebooks/knn.ipynb', 'repo_name': 'hmatalonga/data-science-bits', 'size': '2536'}