Skip to content

Commit

Permalink
Merge branch '_master' into issue#78
Browse files Browse the repository at this point in the history
  • Loading branch information
m3ttiw committed Jun 19, 2020
2 parents 369ae86 + e7deaec commit a858f52
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 40 deletions.
19 changes: 9 additions & 10 deletions orange_cb_recsys/evaluation/classification_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ def perform(self, predictions: pd.DataFrame, truth: pd.DataFrame):
class Precision(ClassificationMetric):
"""
Precision
.. math::
\\begin{align*}
\\mathrm{Precision}& = #relevant recommended items \over #recommended items
\\end{align*}
Args:
relevant_threshold: specify the minimum value to consider
Expand All @@ -60,11 +64,7 @@ def perform(self, predictions: pd.DataFrame, truth: pd.DataFrame) -> float:
"""
Compute the precision of the given ranking (predictions)
based on the truth ranking
.. math::
\\begin{align*}
\\mathrm{Precision}& = #relevant recommended items \over #recommended items
\\end{align*}
Args:
truth (pd.DataFrame): dataframe whose columns are: to_id, rating
predictions (pd.DataFrame): dataframe whose columns are: to_id, rating;
Expand All @@ -83,6 +83,10 @@ def perform(self, predictions: pd.DataFrame, truth: pd.DataFrame) -> float:
class Recall(ClassificationMetric):
"""
Recall
.. math::
\\begin{align*}
\\mathrm{Precision}& = #relevant recommended items \over #tested relevant items
\\end{align*}
Args:
relevant_threshold: specify the minimum value to consider
Expand All @@ -99,11 +103,6 @@ def perform(self, predictions: pd.DataFrame, truth: pd.DataFrame) -> float:
Compute the recall of the given ranking (predictions)
based on the truth ranking
.. math::
\\begin{align*}
\\mathrm{Precision}& = #relevant recommended items \over #tested relevant items
\\end{align*}
Args:
truth (pd.DataFrame): dataframe whose columns are: to_id, rating
predictions (pd.DataFrame): dataframe whose columns are: to_id, rating;
Expand Down
18 changes: 9 additions & 9 deletions orange_cb_recsys/evaluation/fairness_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,7 @@ def perform(self, predictions: pd.DataFrame, truth: pd.DataFrame):
class GiniIndex(FairnessMetric):
"""
Gini index
"""
def __init__(self, item: bool = False):
super().__init__(None, None)
self.__item = item

def perform(self, predictions: pd.DataFrame, truth: pd.DataFrame = None) -> pd.DataFrame:
"""
Calculate Gini index score for each user or item in the DataFrame
.. math::
\\begin{align*}
\\mathrm{Gini\; index }& = \frac{\sum_{i = 0}^{n} (2i - n - 1)}{n \sum_{i = 0}^{n} elem_i }
Expand All @@ -87,6 +79,14 @@ def perform(self, predictions: pd.DataFrame, truth: pd.DataFrame = None) -> pd.D
Where:
- n is the size of the user or item set
- elem(i) is the user or the item in the i-th position in the sorted frame by user or item
"""
def __init__(self, item: bool = False):
super().__init__(None, None)
self.__item = item

def perform(self, predictions: pd.DataFrame, truth: pd.DataFrame = None) -> pd.DataFrame:
"""
Calculate Gini index score for each user or item in the DataFrame
Args:
truth (pd.DataFrame): original rating frame used for recsys config
Expand Down
15 changes: 7 additions & 8 deletions orange_cb_recsys/evaluation/novelty.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,6 @@
class Novelty(Metric):
"""
Novelty
"""
def __init__(self, num_of_recs):
self.__num_of_recs = num_of_recs

def perform(self, predictions: pd.DataFrame, truth: pd.DataFrame) -> float:
"""
Calculates the novelty score
.. math::
\\begin{align*}
\\mathrm{Novelty}& = \frac{\sum_{{i} \in hits} log_2(1/popularity_i)}{|hits|}
Expand All @@ -25,6 +17,13 @@ def perform(self, predictions: pd.DataFrame, truth: pd.DataFrame) -> float:
where:
- hits is a set of predicted items
- Popularity(i) = % users who rated item i
"""
def __init__(self, num_of_recs):
self.__num_of_recs = num_of_recs

def perform(self, predictions: pd.DataFrame, truth: pd.DataFrame) -> float:
"""
Calculates the novelty score
Args:
truth (pd.DataFrame): original rating frame used for recsys config
Expand Down
21 changes: 14 additions & 7 deletions orange_cb_recsys/evaluation/prediction_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,18 @@ def perform(self, predictions: pd.DataFrame, truth: pd.DataFrame):


class RMSE(PredictionMetric):
def perform(self, predictions: pd.DataFrame, truth: pd.DataFrame) -> float:
"""
Compute the RMSE metric
"""
RMSE
.. math::
\\begin{align*}
\\mathrm{RMSE}& = \\sqrt{\frac{\sum_{{u, i} \in T} ({r_{ui} - r'_{ui}})^2}{|T|}}
\\end{align*}
Where T is the test set and r' is the actual score give by user u to item i
"""
def perform(self, predictions: pd.DataFrame, truth: pd.DataFrame) -> float:
"""
Compute the RMSE metric
Args:
truth (pd.DataFrame): dataframe whose columns are: to_id, rating
predictions (pd.DataFrame): dataframe whose columns are: to_id, rating
Expand All @@ -46,14 +50,17 @@ def perform(self, predictions: pd.DataFrame, truth: pd.DataFrame) -> float:


class MAE(PredictionMetric):
def perform(self, predictions: pd.DataFrame, truth: pd.DataFrame) -> float:
"""
Compute the MAE metric
.. math::
"""
MAE
..math::
\\begin{align*}
\\mathrm{MAE}& = \\sum_{{u, i} \in T} \abs{{r_{ui} - r'_{ui}}} \over |T|
\\end{align*}
Where T is the test set and r' is the actual score give by user u to item i
"""
def perform(self, predictions: pd.DataFrame, truth: pd.DataFrame) -> float:
"""
Compute the MAE metric
Args:
truth (pd.DataFrame): dataframe whose columns are: to_id, rating
Expand Down
14 changes: 8 additions & 6 deletions orange_cb_recsys/evaluation/serendipity.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,21 @@


class Serendipity(Metric):
"""
Serendipity
.. math::
\\begin{align*}
\\mathrm{Unexpected \; recommendations}& = |recommendations \; from \; tested \; algorithm| - |recommendations \; from \; obvious \; algorithm|
\\end{align*}
"""
def __init__(self, num_of_recs: int):
self.__num_of_recs = num_of_recs

def perform(self, predictions: pd.DataFrame, truth: pd.DataFrame) -> float:
"""
Calculates the serendipity score: unexpected recommendations, surprisingly and interesting items a user
might not have otherwise discovered
.. math::
\\begin{align*}
\\mathrm{Unexpected \; recommendations}& = |recommendations \; from \; tested \; algorithm| - |recommendations \; from \; obvious \; algorithm|
\\end{align*}
Args:
truth (pd.DataFrame): original rating frame used for recsys config
predictions (pd.DataFrame): dataframe with recommendations for multiple users
Expand Down

0 comments on commit a858f52

Please sign in to comment.