October 2018
Intermediate to advanced
172 pages
4h 6m
English
Now that we have the training and test splits, we can implement the k-NN algorithm on the training sets and evaluate its score on the test sets. We can do this by using the following code:
from sklearn.neighbors import KNeighborsClassifier#Initializing the kNN classifier with 3 neighbors knn_classifier = KNeighborsClassifier(n_neighbors=3)#Fitting the classifier on the training data knn_classifier.fit(X_train, y_train)#Extracting the accuracy score from the test setsknn_classifier.score(X_test, y_test)
In the preceding code, we first initialize a k-NN classifier with three neighbors. The number of neighbors is chosen arbitrarily, and three is a good starting number. Next, we use the .fit() method ...
Read now
Unlock full access