MCPcopy Index your code
hub / github.com/wepe/MachineLearning / _chooseBestFeatureToSplit_C45

Method _chooseBestFeatureToSplit_C45

DecisionTree/id3_c45.py:99–130  ·  view source on GitHub ↗

C4.5 ID3算法计算的是信息增益,C4.5算法计算的是信息增益比,对上面ID3版本的函数稍作修改即可

(self,X,y)

Source from the content-addressed store, hash-verified

97 return bestFeatureIndex
98
99 def _chooseBestFeatureToSplit_C45(self,X,y):
100 """C4.5
101 ID3算法计算的是信息增益,C4.5算法计算的是信息增益比,对上面ID3版本的函数稍作修改即可
102 """
103 numFeatures = X.shape[1]
104 oldEntropy = self._calcEntropy(y)
105 bestGainRatio = 0.0
106 bestFeatureIndex = -1
107 #对每个特征都计算一下gainRatio=infoGain/splitInformation
108 for i in range(numFeatures):
109 featList = X[:,i]
110 uniqueVals = set(featList)
111 newEntropy = 0.0
112 splitInformation = 0.0
113 #对第i个特征的各个value,得到各个子数据集,计算各个子数据集的熵,
114 #进一步地可以计算得到根据第i个特征分割原始数据集后的熵newEntropy
115 for value in uniqueVals:
116 sub_X,sub_y = self._splitDataSet(X,y,i,value)
117 prob = len(sub_y)/float(len(y))
118 newEntropy += prob * self._calcEntropy(sub_y)
119 splitInformation -= prob * np.log2(prob)
120 #计算信息增益比,根据信息增益比选择最佳分割特征
121 #splitInformation若为0,说明该特征的所有值都是相同的,显然不能作为分割特征
122 if splitInformation==0.0:
123 pass
124 else:
125 infoGain = oldEntropy - newEntropy
126 gainRatio = infoGain/splitInformation
127 if(gainRatio > bestGainRatio):
128 bestGainRatio = gainRatio
129 bestFeatureIndex = i
130 return bestFeatureIndex
131
132
133

Callers 1

_createTreeMethod · 0.95

Calls 2

_calcEntropyMethod · 0.95
_splitDataSetMethod · 0.95

Tested by

no test coverage detected