以鸢尾花数据为例,将测试数据中的每朵鸢尾花进行预测,并将预测结果与已知标签结果进行对比,可以计算精度来衡量模型的优劣。精度就是品种预测正确的结果占比。
import numpy as np
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier
iris_dataset=load_iris()
X_train,X_test,y_train,y_test=train_test_split(
iris_dataset['data'],iris_dataset['target'],random_state=0
)
knn=KNeighborsClassifier(n_neighbors=1)
knn.fit(X_train,y_train)
KNeighborsClassifier(algorithm='auto',leaf_size=30,metric='minkowski',metric_params=None,n_jobs=1,n_neighbors=1,p=2,weights='uniform')
y_pred=knn.predict(X_test)
print('Test:{}'.format(y_pred))
print('score:{:.2f}'.format(np.mean(y_pred==y_test)))
从结果来看,模型的精度约为0.97,也就是预测结果里有97%都是正确的