实验内容:
使用ID3算法设计实现决策树,使用uci数据集中的Caesarian Section Classification Dataset Data Set数据进行分类
获取数据集:https://archive.ics.uci.edu/ml/datasets/Caesarian+Section+Classification+Dataset
数据集:
@attribute ‘Age’ { 22,26,28,27,32,36,33,23,20,29,25,37,24,18,30,40,31,19,21,35,17,38 }
@attribute ‘Delivery number’ { 1,2,3,4 }
@attribute ‘Delivery time’ { 0,1,2 }
@attribute ‘Blood of Pressure’ { 2,1,0 }
@attribute ‘Heart Problem’ { 1,0 }
@attribute Caesarian { 0,1 }
代码实现:
import pandas as pd
# 加载数据集
# @attribute 'Age' {
22,26,28,27,32,36,33,23,20,29,25,37,24,18,30,40,31,19,21,35,17,38 }
# @attribute 'Delivery number' {
1,2,3,4 }
# @attribute 'Delivery time' {
0,1,2 }
# @attribute 'Blood of Pressure' {
2,1,0 }
# @attribute 'Heart Problem' {
1,0 }
# @attribute Caesarian {
0,1 }
data= pd.read_csv('D:/Desktop/caesarian.csv.arff',header=None)
print(data.head())
import math
import operator
def get_data():
# 数据集
x = data[[0,1,2,3,4,5]].values.tolist()
# 分类属性
labels = ['年龄','数量','时间','血压','心脏病']
return x,labels
def maxCount(classList):
Count={
}
#统计classList中每个元素出现的次数
for i in classList:
if i not in classCount.keys():
Count[i]=0
Count[i]+=1
#根据字典的值降序排列
sorted_Count=sorted(Count.items(),key=operator.itemgetter(1),reverse=True)
return sorted_Count[0][0]
def Entropy(x):
D=len(x)
#保存每个标签(label)出现次数的字典
labelCounts={
}
#对每组特征向量进行统计
for m in x:
Label=m[-1] #提取标签
if Label not in labelCounts.keys(): #标签没有被统计就添加
labelCounts[Label]=0
labelCounts[Label