机器学习—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 GridSearchCV digits = 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