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

Added asym_rmse and asym_mae accuracy measures #156

Open
wants to merge 2 commits 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
75 changes: 75 additions & 0 deletions surprise/accuracy.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,81 @@ def mae(predictions, verbose=True):
return mae_


def asym_rmse(predictions, weight=0.5, verbose=True):
"""Compute Asymmetric RMSE (Root Mean Squared Error).

.. math::
\\text{Asymmetric RMSE} = \\sqrt{\\frac{1}{|\\hat{R}|}
\\sum_{\\hat{r}_{ui} \in \\hat{R}}(r_{ui} - \\hat{r}_{ui})^2 |\\omega
- 1_{r_{ui} - \\hat{r}_{ui} < 0}|}.

Args:
predictions (:obj:`list` of :obj:`Prediction\
<surprise.prediction_algorithms.predictions.Prediction>`):
A list of predictions, as returned by the :meth:`test()
<surprise.prediction_algorithms.algo_base.AlgoBase.test>` method.
weight (int): Weight used to characterize asymmetry.
verbose: If True, will print computed value. Default is ``True``.


Returns:
The Asymmetric Root Mean Squared Error of predictions.

Raises:
ValueError: When ``predictions`` is empty.
"""

if not predictions:
raise ValueError('Prediction list is empty.')

res = np.array([float(true_r - est)
for (_, _, true_r, est, _) in predictions])
asym_rmse_ = np.sqrt(np.mean(res**2 * np.abs(weight -
(res < 0).astype(int))))

if verbose:
print('Asymmetric RMSE: {0:1.4f}'.format(asym_rmse_))

return asym_rmse_


def asym_mae(predictions, weight=0.5, verbose=True):
"""Compute Asymmetric MAE (Mean Absolute Error).

.. math::
\\text{Asymmetric MAE} = \\frac{1}{|\\hat{R}|} \\sum_{\\hat{r}_{ui} \in
\\hat{R}}|r_{ui} - \\hat{r}_{ui}| |\\omega - 1_{r_{ui} - \\hat{r}_{ui}
< 0}|.

Args:
predictions (:obj:`list` of :obj:`Prediction\
<surprise.prediction_algorithms.predictions.Prediction>`):
A list of predictions, as returned by the :meth:`test()
<surprise.prediction_algorithms.algo_base.AlgoBase.test>` method.
weight (int): Weight used to characterize asymmetry.
verbose: If True, will print computed value. Default is ``True``.


Returns:
The Asymmetric Mean Absolute Error of predictions.

Raises:
ValueError: When ``predictions`` is empty.
"""

if not predictions:
raise ValueError('Prediction list is empty.')

res = np.array([float(true_r - est)
for (_, _, true_r, est, _) in predictions])
asym_mae_ = np.mean(np.abs(res) * np.abs(weight - (res < 0).astype(int)))

if verbose:
print('Asymmetric MAE: {0:1.4f}'.format(asym_mae_))

return asym_mae_


def fcp(predictions, verbose=True):
"""Compute FCP (Fraction of Concordant Pairs).

Expand Down