莫烦scikit-learn学习自修第三天【通用训练模型】

1. 代码实战

#!/usr/bin/env python
#!_*_ coding:UTF-8 _*_

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

# 对数据集进行拆分为训练数据集和测试数据集,拆分比率为7:3
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 "++++++++++预测++++++++++++"
print knn.predict(X_test)

print "++++++++++真实值++++++++++"
print y_test

结果:

/Users/liudaoqiang/PycharmProjects/numpy/venv/bin/python /Users/liudaoqiang/Project/python_project/scikit-learn-day01/common_train.py
/Users/liudaoqiang/PycharmProjects/numpy/venv/lib/python2.7/site-packages/sklearn/cross_validation.py:41: DeprecationWarning: This module was deprecated in version 0.18 in favor of the model_selection module into which all the refactored classes and functions are moved. Also note that the interface of the new CV iterators are different from that of this module. This module will be removed in 0.20.
  "This module will be removed in 0.20.", DeprecationWarning)
++++++++++预测++++++++++++
[0 2 2 1 2 2 1 0 1 2 2 2 2 1 1 2 2 2 2 0 0 1 0 1 1 0 2 2 1 0 0 2 0 2 2 1 2
 1 0 2 1 2 0 1 1]
++++++++++真实值++++++++++
[0 2 2 1 2 2 1 0 1 2 2 2 1 1 2 2 2 2 2 0 0 1 0 1 1 0 2 2 1 0 0 2 0 1 2 1 2
 1 0 2 1 2 0 1 1]

Process finished with exit code 0

猜你喜欢

转载自www.cnblogs.com/liuzhiqaingxyz/p/9589927.html