资源简介
资源中包含完整的C4.5决策树算法Python代码和测试数据,其中有4个文件:C45.py是算法的实现代码,treePlotter.py是绘制决策树代码,PlayData.txt是样本数据,C45test.py用来构建、绘制并测试决策树,您可以运行该文件来依次进行决策树的构建、剪枝、绘制树型图,并对测试样本进行分类。

代码片段和文件信息
# -*- coding: cp936 -*-
from math import log
import operator
import os
import re
from numpy import inf
import copy
# 计算信息熵
def calcShannonEnt(dataSet labelIndex):
# type: (list) -> float
numEntries = 0 # 样本数(按权重计算)
labelCounts = {}
for featVec in dataSet: # 遍历每个样本
if featVec[labelIndex] != ‘N‘:
weight = float(featVec[-2])
numEntries += weight
currentLabel = featVec[-1] # 当前样本的类别
if currentLabel not in labelCounts.keys(): # 生成类别字典
labelCounts[currentLabel] = 0
labelCounts[currentLabel] += weight # 数据集的倒数第二个值用来标记样本权重
shannonEnt = 0.0
for key in labelCounts: # 计算信息熵
prob = float(labelCounts[key]) / numEntries
shannonEnt = shannonEnt - prob * log(prob 2)
return shannonEnt
def splitDataSet(dataSet axis value LorR=‘N‘):
“““
type: (list int string or float string) -> list
划分数据集
axis:按第几个特征划分
value:划分特征的值
LorR: N 离散属性; L 小于等于value值; R 大于value值
“““
retDataSet = []
featVec = []
if LorR == ‘N‘: # 离散属性
for featVec in dataSet:
if featVec[axis] == value:
reducedFeatVec = featVec[:axis]
reducedFeatVec.extend(featVec[axis + 1:])
retDataSet.append(reducedFeatVec)
elif LorR == ‘L‘:
for featVec in dataSet:
if featVec[axis] != ‘N‘:
if float(featVec[axis]) < value:
retDataSet.append(featVec)
elif LorR == ‘R‘:
for featVec in dataSet:
if featVec[axis] != ‘N‘:
if float(featVec[axis]) > value:
retDataSet.append(featVec)
return retDataSet
def splitDataSetWithNull(dataSet axis value LorR=‘N‘):
“““
type: (list int string or float string) -> list
划分数据集
axis:按第几个特征划分
value:划分特征的值
LorR: N 离散属性; L 小于等于value值; R 大于value值
“““
retDataSet = []
nullDataSet = []
featVec = []
totalWeightV = calcTotalWeight(dataSet axis False) # 非空样本权重
totalWeightSub = 0.0
if LorR == ‘N‘: # 离散属性
for featVec in dataSet:
if featVec[axis] == value:
reducedFeatVec = featVec[:axis]
reducedFeatVec.extend(featVec[axis + 1:])
retDataSet.append(reducedFeatVec)
elif featVec[axis] == ‘N‘:
reducedNullVec = featVec[:axis]
reducedNullVec.extend(featVec[axis + 1:])
nullDataSet.append(reducedNullVec)
elif LorR == ‘L‘:
for featVec in dataSet:
if featVec[axis] != ‘N‘:
if float(featVec[axis]) < value:
retDataSet.append(featVec)
elif featVec[axis] == ‘N‘:
nullDataSet.append(featVec)
elif LorR == ‘R‘:
for featVec in dataSet:
if featVec[axis] != ‘N‘:
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 23623 2018-12-09 22:57 C4.5决策树\C45.py
文件 1186 2018-12-09 23:31 C4.5决策树\C45test.py
文件 393 2018-12-04 22:19 C4.5决策树\PlayData.txt
文件 5610 2018-12-04 22:48 C4.5决策树\treePlotter.py
目录 0 2018-12-09 23:31 C4.5决策树
----------- --------- ---------- ----- ----
30812 5
- 上一篇:python音乐播放+滤波器
- 下一篇:spider_LOL.py
相关资源
- python+ selenium教程
- PycURL(Windows7/Win32)Python2.7安装包 P
- 英文原版-Scientific Computing with Python
- 7.图像风格迁移 基于深度学习 pyt
- 基于Python的学生管理系统
- A Byte of Python(简明Python教程)(第
- Python实例174946
- Python 人脸识别
- Python 人事管理系统
- 基于python-flask的个人博客系统
- 计算机视觉应用开发流程
- python 调用sftp断点续传文件
- python socket游戏
- 基于Python爬虫爬取天气预报信息
- python函数编程和讲解
- Python开发的个人博客
- 基于python的三层神经网络模型搭建
- python实现自动操作windows应用
- python人脸识别(opencv)
- python 绘图(方形、线条、圆形)
- python疫情卡UN管控
- python 连连看小游戏源码
- 基于PyQt5的视频播放器设计
- 一个简单的python爬虫
- csv文件行列转换python实现代码
- Python操作Mysql教程手册
- Python Machine Learning Case Studies
- python获取硬件信息
- 量化交易(附python常见函数的使用方
- python 名字用字排行
评论
共有 条评论