def shannonEnt(dataSet): len_dataSet = len(dataSet) # 得到数据集的行数 labelCounts = {} # 创建一个字典,用于计算,每个属性值出现的次数 shannonEnt = 0.0 # 令香农熵初始值为0 for element in dataSet: # 对每一条数据进行逐条分析 currentLabel = element[-1] # 提取属性值信息 if currentLabel not in labelCounts.keys(): # 以属性名作为labelCounts这个字典的key labelCounts[currentLabel] = 0 # 设定字典的初始value为0 labelCounts[currentLabel] += 1 # value值逐渐加一,达到统计标签出现次数的作用 for key in labelCounts: # 遍历字典的key proportion = float(labelCounts[key])/len_dataSet shannonEnt -= proportion*log(proportion, 2) # 根据公式得到香农熵 # print('属性值出现的次数结果:{}'.format(labelCounts)) # print('活动的信息熵为:{}'.format(shannonEnt)) return shannonEnt def splitDataSet(dataSet, i, value): splitDataSet = [] # 创建一个列表,用于存放 划分后的数据集 for example in dataSet: # 遍历给定的数据集 if example[i] == value: splitExample = example[:i] splitExample.extend(example[i+1:]) splitDataSet.append(splitExample) # 去掉i属性这一列,生成新的数据集,即划分的数据集 return splitDataSet # 得到划分的数据集。详细解释这段代码
时间: 2023-07-15 21:11:53 浏览: 52
这段代码实现了决策树算法中的信息增益计算和数据集划分。其中,函数shannonEnt(dataSet)计算给定数据集的香农熵,即数据集的不确定性;函数splitDataSet(dataSet, i, value)根据给定的属性i和属性值value将数据集划分成子集。
具体解释如下:
- 在shannonEnt(dataSet)函数中,首先统计数据集中每个类别标签(即最后一列)出现的次数,将其存储在字典labelCounts中。接着,对于每个类别标签,计算其在数据集中出现的频率proportion,并根据香农熵的公式计算其贡献:proportion*log(proportion, 2)。最后,将所有类别标签的贡献相加并取相反数,得到数据集的香农熵。
- 在splitDataSet(dataSet, i, value)函数中,遍历给定的数据集,对于每一条数据example,如果其第i个属性值等于给定的value,就将其去掉第i个属性,生成新的数据集splitDataSet。最后返回划分后的数据集。
这两个函数是决策树算法中的基础,用于计算信息增益和进行数据集划分。
相关问题
已知# 计算信息熵 def cal_entropy(dataset): numEntries = len(dataset) labelCounts = {} # 给所有可能分类创建字典 for featVec in dataset: currentlabel = featVec[-1] if currentlabel not in labelCounts.keys(): labelCounts[currentlabel] = 0 labelCounts[currentlabel] += 1 Ent = 0.0 # *** START CODE HERE *** for key in labelCounts: prob = float(labelCounts[key])/numEntries Ent -= prob*log(prob,2) # 计算信息熵Ent # *** END CODE HERE *** return Ent # 划分数据集 def splitdataset(dataset, axis, value): retdataset = [] # 创建返回的数据集列表 for featVec in dataset: # 抽取符合划分特征的值 if featVec[axis] == value: reducedfeatVec = featVec[:axis] # 去掉axis特征 reducedfeatVec.extend(featVec[axis + 1:]) # 将符合条件的特征添加到返回的数据集列表 retdataset.append(reducedfeatVec) return retdataset,请补全C4.5算法def C45_chooseBestFeatureToSplit(dataset): numFeatures = len(dataset[0]) - 1 baseEnt = cal_entropy(dataset) bestInfoGain_ratio = 0.0 bestFeature = -1 # *** START CODE HERE *** # 利用C4.5的原理以及仿照ID3_chooseBestFeatureToSplit代码进行编码 # *** END CODE HERE *** return bestFeature
在C4.5算法中,需要对ID3算法进行改进,即考虑到信息增益比。C4.5算法选择最优划分特征的过程如下:
1. 计算每个特征的信息增益比。
2. 选择信息增益比最大的特征作为最优划分特征。
3. 根据最优特征的取值将数据集划分成多个子集。
下面是C4.5算法的代码实现:
import math
# 计算信息熵
def cal_entropy(dataset):
numEntries = len(dataset)
labelCounts = {}
# 给所有可能分类创建字典
for featVec in dataset:
currentlabel = featVec[-1]
if currentlabel not in labelCounts.keys():
labelCounts[currentlabel] = 0
labelCounts[currentlabel] += 1
Ent = 0.0
for key in labelCounts:
prob = float(labelCounts[key])/numEntries
Ent -= prob*math.log(prob,2) # 计算信息熵Ent
return Ent
# 划分数据集
def splitdataset(dataset, axis, value):
retdataset = [] # 创建返回的数据集列表
for featVec in dataset: # 抽取符合划分特征的值
if featVec[axis] == value:
reducedfeatVec = featVec[:axis] # 去掉axis特征
reducedfeatVec.extend(featVec[axis + 1:]) # 将符合条件的特征添加到返回的数据集列表
retdataset.append(reducedfeatVec)
return retdataset
# 计算信息增益比
def cal_infoGain_ratio(dataset, baseEnt, axis):
# 计算特征的取值数目
numEntries = len(dataset)
featList = [example[axis] for example in dataset]
uniqueVals = set(featList)
# 计算条件熵
newEnt = 0.0
splitInfo = 0.0
for value in uniqueVals:
subdataset = splitdataset(dataset, axis, value)
prob = len(subdataset) / float(numEntries)
newEnt += prob * cal_entropy(subdataset)
splitInfo -= prob * math.log(prob, 2)
# 计算信息增益比
infoGain = baseEnt - newEnt
if splitInfo == 0:
return 0
infoGain_ratio = infoGain / splitInfo
return infoGain_ratio
# C4.5算法选择最优划分特征
def C45_chooseBestFeatureToSplit(dataset):
numFeatures = len(dataset[0]) - 1
baseEnt = cal_entropy(dataset)
bestInfoGain_ratio = 0.0
bestFeature = -1
for i in range(numFeatures):
infoGain_ratio = cal_infoGain_ratio(dataset, baseEnt, i)
if infoGain_ratio > bestInfoGain_ratio:
bestInfoGain_ratio = infoGain_ratio
bestFeature = i
return bestFeature
翻译成Java代码 def calcShannonEnt(dataSet): #返回数据集行数 numEntries=len(dataSet) #保存每个标签(label)出现次数的字典 labelCounts={} #对每组特征向量进行统计 for featVec in dataSet: currentLabel=featVec[-1] #提取标签信息 if currentLabel not in labelCounts.keys(): #如果标签没有放入统计次数的字典,添加进去 labelCounts[currentLabel]=0 labelCounts[currentLabel]+=1 #label计数 shannonEnt=0.0 #经验熵 #计算经验熵 for key in labelCounts: prob=float(labelCounts[key])/numEntries #选择该标签的概率 shannonEnt-=prob*log(prob,2) #利用公式计算 return shannonEnt #返回经验熵
以下是翻译成Java代码的结果:
```
public static double calcShannonEnt(ArrayList<ArrayList<String>> dataSet) {
// 返回数据集行数
int numEntries = dataSet.size();
// 保存每个标签(label)出现次数的字典
Map<String, Integer> labelCounts = new HashMap<String, Integer>();
// 对每组特征向量进行统计
for (ArrayList<String> featVec : dataSet) {
String currentLabel = featVec.get(featVec.size() - 1); // 提取标签信息
if (!labelCounts.containsKey(currentLabel)) { // 如果标签没有放入统计次数的字典,添加进去
labelCounts.put(currentLabel, 0);
}
labelCounts.put(currentLabel, labelCounts.get(currentLabel) + 1); // label计数
}
double shannonEnt = 0.0; // 经验熵
// 计算经验熵
for (String key : labelCounts.keySet()) {
double prob = (double) labelCounts.get(key) / numEntries; // 选择该标签的概率
shannonEnt -= prob * log(prob, 2); // 利用公式计算
}
return shannonEnt; // 返回经验熵
}
private static double log(double value, int base) {
return Math.log(value) / Math.log(base);
}
```
这是一个计算数据集经验熵的函数,输入参数为一个包含数据的二维ArrayList对象。函数首先计算数据集中不同标签出现的次数,然后根据公式计算经验熵并返回结果。
相关推荐
![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)