【机器学习实验】学习Python来分类现实世界的数据

引入

一个机器能够依据照片来辨别鲜花的品种吗?在机器学习角度,这事实上是一个分类问题。即机器依据不同品种鲜花的数据进行学习。使其能够对未标记的測试图片数据进行分类。
这一小节。我们还是从scikit-learn出发,理解主要的分类原则,多动手实践。

Iris数据集

Iris flower数据集是1936年由Sir Ronald Fisher引入的经典多维数据集。能够作为判别分析(discriminant analysis)的样本。该数据集包括Iris花的三个品种(Iris setosa, Iris virginica and Iris versicolor)各50个样本,每个样本还有4个特征參数(各自是萼片的长宽和花瓣的长宽,以厘米为单位),Fisher利用这个数据集开发了一个线性判别模型来辨别花朵的品种。


基于Fisher的线性判别模型,该数据集成为了机器学习中各种分类技术的典型实验案例。

如今我们要解决的分类问题是。当我们看到一个新的iris花朵,我们能否依据以上測量參数成功预測新iris花朵的品种。
我们利用给定标签的数据,设计一种规则进而应用到其它样本中做预測。这是主要的监督问题(分类问题)。


因为iris数据集样本量和维度都非常小,所以能够方便进行可视化和操作。

数据的可视化(visualization)

scikit-learn自带有一些经典的数据集。比方用于分类的iris和digits数据集,还实用于回归分析的boston house prices数据集。
能够通过以下的方式加载数据:

from sklearn import datasets
iris = datasets.load_iris()
digits = datasets.load_digits()

该数据集是一种字典结构。数据存储在.data成员中,输出标签存储在.target成员中。

画出随意两维的数据散点图

能够用以下的方式画出随意两个维度的散点图。这里以第一维sepal length和第二维数据sepal width为例:

from sklearn import datasets
import matplotlib.pyplot as plt
import numpy as np

iris = datasets.load_iris()
irisFeatures = iris["data"]
irisFeaturesName = iris["feature_names"]
irisLabels = iris["target"]

def scatter_plot(dim1, dim2):
    for t,marker,color in zip(xrange(3),">ox","rgb"):
        # zip()接受随意多个序列參数,返回一个元组tuple列表
        # 用不同的标记和颜色画出每种品种iris花朵的前两维数据
        # We plot each class on its own to get different colored markers
        plt.scatter(irisFeatures[irisLabels == t,dim1],
                    irisFeatures[irisLabels == t,dim2],marker=marker,c=color)
    dim_meaning = {0:'setal length',1:'setal width',2:'petal length',3:'petal width'}
    plt.xlabel(dim_meaning.get(dim1))
    plt.ylabel(dim_meaning.get(dim2))

plt.subplot(231)
scatter_plot(0,1)
plt.subplot(232)
scatter_plot(0,2)
plt.subplot(233)
scatter_plot(0,3)
plt.subplot(234)
scatter_plot(1,2)
plt.subplot(235)
scatter_plot(1,3)
plt.subplot(236)
scatter_plot(2,3)

plt.show()

效果如图:

构建分类模型

依据某一维度的阈值进行分类

如果我们的目标是差别这三种花朵。我们能够做一些如果。

比方花瓣的长度(petal length)好像将Iris Setosa品种与其它两种花朵区分开来。

我们能够以此来写一段小代码看看这个属性的边界是什么:

petalLength = irisFeatures[:,2] #select the third column,since the features is 150*4
isSetosa = (irisLabels == 0) #label 0 means iris Setosa
maxSetosaPlength = petalLength[isSetosa].max()
minNonSetosaPlength = petalLength[~isSetosa].min()

print ('Maximum of setosa:{0} '.format(maxSetosaPlength))
print ('Minimum of others:{0} '.format(minNonSetosaPlength))

'''
显示结果是:
Maximum of setosa:1.9 
Minimum of others:3.0 
'''

我们依据实验结果能够建立一个简单的分类模型。如果花瓣长度小于2。就是Iris Setosa花朵。否则就是其它两种花朵。
这个模型的结构非常easy。是由数据的一个维度阈值来确定的。

我们通过实验确定这个维度的最佳阈值。
以上的样例将Iris Setosa花朵和其它两种花朵非常容易的分开了,然而我们不能马上确定Iris Virginica花朵和Iris Versicolor花朵的最佳阈值,我们甚至发现。我们无法依据某一维度的阈值将这两种类别非常完美的分开。

比較准确率来得到阈值

我们先选出非Setosa的花朵。

irisFeatures = irisFeatures[~isSetosa]
labels = irisLabels[~isSetosa]
isVirginica = (labels == 2) #label 2 means iris virginica

这里我们非常依赖NumPy对于数组的操作。isSetosa是一个Boolean值数组,我们能够用它来选择出非Setosa的花朵。最后。我们还构造了一个新的Boolean数组,isVirginica。
接下来,我们对每一维度的特征写一个循环小程序。然后看一下哪一个阈值能得到更好的准确率。

# search the threshold between virginica and versicolor
irisFeatures = irisFeatures[~isSetosa]
labels = irisLabels[~isSetosa]
isVirginica = (labels == 2)    #label 2 means iris virginica

bestAccuracy = -1.0
for fi in xrange(irisFeatures.shape[1]):
    thresh = irisFeatures[:,fi].copy()
    thresh.sort()
    for t in thresh:
        pred = (irisFeatures[:,fi] > t)
        acc = (pred == isVirginica).mean()
        if acc > bestAccuracy:
            bestAccuracy = acc;
            bestFeatureIndex = fi;
            bestThreshold = t;

print 'Best Accuracy:\t\t',bestAccuracy
print 'Best Feature Index:\t',bestFeatureIndex
print 'Best Threshold:\t\t',bestThreshold

'''
终于结果:
Best Accuracy:      0.94
Best Feature Index: 3
Best Threshold:     1.6
'''

这里我们首先对每一维度进行排序。然后从该维度中取出任一值作为阈值的一个如果。再计算这个如果的Boolean序列和实际的标签Boolean序列的一致情况。求平均。即得到了准确率。经过全部的循环。终于得到的阈值和所相应的维度。


最后。我们得到了最佳模型针对第四维花瓣的宽度petal width。我们就能够得到这个决策边界decision boundary。

评估模型——交叉检验

上面,我们得到了一个简单的模型。而且针对训练数据实现了94%的正确率,但这个模型參数可能过于优化了。


我们须要的是评估模型针对新数据的泛化能力,所以我们须要保留一部分数据。进行更加严格的评估,而不是用训练数据做測试数据。为此。我们会保留一部分数据进行交叉检验。


这样我们就会得到训练误差和測试误差,当复杂的模型下,可能训练的准确率是100%。可是測试时效果可能仅仅是比随机推測好一点。

交叉检验

在很多实际应用中。数据是不充足的。

为了选择更好的模型。能够採用交叉检验方法。交叉检验的基本想法是反复地使用数据;把给定数据进行切分。将切分的数据集组合为训练集和測试集,在此基础上反复地进行训练、測试以及模型选择。

S-fold交叉检验

应用最多的是S折交叉检验(S-fold cross validation)。方法例如以下:首先随机地将已给数据切分为S个互不相交的大小同样的子集。然后利用S-1个子集的数据训练模型,利用余下的子集測试模型;将这一过程对可能的S种选择反复进行;最后选出S次评測中平均測试误差最小的模型。

如上图,我们将数据集分成5部分,即5-fold交叉检验。接下来,我们能够对每个fold生成一个模型,留出20%的数据进行检验。

leave-one-out交叉检验方法

留一交叉检验(leave-one-out cross validation)是S折交叉检验的特殊情形,是S为给定数据集的容量时情形。


我们能够从训练数据中挑选一个样本,然后拿其它训练数据得到模型,最后看该模型能否将这个挑出来的样本正确的分类。

def learn_model(features,labels):
    bestAccuracy = -1.0
    for fi in xrange(features.shape[1]):
        thresh = features[:,fi].copy()
        thresh.sort()
        for t in thresh:
            pred = (features[:,fi] > t)
            acc = (pred == labels).mean()
            if acc > bestAccuracy:
                bestAccuracy = acc;
                bestFeatureIndex = fi;
                bestThreshold = t;
    '''
    print 'Best Accuracy:\t\t',bestAccuracy
    print 'Best Feature Index:\t',bestFeatureIndex
    print 'Best Threshold:\t\t',bestThreshold
    '''
    return {'dim':bestFeatureIndex, 'thresh':bestThreshold, 'accuracy':bestAccuracy}

def apply_model(features,labels,model):
    prediction = (features[:,model['dim']] > model['thresh'])
    return prediction

#-----------cross validation-------------
error = 0.0
for ei in range(len(irisFeatures)):
    # select all but the one at position 'ei':
    training = np.ones(len(irisFeatures), bool)
    training[ei] = False
    testing = ~training
    model = learn_model(irisFeatures[training], isVirginica[training])
    predictions = apply_model(irisFeatures[testing],
                              isVirginica[testing], model)
    error += np.sum(predictions != isVirginica[testing])

上面的程序,我们用全部的样本对一系列的模型进行了測试,终于的预计说明了模型的泛化能力。

小结

对于上面对数据集进行划分时,我们须要注意平衡分配数据。如果对于一个子集。全部的数据都来自一个类别。则结果没有代表性。
基于以上的讨论,我们利用一个简单的模型来训练。交叉检验过程给出了这个模型泛化能力的预计。

參考文献

Wiki:Iris flower data set
Building Machine Learning Systems with Python

转载请注明作者Jason Ding及其出处
Github主页(http://jasonding1354.github.io/)
CSDN博客(http://blog.csdn.net/jasonding1354)
简书主页(http://www.jianshu.com/users/2bd9b48f6ea8/latest_articles)

posted @ 2016-01-24 12:48  phlsheji  阅读(875)  评论(0编辑  收藏  举报