KNN-学习笔记

仅供学习使用

练习1

# coding:utf-8
# 2019/10/16 16:49
# huihui
# ref:
import numpy as np
from sklearn import datasets
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier

iris = datasets.load_iris()
X = iris.data
y = iris.target
print(X, y)

X_train, X_test, y_train, y_test = train_test_split(X, y,random_state=2003)
clf = KNeighborsClassifier(n_neighbors=3)
clf.fit(X_train, y_train)

correct = np.count_nonzero((clf.predict(X_test) == y_test) == True)
print("准确率:%.3f" % (correct / len(X_test)))
上一篇:KNN算法


下一篇:Python3入门机器学习经典算法与应用 学习 教程