Python实现DescionTree决策树 --- 划分数据集

版权声明:学习交流为主,未经博主同意禁止转载,禁止用于商用。 https://blog.csdn.net/u012965373/article/details/83831708
 """
    划分数据集
    [[1, 1, ‘yes’], [1, 1, ‘yes’], [1, 0, ‘no’], [0, 1, ‘no’], [0, 1, ‘no’]]
    这个是我们的数据集。 如果我们选取第一个特征值也就是需不需要浮到水面上才能生存来划分我们的数据,这里生物有两种可能,1就是需要,0就是不需要。那么第一个特征的取值就是两种。
    如果我们按照第一个特征的第一个可能的取值来划分数据也就是当所有的样本的第一列取1的时候满足的样本,那就是如下三个:
    [1, 1, ‘yes’], [1, 1, ‘yes’], [1, 0, ‘no’]
    可以理解为这个特征为一条分界线,我们选取完这个特征之后这个特征就要从我们数据集中剔除,因为要把他理解
    """
    def split_data_set(self, data_set, index, value):
        ret_data_set = []
        for feat_vec in data_set:
            if feat_vec[index] == value:
                reduced_feat_vec = feat_vec[:index]
                reduced_feat_vec.extend(feat_vec[index + 1:])
                ret_data_set.append(reduced_feat_vec)
        return ret_data_set
    # 划分数据集 此处的函数式写法是split_data_set同样的方式
    def split_data_set1(self, data_set, index, value):
        ret_data_set = [data[:index] + data[index + 1:] for data in data_set for i, v in enumerate(data) if i == index and v == value]
        return ret_data_set

猜你喜欢

转载自blog.csdn.net/u012965373/article/details/83831708