sklearn iris快速

#导入模块
from sklearn.model_selection import train_test_split
from sklearn import datasets
#k近邻函数
from sklearn.neighbors import KNeighborsClassifier
from matplotlib import pyplot as plt
import numpy as np
iris = datasets.load_iris()
#导入数据和标签
iris_X = iris.data
iris_y = iris.target
#划分为训练集和测试集数据
print(iris)
X_train, X_test, y_train, y_test = train_test_split(iris_X, iris_y, test_size=0.1)
#print(y_train)
#设置knn分类器
knn = KNeighborsClassifier()
#进行训练
knn.fit(X_train,y_train)
#使用训练好的knn进行数据预测
print(knn.predict(X_test))
print(y_test)
x_axis=np.array([i+1 for i in range(150)])
plt.scatter(x_axis,iris_X[:,0],edgecolors="black")
plt.scatter(x_axis,iris_X[:,1],edgecolors="red")
plt.scatter(x_axis,iris_X[:,2],edgecolors="blue")
plt.scatter(x_axis,iris_X[:,3],edgecolors="green")
plt.show()
上一篇:3.K均值算法


下一篇:深入浅出KNN算法(二) sklearn KNN实践