python中针对数据集错误的决策树实现

Posted

技术标签:

【中文标题】python中针对数据集错误的决策树实现【英文标题】:Decision tree implementation in python for dataset error 【发布时间】:2018-04-21 07:31:17 【问题描述】:

我已经为python决策树算法的实现做了如下代码:

from csv import reader

def load_csv(filename):
    file = open(filename, "rb")
    lines = reader(file)
    dataset = list(lines)
    return dataset

# Split a dataset based on an attribute and an attribute value
def test_split(index, value, dataset):
    left, right = list(), list()
    for row in dataset:
        if row[index] < value:
            left.append(row)
        else:
            right.append(row)
    return left, right

# Calculate the Gini index for a split dataset
def gini_index(groups, classes):
    # count all samples at split point
    n_instances = float(sum([len(group) for group in groups]))
    # sum weighted Gini index for each group
    gini = 0.0
    for group in groups:
        size = float(len(group))
        # avoid divide by zero
        if size == 0:
            continue
        score = 0.0
        # score the group based on the score for each class
        for class_val in classes:
            p = [row[-1] for row in group].count(class_val) / size
            score += p * p
        # weight the group score by its relative size
        gini += (1.0 - score) * (size / n_instances)
    return gini

# Select the best split point for a dataset
def get_split(dataset):
    class_values = list(set(row[-1] for row in dataset))
    b_index, b_value, b_score, b_groups = 999, 999, 999, None
    for index in range(len(dataset[0])-1):
        for row in dataset:
            groups = test_split(index, row[index], dataset)
            gini = gini_index(groups, class_values)
            if gini < b_score:
                b_index, b_value, b_score, b_groups = index, row[index], gini, groups
    return 'index':b_index, 'value':b_value, 'groups':b_groups

# Create a terminal node value
def to_terminal(group):
    outcomes = [row[-1] for row in group]
    return max(set(outcomes), key=outcomes.count)

# Create child splits for a node or make terminal
def split(node, max_depth, min_size, depth):
    left, right = node['groups']
    del(node['groups'])
    # check for a no split
    if not left or not right:
        node['left'] = node['right'] = to_terminal(left + right)
        return
    # check for max depth
    if depth >= max_depth:
        node['left'], node['right'] = to_terminal(left), to_terminal(right)
        return
    # process left child
    if len(left) <= min_size:
        node['left'] = to_terminal(left)
    else:
        node['left'] = get_split(left)
        split(node['left'], max_depth, min_size, depth+1)
    # process right child
    if len(right) <= min_size:
        node['right'] = to_terminal(right)
    else:
        node['right'] = get_split(right)
        split(node['right'], max_depth, min_size, depth+1)

# Build a decision tree
def build_tree(train, max_depth, min_size):
    root = get_split(train)
    split(root, max_depth, min_size, 1)
    return root

# Print a decision tree
def print_tree(node, depth=0):
    if isinstance(node, dict):
        print('%s[X%d < %.3f]' % ((depth*' ', (node['index']+1), node['value'])))
        print_tree(node['left'], depth+1)
        print_tree(node['right'], depth+1)
    else:
        print('%s[%s]' % ((depth*' ', node)))

# load and prepare data
filename = 'spine.csv'
dataset = load_csv(filename)

tree = build_tree(dataset, 1, 1)
print_tree(tree)

数据集共包含13个与脊柱相关的属性,通过数据挖掘算法来判断一个人的脊柱是正常还是异常

对于下面给出的数据集spine.csv链接:

https://drive.google.com/file/d/1wubSDVMD2uhJXYJNNbCBHW5Gerp-YDlx/view?usp=sharing

它显示以下错误:

Traceback (most recent call last):
  File "spine.py", line 101, in <module>
    print_tree(tree)
  File "spine.py", line 90, in print_tree
    print('%s[X%d < %.3f]' % ((depth*' ', (node['index']+1), node['value'])))
TypeError: float argument required, not str

【问题讨论】:

【参考方案1】:

node['value'] 是一个刺,而不是一个浮点数。

尝试在格式字符串中使用%s

【讨论】:

以上是关于python中针对数据集错误的决策树实现的主要内容,如果未能解决你的问题,请参考以下文章

实验三:CART分类决策树python实现(两个测试集)|机器学习

实验三:CART分类决策树python实现(两个测试集)|机器学习

实验三:CART回归决策树python实现(两个测试集)|机器学习

Python数据挖掘课程四.决策树DTC数据分析及鸢尾数据集分析

python实现决策树分类

python-sklearn数据拆分与决策树的实现