<jupyter_start><jupyter_text>Hard Coding Simple k-Nearest Neighbor Classifiers- When used for classification, a query point (or test point) is classified based on the k labeled training points that are closest to that query point.<jupyter_code>import pandas as pd
import numpy as np
import matplotlib.pyplot as plt<jupyter_output><empty_output><jupyter_text>Using the iris data as example<jupyter_code>from sklearn import datasets

iris = datasets.load_iris()

X = iris["data"][:100,:2]
Y = iris["target"][:100]

plt.scatter(X[:,0],X[:,1], c = Y)
plt.show()
# calculate the Euclidean distance between two vectors
def euclidean_distance(vector1: list, vector2: list) -> float:
    distance = 0.0
    for i in range(len(vector1)):
        distance += (vector1[i] - vector2[i])**2
    return np.sqrt(distance)
from collections.abc import Callable

class SimpleKNNClassifier:
    
    def __init__(self, k: int, distance_function: Callable):
        self.k = k
        self.distance_function = distance_function
        
    def load_data(self, X_train: np.array, Y_train: np.array):
        self.X_train = X_train; self.Y_train = Y_train
        
    def predict(self, X_test: np.array) -> np.array:
        prediction = []
        for reference in X_test:
            distance_list = [self.distance_function(reference, target) for target in self.X_train]
            sorted_list = np.argsort(distance_list)
            major_class = np.argmax(np.bincount(self.Y_train[sorted_list[:self.k]]))
            prediction.append(major_class)   
        return np.array(prediction)        
from sklearn.model_selection import train_test_split

X_train, X_test, Y_train, Y_test = train_test_split(X, Y,\
                                                    test_size = 0.40, random_state = 89)
KNN = SimpleKNNClassifier(k = 10, distance_function = euclidean_distance)

KNN.load_data(X_train, Y_train)
Y_pred = KNN.predict(X_test)
Y_pred
from sklearn.metrics import accuracy_score

accuracy_score(Y_pred, Y_test)<jupyter_output><empty_output><jupyter_text>*** Detailed Steps1. Define a function to calculate the distance between two points2. Use the distance function to get the distance between a test point and all train data points3. Sort distance measurements and find the k nearest neighbors*For Simple Classification*4. Use majority class labels of the k nearest neighbors to predict the label of the test point*For Simple Classification*4. Use mean traget value of the k nearest neighbors to predict the label of the test point5. Repeat steps 1 through 4 until all test data points are predicted  Define a function to calculate the distance between two points<jupyter_code># calculate the Euclidean distance between two vectors
def euclidean_distance(vector1, vector2):
    distance = 0.0
    for i in range(len(vector1)):
        distance += (vector1[i] - vector2[i])**2
    return np.sqrt(distance)
euclidean_distance(X[0], X[1])<jupyter_output><empty_output><jupyter_text>Use the distance function to get the distance between a test point and all known data points<jupyter_code># set the jth point as our reference point
distance_list = [euclidean_distance(X_test[0], target) for target in X_train]
np.array(distance_list)<jupyter_output><empty_output><jupyter_text>Sort distance measurements to find the points closest to the test point (i.e., find the nearest neighbors)<jupyter_code>sorted_list= np.argsort(distance_list)
sorted_list<jupyter_output><empty_output><jupyter_text>Use majority class labels of those closest points to predict the label of the test point<jupyter_code>k = 5
# class for the neighbors
Y_train[sorted_list[:k]]
# count the classes
np.bincount(Y_train[sorted_list[:k]])
k = 5
major_class = np.argmax(np.bincount(Y_train[sorted_list[:k]]))
major_class<jupyter_output><empty_output><jupyter_text>Repeat steps 1 through 4 until all test data points are classified<jupyter_code>prediction = []
for j in X_test:
    distance_list = [euclidean_distance(j, i) for i in X_train]
    sorted_list= np.argsort(distance_list)
    major_class = np.argmax(np.bincount(Y_train[sorted_list[:k]]))
    prediction.append(major_class)
    
prediction = np.array(prediction)
prediction
Y_test<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.08684335068622812, 'fraction_numerical': 0.011121628017037387, 'mean_word_length': 4.583883751651255, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '480918', 'license': 'Apache-2.0', 'n_tokens_mistral': 1349, 'n_tokens_neox': 1313, 'n_words': 397, 'path': '.ipynb_checkpoints/Simple-k-Nearest-Neighbor-Classifiers-checkpoint.ipynb', 'repo_name': 'charliecharlie29/Random-Code-Library', 'size': '4226'}