Skip to content

Commit

Permalink
more PEP8
Browse files Browse the repository at this point in the history
  • Loading branch information
ogrisel committed Oct 30, 2010
1 parent 297ccc4 commit decbabe
Showing 1 changed file with 18 additions and 13 deletions.
31 changes: 18 additions & 13 deletions scikits/learn/metrics.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
"""Utilities to evaluate the predictive performance of models"""

# Authors: Alexandre Gramfort <[email protected]>
# Mathieu Blondel <[email protected]>
# License: BSD Style.

import numpy as np


def confusion_matrix(y, y_):
"""Compute confusion matrix to evaluate the accuracy of a classification
Expand Down Expand Up @@ -38,10 +41,10 @@ def confusion_matrix(y, y_):
labels = np.unique(labels)
n_labels = labels.size

cm = np.empty((n_labels,n_labels))
cm = np.empty((n_labels, n_labels))
for i, label_i in enumerate(labels):
for j, label_j in enumerate(labels):
cm[i,j] = np.sum(np.logical_and(y==label_i, y_==label_j))
cm[i, j] = np.sum(np.logical_and(y == label_i, y_ == label_j))

return cm

Expand Down Expand Up @@ -77,13 +80,15 @@ def roc_curve(y, probas_):
probas_ = probas_.ravel()
thresholds = np.sort(np.unique(probas_))[::-1]
n_thresholds = thresholds.size

tpr = np.empty(n_thresholds) # True positive rate
fpr = np.empty(n_thresholds) # False positive rate
n_pos = float(np.sum(y==1)) # nb of true positive
n_neg = float(np.sum(y==0)) # nb of true negative
n_pos = float(np.sum(y == 1)) # nb of true positive
n_neg = float(np.sum(y == 0)) # nb of true negative

for i, t in enumerate(thresholds):
tpr[i] = np.sum(y[probas_>=t]==1) / n_pos
fpr[i] = np.sum(y[probas_>=t]==0) / n_neg
tpr[i] = np.sum(y[probas_ >= t] == 1) / n_pos
fpr[i] = np.sum(y[probas_ >= t] == 0) / n_neg

return fpr, tpr, thresholds

Expand Down Expand Up @@ -134,8 +139,8 @@ def precision(y_true, y_pred):
=======
precision : float
"""
true_pos = np.sum(y_true[y_pred == 1]==1)
false_pos = np.sum(y_true[y_pred == 1]==0)
true_pos = np.sum(y_true[y_pred == 1] == 1)
false_pos = np.sum(y_true[y_pred == 1] == 0)
return true_pos / float(true_pos + false_pos)


Expand All @@ -160,8 +165,8 @@ def recall(y_true, y_pred):
=======
recall : float
"""
true_pos = np.sum(y_true[y_pred == 1]==1)
false_neg = np.sum(y_true[y_pred == 0]==1)
true_pos = np.sum(y_true[y_pred == 1] == 1)
false_neg = np.sum(y_true[y_pred == 0] == 1)
return true_pos / float(true_pos + false_neg)


Expand Down Expand Up @@ -194,9 +199,9 @@ def precision_recall(y_true, y_pred):
==========
http:https://en.wikipedia.org/wiki/Precision_and_recall
"""
true_pos = np.sum(y_true[y_pred == 1]==1)
false_pos = np.sum(y_true[y_pred == 1]==0)
false_neg = np.sum(y_true[y_pred == 0]==1)
true_pos = np.sum(y_true[y_pred == 1] == 1)
false_pos = np.sum(y_true[y_pred == 1] == 0)
false_neg = np.sum(y_true[y_pred == 0] == 1)
precision = true_pos / float(true_pos + false_pos)
recall = true_pos / float(true_pos + false_neg)
return precision, recall
Expand Down

0 comments on commit decbabe

Please sign in to comment.