机器学习—K近邻
一、算法原理
還是圖片格式~
?
二、sklearn實現
import pandas as pd import numpy as np import matplotlib.pyplot as plt import matplotlib as mpl import seaborn as sns mpl.rcParams['font.sans-serif'] = [u'SimHei'] mpl.rcParams['axes.unicode_minus'] = False from sklearn.datasets import load_digits from sklearn.model_selection import train_test_split from sklearn.metrics import accuracy_score from sklearn.neighbors import KNeighborsClassifier from sklearn.model_selection import GridSearchCVdigits = load_digits() x_data = digits.data y_data = digits.target x_train,x_test,y_train,y_test = train_test_split(x_data,y_data) knn = KNeighborsClassifier() model_knn = GridSearchCV(knn,param_grid=({'n_neighbors':np.arange(1,5)}),cv=10) model_knn.fit(x_train,y_train) print(model_knn.best_params_) y_hat = model_knn.predict(x_test) print('正確率',accuracy_score(y_hat,y_test))正確率好高啊,直接用1個k來做。。。
{'n_neighbors': 1}
正確率 0.986666666667
?
轉載于:https://www.cnblogs.com/slowlyslowly/p/8811177.html
總結
- 上一篇: 在VMware中装Win server
- 下一篇: python文件操作,自动关闭