Python实现决策树算法 C4.5和ID3算法

该博客介绍了如何用Python语言实现C4.5和ID3决策树算法,提供了源代码并详细说明了切换算法的方法。用户可以通过修改函数熵的返回值来选择使用C4.5或ID3。示例运行命令为:`python c45.py data.txt`,其中data.txt是数据文件路径。
摘要由CSDN通过智能技术生成

本文以python语言实现了C4.5和ID3算法,默认为C4.5算法,若要使用ID3算法,将函数 entropy()最后的返回值改变一下即可,即注释掉C4.5那行代码,启用ID3那行代码即可。

将源代码保存为python文件,命名为c45.py,最后一个参数为数据的路径,可自由设置,参考以下运行方式:

python c45.py data.txt

特别感谢:

点击打开链接

源代码如下:

#!/usr/bin/python
# -*- coding: UTF-8 -*-
__author__ = 'Administrator'
########      C4.5  ID3  finished!!        ######
#################  (tm_year=2016, tm_mon=3, tm_mday=15, tm_hour=22, tm_min=56, tm_sec=56, tm_wday=1, tm_yday=75, tm_isdst=0)  ################
import re
import math
import sys

mini_size = 1 #### the minimum size of the nodes, the nodes will not be splited in the next though it is not fully just one type
DataLength = 100  ### the length of data items
used = [0 for i in range(DataLength)] ### attribute used or not
ended = [0 for i in range(DataLength)]  #### if the nodes will be splited in the next
tp = [-1 for i in range(DataLength)]  #### 1 - yes, 0 - no

class node:
    def __init__(self):
        self.value = ''
        self.father =
  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值