Learn Before
Relation
Implemention of KNN in Python
Algorithm: Given a training set X_train with labels y_train, and given a new instance x_test to be classified:
- Find the most similar instances (let's call them X_NN) to x_test that are in X_train.
- Get the labels y_NN for the instances in X_NN
- Predict the label for x_test by combining the labels y_NN Implementation: from sklearn.neighbors import KNeighborsClassifier X_train, X_test, y_train, y_test = train_test_split(X_C1, y_C1, random_state = 0) knnc = KNeighborsClassifier(n_neighbors = 5).fit(X_train, y_train) knnc.predict(X_test) knnc.score(X_test, y_test)
0
6
Updated 2020-10-05
Tags
Data Science