import pickleimport matplotlib.pyplot as pltfrom sklearn import datasetsfrom sklearn.model_selection import train_test_splitfrom sklearn.neighbors import KNeighborsClassifier from sklearn.model_selection import cross_val_scorefrom sklearn i
import pickle import matplotlib.pyplot as plt from sklearn import datasets from sklearn.model_selection import train_test_split from sklearn.neighbors import KNeighborsClassifier from sklearn.model_selection import cross_val_score from sklearn import metrics iris = datasets.load_iris() X_train,X_test,y_train,y_test = train_test_split(iris.data, iris.target, test_size=0.3) k_range = range(1,31) k_score = [] for k in k_range: knn = KNeighborsClassifier(n_neighbors=k) scores = cross_val_score(knn, X_train, y_train, cv=10, scoring=‘accuracy‘) k_score.append(scores.mean()) plt.figure() plt.plot(k_range, k_score) plt.xlabel(‘Value of k for KNN‘) plt.ylabel(‘CrossValidation accuracy‘) plt.show() max_score = max(k_score) print("max score:", max_score) best_k = k_range[k_score.index(max_score)] knn = KNeighborsClassifier(n_neighbors=best_k) knn.fit(X_train, y_train) y_predict = knn.predict(X_test) print("test data accuracy:", metrics.accuracy_score(y_test, y_predict)) print("classification report:\n”, metrics.classification_report(y_test, y_predict)) with open("knn.pkl", "wb") as f: pickle.dump(knn, f)