Skip to content

Commit fc63509

Browse files
optimize code
2 parents 18d4b39 + 5685ef7 commit fc63509

File tree

3 files changed

+46
-3
lines changed

3 files changed

+46
-3
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@
150150
regularizer algorithm including L1, L2, elastic-net
151151

152152
## [metrics.py](metrics.py)
153-
scores including accuracy, precision, recall, f-score, R2 score, confusion matrix, pr curve, roc curve, auc, silhouette coefficient, 2d feature scatter, learning curve
153+
scores including accuracy, precision, recall, f-score, R2 score, confusion matrix, pr curve, roc curve, auc, silhouette coefficient, 2d feature scatter, learning curve, information value
154154

155155
## [optimizer.py](optimizer.py)
156156
optimizer algorithm including following components

metrics.py

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,4 +266,46 @@ def learning_curve(train_X, train_y, train_ratios, test_X, test_y, fit, accuracy
266266

267267
plt.plot(accuracy_train, 'r')
268268
plt.plot(accuracy_test, 'b')
269-
plt.show()
269+
plt.show()
270+
271+
def information_value(X, y):
272+
'''
273+
Parameters
274+
----------
275+
X : shape (data_number, feature_number)
276+
Training data
277+
y : shape (data_number, 1)
278+
Target label
279+
280+
Returns
281+
-------
282+
information values of each feature
283+
'''
284+
feature_number = X.shape[1]
285+
286+
n_positive_total = sum(y == 1)
287+
n_negative_total = sum(y == 0)
288+
289+
ivs = []
290+
for i in range(feature_number):
291+
iv = 0
292+
feature_labels = np.unique(X[:, i])
293+
for feature_label in feature_labels:
294+
indexes = np.flatnonzero(X[:, i] == feature_label)
295+
296+
n_positive = sum(y[indexes] == 1)
297+
n_negative = sum(y[indexes] == 0)
298+
299+
p_positive = n_positive / n_positive_total
300+
p_negative = n_negative / n_negative_total
301+
302+
iv += (p_positive - p_negative) * np.log(p_positive + 1e-8 / p_negative + 1e-8)
303+
304+
ivs.append(iv)
305+
306+
return ivs
307+
308+
309+
310+
311+

softmax_regression.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,4 +79,5 @@ def score(self, X):
7979
y : shape (data_number, class_number)
8080
Predicted score of class per sample.
8181
'''
82-
return scipy.special.softmax(X.dot(self.__W) + self.__b, axis=1)
82+
out = X.dot(self.__W) + self.__b
83+
return scipy.special.softmax(out - np.max(out), axis=1)

0 commit comments

Comments
 (0)