MCPcopy Create free account
hub / github.com/Jack-Cherish/Machine-Learning / chooseBestFeatureToSplit

Function chooseBestFeatureToSplit

Decision Tree/Decision Tree.py:109–128  ·  view source on GitHub ↗
(dataSet)

Source from the content-addressed store, hash-verified

107 2017-07-20
108"""
109def chooseBestFeatureToSplit(dataSet):
110 numFeatures = len(dataSet[0]) - 1 #特征数量
111 baseEntropy = calcShannonEnt(dataSet) #计算数据集的香农熵
112 bestInfoGain = 0.0 #信息增益
113 bestFeature = -1 #最优特征的索引值
114 for i in range(numFeatures): #遍历所有特征
115 #获取dataSet的第i个所有特征
116 featList = [example[i] for example in dataSet]
117 uniqueVals = set(featList) #创建set集合{},元素不可重复
118 newEntropy = 0.0 #经验条件熵
119 for value in uniqueVals: #计算信息增益
120 subDataSet = splitDataSet(dataSet, i, value) #subDataSet划分后的子集
121 prob = len(subDataSet) / float(len(dataSet)) #计算子集的概率
122 newEntropy += prob * calcShannonEnt(subDataSet) #根据公式计算经验条件熵
123 infoGain = baseEntropy - newEntropy #信息增益
124 # print("第%d个特征的增益为%.3f" % (i, infoGain)) #打印每个特征的信息增益
125 if (infoGain > bestInfoGain): #计算信息增益
126 bestInfoGain = infoGain #更新信息增益,找到最大的信息增益
127 bestFeature = i #记录信息增益最大的特征的索引值
128 return bestFeature #返回信息增益最大的特征的索引值
129
130
131"""

Callers 1

createTreeFunction · 0.85

Calls 2

calcShannonEntFunction · 0.85
splitDataSetFunction · 0.85

Tested by

no test coverage detected