from sklearn import datasets from sklearn.model_selection import train_test_split from sklearn.neighbors import KNeighborsClassifier from sklearn import metrics iris=datasets.load_iris() iris_x = iris.data iris_y = iris.target x_train,x_test,y_test = train_test_split(iris_x,iris_y,test_size = 0.3) model_knn = KNeighborsClassifier() model_knn.fit(x_train,y_train) predict = model_knn.predict(x_test) for i in range(len(predict)): print("第%d次测试结果:真实值:%s\t预测值:%s" % ((i+1),iris.target_names[predict[i]],iris.target_names[y_test[i]])) print("准确值:%0.2f%%" % (metrics.precision_score(y_test,predict,average='macro')*100)) model_predict1 = model_knn.predict([[5.9,3.0,3.5,1.5]]) model_predict2 = model_knn.predict([[1.5,1.0,2.0,1.0]]) print("[5.9,3.0,3.5,1.5]预测结果为:",iris.target_names[model_predict1[0]]) print("[1.5,1.0,2.0,1.0]预测结果为:",iris.target_names[model_predict2[0]])
sklearn鸢尾花测试案例
最新推荐文章于 2024-05-22 19:09:13 发布