use scikit-learn framework to knn classifier
使用knn【k近邻】算法来进行分类。具体的原理这个地方就不列出来了——如果有机会,在通过文字对他人进行描述的时候,书写一下。
# coding: utf-8
from sklearn import datasets
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier
iris = datasets.load_iris()
iris_X = iris.data
iris_y = iris.target
iris_X
iris_y
print(iris_X[:2, :])
X_train, X_test, y_train, y_test = train_test_split(
iris_X, iris_y, test_size=0.3)
X_train
X_test
print(y_train)
knn = KNeighborsClassifier()
knn.fit(X_train, y_train)
print(knn.predict(X_test))
print(y_test)