knn code

Solutions on MaxInterview for knn code by the best coders in the world

showing results for - "knn code"
Benjamin
28 Nov 2018
1from sklearn.neighbors import KNeighborsClassifier
2from sklearn.metrics import accuracy_score
3from sklearn.metrics import classification_report
4
5X = df.drop('column name', axis = 1)
6y = df.["Column name(target)"].values
7
8X_train_knn, X_test_knn, y_train_knn, y_test_knn = train_test_split(X, y, test_size=0.2, random_state=10)
9
10knn_classifier = KNeighborsClassifier(n_neighbors=6, p=2, metric='minkowski')
11
12knn_classifier.fit(X_train_knn, y_train_knn)
13
14knn_classifier.score(X_test, y_test) # to find score 
15
16pred_test_knn = knn_classifier.predict(X_test_knn)
17pred_test_knn #prediction
18
19class_report_knn = (classification_report(y_test_knn, pred_test_knn))
20print(class_report_knn) #classification report