决策树id3、c4.5和cart pyhton代码
时间: 2024-02-04 13:02:38 浏览: 63
决策树是一种常见的机器学习算法,其中ID3、C4.5和CART是三种常见的决策树分类算法。下面是它们的Python代码介绍:
1. ID3算法代码:
```
def ID3(data, labels):
# 计算数据集的信息熵
entropy = calcEntropy(data)
# 如果数据集的信息熵为0,则直接返回该数据集的类别
if entropy == 0:
return data[0][-1]
# 如果特征集为空,则返回数据集中出现次数最多的类别
if len(labels) == 0:
return majorityClass(data)
# 选择最优划分特征
bestFeature = chooseBestFeature(data, labels)
# 根据最优特征生成决策树
decisionTree = {bestFeature: {}}
# 从特征集中删除已经使用的特征
labels.remove(bestFeature)
# 获取最优特征的所有取值
featureValues = set([example[bestFeature] for example in data])
# 遍历最优特征的所有取值,生成子树
for value in featureValues:
subLabels = labels[:]
decisionTree[bestFeature][value] = ID3(splitData(data, bestFeature, value), subLabels)
return decisionTree
```
2. C4.5算法代码:
```
def C45(data, labels):
# 计算数据集的信息熵
entropy = calcEntropy(data)
# 如果数据集的信息熵为0,则直接返回该数据集的类别
if entropy == 0:
return data[0][-1]
# 如果特征集为空,则返回数据集中出现次数最多的类别
if len(labels) == 0:
return majorityClass(data)
# 选择最优划分特征
bestFeature = chooseBestFeature(data, labels)
# 根据最优特征生成决策树
decisionTree = {bestFeature: {}}
# 从特征集中删除已经使用的特征
labels.remove(bestFeature)
# 获取最优特征的所有取值
featureValues = set([example[bestFeature] for example in data])
# 遍历最优特征的所有取值,生成子树
for value in featureValues:
subLabels = labels[:]
# 计算每个取值的信息增益率
subData = splitData(data, bestFeature, value)
ratio = calcRatio(subData, entropy)
# 如果信息增益率高于平均水平,则生成子树
if ratio >= averageRatio(data, bestFeature):
decisionTree[bestFeature][value] = C45(subData, subLabels)
# 否则返回数据集中出现次数最多的类别
else:
decisionTree[bestFeature][value] = majorityClass(subData)
return decisionTree
```
3. CART算法代码:
```
def CART(data, labels):
# 如果数据集中只有一个类别,则直接返回该类别
if len(set([example[-1] for example in data])) == 1:
return data[0][-1]
# 如果特征集为空,则返回数据集中出现次数最多的类别
if len(labels) == 0:
return majorityClass(data)
# 选择最优划分特征
bestFeature, bestValue, bestScore = chooseBestSplit(data)
# 根据最优特征生成决策树
decisionTree = {bestFeature: {}}
# 从特征集中删除已经使用的特征
labels.remove(bestFeature)
# 生成左子树和右子树
leftData = [example for example in data if example[bestFeature] <= bestValue]
rightData = [example for example in data if example[bestFeature] > bestValue]
decisionTree[bestFeature]['left'] = CART(leftData, labels)
decisionTree[bestFeature]['right'] = CART(rightData, labels)
return decisionTree
```
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)