<jupyter_start><jupyter_text>KNN CLASSIFICATION ALGORITHM  Creating knn classification algorithm<jupyter_code>import matplotlib.pyplot as plt
import cv2
image = cv2.imread("./image.png")
plt.imshow(image)<jupyter_output><empty_output><jupyter_text>Algorithm :     - Finding distence with all predicted point to the label which is nearest    - Then sort that data according to highest count to lowest    - Then select top k nearest neighbors     - Then return most occurence class<jupyter_code>class Knn_classification:
    # defining n-neighbors in contructor 
    def __init__(self,n_neighbors):
        self.n_neighbors=n_neighbors
    # in this fit method we just assigning variables     
    def fit(self,features,labels):
        self.features = features.astype(int)
        self.labels = labels
    # this is main method where actual algorithm implemented      
    def distance_from_k(self,point):
        # here what we does is we measure distance from point to every near neighbor of features
        distance_arr=[]
        for distance,point_class in zip(self.features,self.labels):
            # measuring distance with the distance formula (x-x1)**2 + (y-y1)**2
            distance = ((point-distance)**2).sum()
            # storing distance with class
            distance_arr.append([distance,point_class])
            # now will have every point distance with our point
        
        # so will sort an list by it's distance
        sorted_distance_list = sorted(distance_arr)
        # now will find most counted class item 
        item,count = np.unique(np.array(sorted_distance_list[:self.n_neighbors]),return_counts=True)
        ans = item[np.argmax(count)]
        return ans
    def predict(self,features):
        result = []
        for point in features:
            result.append(self.distance_from_k(point))
        return result
    def score(self,actual_data,predicted_data):
        return (sum(actual_data)-sum(predicted_data))/len(predicted_data)
import numpy as np
dataset = np.load("mnist_train_small.npy")
features = dataset[:, 1:]
labels =   dataset[:, 0]
model = Knn_classification(5)
model.fit(features,labels)
prediction = model.predict(features[:10])
labels[:10]
model.score(features[:100],y_test[:100])<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.08995173321632295, 'fraction_numerical': 0.008775778850372971, 'mean_word_length': 3.4186046511627906, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, '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': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '12050', 'license': 'MIT', 'n_tokens_mistral': 677, 'n_tokens_neox': 653, 'n_words': 181, 'path': 'KNN-CLASSIFICATION/KNN.ipynb', 'repo_name': 'aditya-2703/MACHINE_LEARNING', 'size': '2279'}