knn train code code example
Example: knn code
from sklearn.neighbors import KNeighborsClassifier
from sklearn.metrics import accuracy_score
from sklearn.metrics import classification_report
X = df.drop('column name', axis = 1)
y = df.["Column name(target)"].values
X_train_knn, X_test_knn, y_train_knn, y_test_knn = train_test_split(X, y, test_size=0.2, random_state=10)
knn_classifier = KNeighborsClassifier(n_neighbors=6, p=2, metric='minkowski')
knn_classifier.fit(X_train_knn, y_train_knn)
knn_classifier.score(X_test, y_test)
pred_test_knn = knn_classifier.predict(X_test_knn)
pred_test_knn
class_report_knn = (classification_report(y_test_knn, pred_test_knn))
print(class_report_knn)