MCPcopy Index your code
hub / github.com/clips/pattern / information_gain

Method information_gain

pattern/vector/__init__.py:1134–1174  ·  view source on GitHub ↗

Returns the information gain (IG) for the given feature, by examining how much it contributes to each document type (class). High IG means low entropy (or predictability) = interesting for feature selection.

(self, word)

Source from the content-addressed store, hash-verified

1132 reduce = latent_semantic_analysis
1133
1134 def information_gain(self, word):
1135 """ Returns the information gain (IG) for the given feature,
1136 by examining how much it contributes to each document type (class).
1137 High IG means low entropy (or predictability) = interesting for feature selection.
1138 """
1139 if not self._ig:
1140 # Based on Vincent Van Asch, http://www.clips.ua.ac.be/~vincent/scripts/textgain.py
1141 # For classes {xi...xn} and features {yi...yn}:
1142 # IG(X,Y) = H(X) - H(X|Y)
1143 # H(X) = -sum(p(x) * log2(x) for x in X)
1144 # H(X|Y) = sum(p(y) * H(X|Y=y) for y in Y)
1145 # H(X|Y=y) = -sum(p(x) * log2(x) for x in X if y in x)
1146 # H is the entropy for a list of probabilities.
1147 # Lower entropy indicates predictability, i.e., some values are more probable.
1148 # H([0.50,0.50]) = 1.00
1149 # H([0.75,0.25]) = 0.81
1150 H = entropy
1151 # X = document type (class) distribution.
1152 # "How many documents have class xi?"
1153 X = dict.fromkeys(self.classes, 0)
1154 for d in self.documents:
1155 X[d.type] += 1
1156 # Y = document feature distribution.
1157 # "How many documents have feature yi?"
1158 Y = dict.fromkeys(self.features, 0)
1159 for d in self.documents:
1160 for y, v in d.vector.items():
1161 if v > 0:
1162 Y[y] += 1 # Discrete: feature is present (1) or not (0).
1163 Y = dict((y, Y[y] / float(len(self.documents))) for y in Y)
1164 # XY = features by class distribution.
1165 # "How many documents of class xi have feature yi?"
1166 XY = dict.fromkeys(self.features, {})
1167 for d in self.documents:
1168 for y, v in d.vector.items():
1169 if v != 0:
1170 XY[y][d.type] = XY[y].get(d.type, 0) + 1
1171 # IG.
1172 for y in self.features:
1173 self._ig[y] = H(X.values()) - Y[y] * H(XY[y].values())
1174 return self._ig[word]
1175
1176 IG = ig = infogain = gain = information_gain
1177

Callers 2

feature_selectionMethod · 0.95
test_information_gainMethod · 0.95

Calls 6

lenFunction · 0.85
HFunction · 0.85
fromkeysMethod · 0.45
itemsMethod · 0.45
getMethod · 0.45
valuesMethod · 0.45

Tested by 1

test_information_gainMethod · 0.76