【机器学习】【监督学习】【算法01-实例02】K近邻(k-nearest neighbor)-鸢尾花

鸢尾花的分类实例是一个非常经典的例子。也是sklearn中一个非常经典的数据集合。我们首先从数据集的结构特点来开始。

1.数据集的加载

# 从sklearn.datasets 导入 iris数据加载器。
from sklearn.datasets import load_iris
# 使用加载器读取数据并且存入变量iris。
iris = load_iris()
# 查验数据规模。
#如果想要在pycharm这类的编辑器里面显示,加上print函数即可
iris.data.shape

2.数据集的描述

#注意此处可能python3以上的版本需要添加print()
print iris.DESCR

简单说明:这个数据集一共有150个样本,分成三类。四个特征:
- sepal length in cm
- sepal width in cm
- petal length in cm
- petal width in cm
单位均统一为cm,

Data Set Characteristics:
:Number of Instances: 150 (50 in each of three classes)
:Number of Attributes: 4 numeric, predictive attributes and the class
:Attribute Information:
    - sepal length in cm
    - sepal width in cm
    - petal length in cm
    - petal width in cm
    - class:
            - Iris-Setosa
            - Iris-Versicolour
            - Iris-Virginica
:Summary Statistics:

============== ==== ==== ======= ===== ====================
                Min  Max   Mean    SD   Class Correlation
============== ==== ==== ======= ===== ====================
sepal length:   4.3  7.9   5.84   0.83    0.7826
sepal width:    2.0  4.4   3.05   0.43   -0.4194
petal length:   1.0  6.9   3.76   1.76    0.9490  (high!)
petal width:    0.1  2.5   1.20  0.76     0.9565  (high!)
============== ==== ==== ======= ===== ====================

:Missing Attribute Values: None
:Class Distribution: 33.3% for each of 3 classes.
:Creator: R.A. Fisher
:Donor: Michael Marshall (MARSHALL%[email protected])
:Date: July, 1988

3.数据集分割

获得了数据,就可以开始我们的”机器学习“了~
首先,按照老样子,我们先对数据集进行切割。
按照3:1的比例来切割训练集以及测试集,当然也可以加入验证集,但是由于我们数量较少,就没有再设立验证集了。

# 从sklearn.cross_validation里选择导入train_test_split用于数据分割。
from sklearn.cross_validation import train_test_split
# 从使用train_test_split,利用随机种子random_state采样25%的数据作为测试集。
X_train, X_test, y_train, y_test = train_test_split(iris.data, iris.target, test_size=0.25, random_state=33)

4.使用KNN对数据进行训练

# 从sklearn.preprocessing里选择导入数据标准化模块。
from sklearn.preprocessing import StandardScaler

# 从sklearn.neighbors里选择导入KNeighborsClassifier,即K近邻分类器。
from sklearn.neighbors import KNeighborsClassifier

# 对训练和测试的特征数据进行标准化。
ss = StandardScaler()
X_train = ss.fit_transform(X_train)
X_test = ss.transform(X_test)

# 使用K近邻分类器对测试数据进行类别预测,预测结果储存在变量y_predict中。
knc = KNeighborsClassifier()
knc.fit(X_train, y_train)
y_predict = knc.predict(X_test)

由于我们这里做的是对于算法的应用实例,就没有再自己去实现KNN算法来对数据进行训练了。我非常推荐大家按照算法的描述自己实现每一个机器学习算法,但是在实际的开发中或者使用中,我们一般都使用成熟的封装好的package
如需要请移步到本人的博客:

K近邻(k-nearest neighbor)实现细节


5.性能评测

这里我们常用常使用的准确率,召回率,精确率,以及F1指标来判定。

  • 首先可以直接用模型自带的knc.score来看看准确率
# 使用模型自带的评估函数进行准确性测评。
print 'The accuracy of K-Nearest Neighbor Classifier is', knc.score(X_test, y_test)

打印的结果:

The accuracy of K-Nearest Neighbor Classifier is 0.894736842105
  • 然后用更加详细的分析模型代码
#使用classification_report模块对预测结果做更加详细的分析。
from sklearn.metrics import classification_report
print classification_report(y_test, y_predict, target_names=iris.target_names)

输出结果:

               precision    recall   f1-score   support

     setosa       1.00      1.00      1.00         8
 versicolor       0.73      1.00      0.85        11
  virginica       1.00      0.79      0.88        19

avg / total       0.92      0.89      0.90        38

#说明:平均准确率 0.92,召回率0.89,F1指标0.9


6.总结

虽然整个过程很简单,但是我们需要明白的理解的点很多,包括数据集的获取,分割,以及对于相应算法的使用。虽然我们在代码的实现中,隐藏了很多原理性的东西。这有助于我们的快速开发,但是我认为任何一名机器学习或者数据科学从业者,若是从事技术相关的工作和研究,对于原理的掌握还是必不可少的。

希望每个能够看到博文的有缘人都自己尝试去做一些这样的实现。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章