• 根据某列值进行样本的分类


    根据某列值进行样本的分类

    '''
    dataSet:数据集
    feature:待划分的特征
    value:对应的特征值
    '''
    def binSplitDataSet(dataSet, feature, value):
        #dataSet[:,feature]取出该列特征值
        #dataSet[:,feature] > value将大于value的值筛选出来,得到的是true,false的矩阵
        #然后利用nonzero选择为true的值所在的行,和列
        #取出行,然后在dataset中取出对应的样本
        mat0 = dataSet[nonzero(dataSet[:,feature] > value)[0],:][0]
        mat1 = dataSet[nonzero(dataSet[:,feature] <= value)[0],:][0]
        return mat0,mat1
    >>> testMat=mat(eye(4))
    >>> testMat
    matrix([[ 1.,  0.,  0.,  0.],
            [ 0.,  1.,  0.,  0.],
            [ 0.,  0.,  1.,  0.],
            [ 0.,  0.,  0.,  1.]])
    >>> mat0,mat1=regtree.binSplitDataSet(testMat,1,0.5)
    >>>
    >>>
    >>> mat0
    matrix([[ 0.,  1.,  0.,  0.]])
    >>> mat1
    matrix([[ 1.,  0.,  0.,  0.]])
    >>> b=testMat[:,1]>0.5
    >>> b
    matrix([[False],
            [ True],
            [False],
            [False]], dtype=bool)
    >>> nonzero(b)
    (array([1], dtype=int64), array([0], dtype=int64))
    >>> nonzero(b)[0]
    array([1], dtype=int64)
    >>> testMat[nonzero(b)[0],:]
    matrix([[ 0.,  1.,  0.,  0.]])
    >>> testMat[nonzero(b)[0],:][0]
    matrix([[ 0.,  1.,  0.,  0.]])
    >>>



    欢迎关注我的公众号:小秋的博客 CSDN博客:https://blog.csdn.net/xiaoqiu_cr github:https://github.com/crr121 联系邮箱:rongchen633@gmail.com 有什么问题可以给我留言噢~
  • 相关阅读:
    python基础
    python中自定义的栈
    python内置函数
    python函数之可迭代对象、迭代器的判断
    关系型数据库
    数据库基础知识
    进程间通信--管道
    共享内存应用范例
    Win7秘籍 如何用压缩卷调整不合理分区
    KL-divergence
  • 原文地址:https://www.cnblogs.com/flyingcr/p/10327029.html
Copyright © 2020-2023  润新知