代码如下:
from sklearn import datasets iris=datasets.load_iris() x=iris.data y=iris.target from sklearn.cross_validation import train_test_split x_train,x_test,y_train,y_test=train_test_split(x,y,test_size= .5) #使用决策树 # from sklearn import tree # my_classifier=tree.DecisionTreeClassifier() #另一种分类器 from sklearn.neighbors import KNeighborsClassifier my_classifier=KNeighborsClassifier() my_classifier.fit(x_train,y_train)#训练分类器 predictions=my_classifier.predict(x_test)#对测试数据进行结果预测 print(predictions)#输出预测结果 from sklearn.metrics import accuracy_score print(accuracy_score(y_test,predictions))#预测的准确率