from math import log
import operator
import csv
def readDataset(filename):
'''
读取数据
:param filename: 数据文件名,CSV格式
:return: 以列表形式返回数据列表和特征列表
'''
with open(filename) as f:
reader = csv.reader(f)
header_row = next(reader)
labels = header_row[1:7]
dataset = []
for line in reader:
tempVect = line[1:7]
tempVect.append(line[7])
dataset.append(tempVect)
return dataset, labels
def infoEnt(dataset):
'''
计算信息熵
:param dataset: 输入数据集
:return: 返回信息熵
'''
numdata = len(dataset)
labels = {}
for featVec in dataset:
label = featVec[-1]
if label not in labels.keys():
labels[label] = 0
labels[label] += 1
infoEnt = 0
for lab in labels.keys():
prop = float(labels[lab
西瓜书决策树ID3算法实现,离散属性
最新推荐文章于 2024-06-08 21:04:55 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)