美文网首页机器学习机器学习机器学习与模式识别
【scikit-learn】学习Python来分类现实世界的数据

【scikit-learn】学习Python来分类现实世界的数据

作者: JasonDing | 来源:发表于2014-12-25 11:01 被阅读679次

    引入

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

    Iris数据集

    Iris flower数据集是1936年由Sir Ronald Fisher引入的经典多维数据集,可以作为判别分析(discriminant analysis)的样本。该数据集包含Iris花的三个品种(Iris setosa, Iris virginica and Iris versicolor)各50个样本,每个样本还有4个特征参数(分别是萼片<sepals>的长宽和花瓣<petals>的长宽,以厘米为单位),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花朵,否则就是其他两种花朵。
    这个模型的结构非常简单,是由数据的一个维度阈值来确定的。我们通过实验确定这个维度的最佳阈值。
    以上的例子将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)

    相关文章

      网友评论

      本文标题:【scikit-learn】学习Python来分类现实世界的数据

      本文链接:https://www.haomeiwen.com/subject/bsuatttx.html