### Introduction:

*Classification is computed from a simple majority vote of the nearest neighbors of each point: a query point is assigned the data class which has the most representatives within the nearest neighbors of the point. *KNeighborsClassifier implements learning based on the k nearest neighbors of each query point, where k is an integer value specified by the user.

### Parameter:

*n_neighbors : int, default=5

### Implementation of KNeighborsClassifier:

import numpy as np
import matplotlib.pyplot as plt
from sklearn.datasets import make_blobs
from sklearn.model_selection import train_test_split

class KNN:

def __init__(selfn_neighbors=5):
self.n = n_neighbors

def fit(selfXy):
self.X = X
self.y = y

def get_point(selfpoint):

dist = np.sqrt(((self.X - point) ** 2).sum(axis=1))

idx = dist.argsort()
near_idx = idx[:self.n]

opt = self.y[near_idx]
values, freq = np.unique(opt, return_counts=True)

return values[freq.argmax()]

def predict(selfX):

result = []
for point in X:
result.append(self.get_point(point))

return np.array(result)

def main():

X, y = make_blobs(n_samples=1000, centers=4, random_state=0)
X_train , X_test , y_train , y_test = train_test_split(X , y , test_size = 0.1 , random_state = 42)

knn = KNN()
knn.fit(X_train,y_train)
y_pred = knn.predict(X_test)
print(y_test)
print(y_pred)

if __name__=="__main__":
main()

### Output:

[3 0 0 3 0 1 1 3 1 2 0 0 3 1 2 0 1 0 1 3 2 2 1 1 3 1 2 3 3 3 1 3 0 0 3 2 1 1 2 3 3 2 3 2 3 3 0 3 3 2 2 2 3 1 1 1 1 3 0 3 3 3 1 0 1 2 3 2 3 1 3 2 0 0 0 3 2 2 0 0 3 0 3 2 0 2 3 0 1 1 2 3 0 1 3 2 1 2 2 0] [0 0 0 3 0 1 1 3 1 2 0 0 0 1 2 0 1 3 1 3 2 2 1 1 0 1 2 3 3 3 1 3 0 0 3 2 1 1 2 3 3 2 3 2 3 3 0 3 3 2 2 0 3 1 1 1 1 3 0 3 3 3 1 0 1 2 3 2 3 1 3 2 1 0 0 3 2 2 0 0 3 1 3 2 0 2 3 0 1 1 2 3 0 1 3 2 1 2 2 2]

### Using Inbuilt Library:

from sklearn.neighbors import KNeighborsClassifier
k = KNeighborsClassifier()
k.fit(X_train,y_train)
y_pred = k.predict(X_test)
print(y_test)
print(y_pred)

### Output:

[3 0 0 3 0 1 1 3 1 2 0 0 3 1 2 0 1 0 1 3 2 2 1 1 3 1 2 3 3 3 1 3 0 0 3 2 1 1 2 3 3 2 3 2 3 3 0 3 3 2 2 2 3 1 1 1 1 3 0 3 3 3 1 0 1 2 3 2 3 1 3 2 0 0 0 3 2 2 0 0 3 0 3 2 0 2 3 0 1 1 2 3 0 1 3 2 1 2 2 0] [0 0 0 3 0 1 1 3 1 2 0 0 0 1 2 0 1 3 1 3 2 2 1 1 0 1 2 3 3 3 1 3 0 0 3 2 1 1 2 3 3 2 3 2 3 3 0 3 3 2 2 0 3 1 1 1 1 3 0 3 3 3 1 0 1 2 3 2 3 1 3 2 1 0 0 3 2 2 0 0 3 1 3 2 0 2 3 0 1 1 2 3 0 1 3 2 1 2 2 2]

#### More Articles of Piyush unknown:

Name Views Likes
Python Scikit Learn - Precision Score 401 1
Python Scikit Learn - Mean Absolute Error 333 1
Python Scikit Learn Linear Model - MNIST with Logistic Regression 605 1
Python Scikit Learn Metrics - DCG Score 549 1
Python Scikit Learn Neighbors - KNeighborsClassifier 424 2
Python Scikit Learn Metrics - Sigmoid Kernel 629 2
Python Scikit Learn Metrics - Cohen Kappa Score 2004 1
Python Scikit Learn Metrics - ROC Curve 264 1
Python Scikit Learn Metrics - ROC AUC Score 420 1
Python Scikit Learn Metrics - Mean Poisson Deviance 810 1
Python Scikit Learn Metrics - NaN Euclidean Distances 1452 1
Python Scikit Learn - Jaccard Score 1198 1
Python Scikit Learn Introduction 321 1
Python Scikit Learn - FBeta Score 332 1
Python Scikit Learn - Max Error 320 1
Python Scikit Learn Metrics - Chi2 Kernel 441 2
Python Scikit Learn Metrics - Manhattan Distances 636 1
Python Scikit Learn Metrics - Euclidean Distance 302 1
Python Scikit Learn Model Selection - Train Test Split 490 1
Python Scikit Learn Metrics - Laplacian Kernel 619 2
Python Scikit Learn Metrics - Zero One Loss 917 1
Python Scikit Learn - Hamming Loss 326 1
Python Scikit Learn Metrics - Linear Kernel 460 1
Pytho Scikit Learn - Balanced Accuracy Score 692 1
Python Scikit Learn - Accuracy Score 358 1
Python Scikit Learn - F1 score 396 1
Python Scikit Learn Metrics - Polynomial Kernel 483 2
Python Scikit Learn Metrics - AUC 254 1
Python Scikit Learn - Mean Squared Log Error 291 1
Python Scikit Learn Metrics - Precision Recall Fscore Support 1028 1
Python Scikit Learn - R2 Score 351 1
Python Scikit Learn Preprocessing - Standard Scaler 242 1
Python Scikit Learn Linear Model - Linear Regression 230 1
Python Scikit Learn Metrics - Brier Score 577 1
Python Scikit Learn - Median Absolute Error 665 1
Python Scikit Learn Metrics - Multilabel Confusion Matrix 764 1
Python Scikit Learn Linear Model - Logistic Regression 252 1
Python Scikit Learn - Mean Squared Error 367 1
Python Scikit Learn - Matthews Correlation Coefficient 1142 1
Python Scikit Learn Metrics - Cosine Similarity 503 1
Python Scikit Learn Metrics - NDCG Score 1772 1
Python Scikit Learn Metrics - Pairwise Distances Argmin 411 2
Python Scikit Learn Preprocessing - Max Abs Scaler 686 1
Python Scikit Learn Preprocessing - Min Max Scaler 364 1
Python Scikit Learn Metrics - Mean Gamma Deviance 908 1
Python Scikit Learn - Confusion Matrix 346 1
Python Scikit Learn - Recall Score 329 1
Python Scikit Learn Metrics - Cosine Distances 293 1
Python Scikit Learn - Explained Variance Score 840 1
Python Scikit Learn Metrics - Precision Recall Curve 537 1