Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add ap, acc, precision, recall, f1 #2100

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 56 additions & 1 deletion recbole/evaluator/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import numpy as np
from collections import Counter
from sklearn.metrics import auc as sk_auc
from sklearn.metrics import mean_absolute_error, mean_squared_error
from sklearn.metrics import mean_absolute_error, mean_squared_error, average_precision_score, accuracy_score, f1_score, precision_score, recall_score

from recbole.evaluator.utils import _binary_clf_curve
from recbole.evaluator.base_metric import AbstractMetric, TopkMetric, LossMetric
Expand Down Expand Up @@ -380,6 +380,61 @@ def metric_info(self, preds, trues):
return result


class AP(LossMetric):
def __init__(self, config):
super().__init__(config)

def calculate_metric(self, dataobject):
return self.output_metric("ap", dataobject)

def metric_info(self, preds, trues):
return average_precision_score(trues, preds)


class ACC(LossMetric):
def __init__(self, config):
super().__init__(config)

def calculate_metric(self, dataobject):
return self.output_metric("acc", dataobject)

def metric_info(self, preds, trues):
return accuracy_score(trues, preds > 0.5)


class Preci(LossMetric):
def __init__(self, config):
super().__init__(config)

def calculate_metric(self, dataobject):
return self.output_metric("precision", dataobject)

def metric_info(self, preds, trues):
return precision_score(trues, preds > 0.5)


class Recal(LossMetric):
def __init__(self, config):
super().__init__(config)

def calculate_metric(self, dataobject):
return self.output_metric("recall", dataobject)

def metric_info(self, preds, trues):
return recall_score(trues, preds > 0.5, zero_division=0)


class F1(LossMetric):
def __init__(self, config):
super().__init__(config)

def calculate_metric(self, dataobject):
return self.output_metric("f1_score", dataobject)

def metric_info(self, preds, trues):
return f1_score(trues, preds > 0.5, zero_division=0)


# Loss-based Metrics


Expand Down