Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/293.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 生成具有相同类的终端叶的决策树_Python_Scikit Learn_Decision Tree - Fatal编程技术网

Python 生成具有相同类的终端叶的决策树

Python 生成具有相同类的终端叶的决策树,python,scikit-learn,decision-tree,Python,Scikit Learn,Decision Tree,我对决策树比较陌生,我一直坚持我的决策树算法。我使用交叉验证和参数调整来优化分类,如下示例:。但不管我如何调整参数,我总是得到如下结果(这里只是一个小树的示例): 我不明白这种行为的原因。为什么树会生成具有相同类(这里是class2)的叶子?为什么它不在一个之后停止呢?您所指的类属性是该特定节点上的多数类,并且颜色来自您传递到的filled=True参数 现在,看看您的数据集,您有147个class1样本和525个class2样本,这是一个相当不平衡的比率。碰巧的是,在这个深度上,特定数据集的

我对决策树比较陌生,我一直坚持我的决策树算法。我使用交叉验证和参数调整来优化分类,如下示例:。但不管我如何调整参数,我总是得到如下结果(这里只是一个小树的示例):


我不明白这种行为的原因。为什么树会生成具有相同类(这里是class2)的叶子?为什么它不在一个之后停止呢?您所指的
属性是该特定节点上的多数类,并且颜色来自您传递到的
filled=True
参数

现在,看看您的数据集,您有147个class1样本和525个class2样本,这是一个相当不平衡的比率。碰巧的是,在这个深度上,特定数据集的最佳分割会产生分割,其中大多数类是class2。这是正常的行为,是数据的产物,考虑到class2的数量比class1的数量多3:1,这并不奇怪

至于为什么当多数类对于拆分的两个子类相同时,树不会停止,这是因为算法的工作方式。如果没有“最大深度”(max depth)而保持无界,则它将继续,直到只生成独占包含单个类的纯叶节点为止(其中的值为0)。您在示例中设置了
max_depth=2
,因此树在生成所有纯节点之前就停止了

您会注意到,在示例中用红色框住的拆分中,右侧的节点几乎是100%的class2,有54个class2实例,只有2个class1实例。如果算法在此之前停止,它将生成上面的节点,即291-45 class2-class1,这远没有那么有用

也许您可以增加树的最大深度,看看是否可以进一步分离类

     def load_csv(filename):
           dataset = list()
           with open(filename, 'r') as file:
               csv_reader = reader(file)
               for row in csv_reader:
                   if not row:
                       continue
                   dataset.append(row)
           return dataset

    # Convert string column to float
    def str_column_to_float(dataset, column):
        for row in dataset:
            row[column] = float(row[column].strip())


    # Load dataset
    filename = 'C:/Test.csv'
    dataset = load_csv(filename)


    # convert string columns to float
    for i in range(len(dataset[0])):
        str_column_to_float(dataset, i)

    # Transform to x and y
    x = []
    xpart = []
    y = []
    for row in dataset:
        for i in range(len(row)):
            if i != (len(row) - 1):
                xpart.append(row[i])
            else:
                y.append(row[i])
        x.append(xpart)
        xpart = []

    features_names = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']
    labels = ['class1', 'class2']

    #here I tried to tune the parameters 
    #(I changed them several times, this is just an example to show, how the code looks like). 
    # However, I always ended up with terminal leaves with same classes
    """dtree=DecisionTreeClassifier(class_weight=None, criterion='entropy', max_depth=5,
        max_features=8, max_leaf_nodes=None, min_impurity_decrease = 0.0, min_impurity_split = None, min_samples_leaf=1,
        min_samples_split=2, min_weight_fraction_leaf=0.0,
        presort=False, random_state=None, splitter='random')"""

    #here, I created the small example
    dtree = DecisionTreeClassifier(max_depth=2)
    dtree.fit(x,y)

    dot_data = tree.export_graphviz(dtree, out_file=None) 
    graph = graphviz.Source(dot_data) 
    graph.render("Result") 

    dot_data = tree.export_graphviz(dtree, out_file=None, 
                     feature_names= features_names,  
                     class_names=labels,  
                     filled=True, rounded=True,  
                     special_characters=True)  
    graph = graphviz.Source(dot_data)  
    graph.format = 'png'
    graph.render('Result', view = True)