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

Posted liuzhiqaingxyz

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了莫烦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

 

以上是关于莫烦scikit-learn学习自修第三天通用训练模型的主要内容,如果未能解决你的问题,请参考以下文章

莫烦scikit-learn学习自修第五天训练模型的属性

莫烦scikit-learn学习自修第四天内置训练数据集

莫烦sklearn学习自修第九天过拟合问题处理

莫烦sklearn学习自修第八天过拟合问题

莫烦keras学习自修第四天分类问题

莫烦theano学习自修第二天激励函数