原文链接: sklearn 入门
上一篇: vue todos and maze
下一篇: itchat 简单使用
分割数据集
随机分割
x = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
y = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.3)
print(x_train)
print(y_train)
print(x_test)
print(y_test)
[3, 0, 7, 9, 5, 8, 1]
[3, 0, 7, 9, 5, 8, 1]
[2, 4, 6]
[2, 4, 6]
简单分类器的使用
import numpy as np
from sklearn import datasets
from sklearn.cross_validation import train_test_split
from sklearn.neighbors import KNeighborsClassifier
iris = datasets.load_iris()
#数据
iris_x = iris.data
#标签
iris_y = iris.target
# //分割数据为训练集和测试集,并且打乱数据,
x_train, x_test, y_train, y_test = train_test_split(iris_x, iris_y, test_size=0.3)
# 创建分类器
knn = KNeighborsClassifier()
# 训练数据
knn.fit(x_train, y_train)
# 输出预测结果
print(knn.predict(x_test))
# 输出真实结果
print(y_test)