数据挖掘实验——python实现决策树(ID3算法)

实验内容:

使用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
  • 6
    点赞
  • 76
    收藏
    觉得还不错? 一键收藏
  • 8
    评论
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值