决策树算法及代码

本文介绍了决策树算法的基本原理,包括数据集、属性集的概念,以及如何利用ID3算法进行最优划分属性的选择。通过递归构建决策树,直至属性集为空。文中还提到了在Mac OS环境下使用PyCharm进行代码实现,并给出了代码文件名,以及训练集和分类数据的文件格式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最近重新旁听一门数据挖掘课程,分类算法首先讲到的是决策树算法。
简而言之,决策树即为if-then结构的树。
输入:训练集{(x1,y1),(x2,y2),…,(xn,yn)};
属性集{a1,a2,…,ad}
过程:函数DecesionTree(D,A)
if 数据D均属于同一属性,就将结点标记为对应类叶节点,return;
if A=空集,return
经计算(这里选择ID3算法计算信息增益)选择最优划分属性a*,生成结点;
for a*的每个值a*v,do
生成一个分支,令Dv表示D在a*上取值为a*v的样本子集;
if Dv为空 then
将分支标记为叶节点,return
else
以DecesionTree{Dv,A{a*}}为分支节点(在每次生成分支时,将已使用的属性去除,递归生成子树,直到属性集划分完毕)
end
输出:一棵决策树。
运行环境:Mac OS10.12.3,PyCharm Community Edition
代码内容:
main.py

import DecisionTree
def main():
    # Insert input file
    """
    IMPORTANT: Change this file path to change training data
    """
    file = open('WeatherTraining.csv')
    """
    IMPORTANT: Change this variable too change target attribute
    """
    target = "play"
    data = [[]]
    for line in file:
        line = line.strip("\r\n")
        data.append(line.split(','))
    data.remove([])
    attributes = data[0]
    data.remove(attributes)
    # Run ID3
    tree = DecisionTree.makeTree(data, attributes, target, 0)
    print "generated decision tree"
    # Generate program
    file = open('program.py', 'w')
    file.write("import Node\n\n")
    # open input file
    file.write("data = [[]]\n")
    """
    IMPORTANT: Change this file path to change testing data
    """
    file.write("f = open('Soybean.csv')\n")
    # gather data
    file.write("for line in f:\n\tline = line.strip(\"\\r\\n\")\n\tdata.append(line.split(','))\n")
    file.write("data.remove([])\n")
    # input dictionary tree
    file.write("tree = %s\n" % str(tree))
    file.write("attributes = %s\n" % str(attributes))
    file.write("count = 0\n")
    file.write("for entry in data:\n")
    file.write("\tcount += 1\n")
    # copy dictionary
    file.write("\ttempDict = tree.copy()\n")
    file.write("\tresult = \"\"\n")
    # generate actual tree
    file.write("\twhile(isinstance(tempDict, dict)):\n")
    file.write("\t\troot = Node.Node(tempDict.keys()[0], tempDict[tempDict.keys()[0]])\n")
    file.write("\t\ttempDict = tempDict[tempDict.keys()[0]]\n")
    # this must be attribute
    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值