Tutorials/2_Models/DecisionTree/python/decision_tree.py
def _chooseBestFeatureTosplit_C45(self, X, y):
        numFeatures = X.shape[1]
        oldEntropy = self._calculateEntropy(y)
        bestInfoGain = 0.0
        bestFeatureIndex = -1
        """compute InfoGain of every feature , then choose the best one"""
        for feature in range(numFeatures):
            featureList = X[:, feature]
            uniqueValue = set(featureList)
            newEntropy = 0.0
            splitInformation = 0.0
            for value in uniqueValue:
                sub_X, sub_y = self._splitDataset(X, y, feature, value)
                pro = len(sub_y)/float(len(y))
                newEntropy += pro * self._calculateEntropy(sub_y)
                splitInformation -= pro * np.log2(pro)
            if(splitInformation == 0.0):
                pass
            else:
                InfoGain = oldEntropy - newEntropy
                GainRatio = InfoGain/splitInformation
                if(GainRatio > bestInfoGain):
                    bestInfoGain = GainRatio
                    bestFeatureIndex = feature
        return bestFeatureIndex
compute InfoGain of every feature , then choose the best one
compute InfoGain of every feature , then choose the best one
compute InfoGain of every feature then choose the best one
def _chooseBestFeatureTosplit_C45(self, X, y):
        numFeatures = X.shape[1]
        oldEntropy = self._calculateEntropy(y)
        bestInfoGain = 0.0
        bestFeatureIndex = -1
        for feature in range(numFeatures):
            featureList = X[:, feature]
            uniqueValue = set(featureList)
            newEntropy = 0.0
            splitInformation = 0.0
            for value in uniqueValue:
                sub_X, sub_y = self._splitDataset(X, y, feature, value)
                pro = len(sub_y)/float(len(y))
                newEntropy += pro * self._calculateEntropy(sub_y)
                splitInformation -= pro * np.log2(pro)
            if(splitInformation == 0.0):
                pass
            else:
                InfoGain = oldEntropy - newEntropy
                GainRatio = InfoGain/splitInformation
                if(GainRatio > bestInfoGain):
                    bestInfoGain = GainRatio
                    bestFeatureIndex = feature
        return bestFeatureIndex
def _chooseBestFeatureTosplit_C45 ( self , X , y ) : numFeatures = X . shape [ 1 ] oldEntropy = self . _calculateEntropy ( y ) bestInfoGain = 0.0 bestFeatureIndex = - 1 for feature in range ( numFeatures ) : featureList = X [ : , feature ] uniqueValue = set ( featureList ) newEntropy = 0.0 splitInformation = 0.0 for value in uniqueValue : sub_X , sub_y = self . _splitDataset ( X , y , feature , value ) pro = len ( sub_y ) / float ( len ( y ) ) newEntropy += pro * self . _calculateEntropy ( sub_y ) splitInformation -= pro * np . log2 ( pro ) if ( splitInformation == 0.0 ) : pass else : InfoGain = oldEntropy - newEntropy GainRatio = InfoGain / splitInformation if ( GainRatio > bestInfoGain ) : bestInfoGain = GainRatio bestFeatureIndex = feature return bestFeatureIndex
compute InfoGain of every feature , then choose the best one
compute InfoGain of every feature then choose the best one
"""compute InfoGain of every feature , then choose the best one"""
self
X
y