Skip to content

Commit

Permalink
Code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
PrimozGodec committed Jun 27, 2019
1 parent 5d8ff22 commit 373fa98
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions Orange/evaluation/scoring.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,8 @@ class AUC(ClassificationScore):
is_binary = True
long_name = "Area under ROC curve"

def calculate_weights(self, results):
@staticmethod
def calculate_weights(results):
classes = np.unique(results.actual)
class_cases = [sum(results.actual == class_)
for class_ in classes]
Expand All @@ -212,14 +213,14 @@ def calculate_weights(self, results):
else:
return weights / wsum

def single_class_auc(self, results, target):
@staticmethod
def single_class_auc(results, target):
y = np.array(results.actual == target, dtype=int)
return np.fromiter(
(skl_metrics.roc_auc_score(y, probabilities[:, int(target)])
for probabilities in results.probabilities),
dtype=np.float64, count=len(results.predicted))


def multi_class_auc(self, results):
classes = np.unique(results.actual)
weights = self.calculate_weights(results)
Expand Down Expand Up @@ -285,14 +286,15 @@ def compute_score(self, results, eps=1e-15, normalize=True,
class Specificity(ClassificationScore):
is_binary = True

def calculate_weights(self, results):
@staticmethod
def calculate_weights(results):
classes, counts = np.unique(results.actual, return_counts=True)
n = np.array(results.actual).shape[0]
return counts / n, classes

@staticmethod
def specificity(y_true, y_pred):
tn, fp, fn, tp = confusion_matrix(y_true, y_pred).ravel()
tn, fp, _, _ = confusion_matrix(y_true, y_pred).ravel()
return tn / (tn + fp)

def single_class_specificity(self, results, target):
Expand All @@ -306,7 +308,7 @@ def single_class_specificity(self, results, target):
def multi_class_specificity(self, results):
weights, classes = self.calculate_weights(results)
scores = np.array([self.single_class_specificity(results, class_)
for class_ in classes])
for class_ in classes])
return np.sum(scores.T * weights, axis=1)

def compute_score(self, results, target=None, average="binary"):
Expand Down

0 comments on commit 373fa98

Please sign in to comment.