Skip to content

Commit

Permalink
AP and MAP metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
mike-chesnokov committed Feb 26, 2021
1 parent 4f1a25f commit 8d4503f
Show file tree
Hide file tree
Showing 9 changed files with 64 additions and 4 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# kaggle_utils
Python utils for kaggle
# ML utils
Machine Learning Python utils:

- `metrics_utils.py`: functions for metrics calculation;
- `lightgbm_utils.py`: LightGBM functions (cross-validation with Out Of Fold predictions and etc.);
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion kaggle_utils/lightgbm_utils.py → utils/lightgbm_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
)
from sklearn.model_selection import KFold

from kaggle_utils.metrics_utils import rmse, rmse_lgb
from utils.metrics_utils import rmse, rmse_lgb


kf = KFold(n_splits=5, random_state=7, shuffle=False)
Expand Down
62 changes: 61 additions & 1 deletion kaggle_utils/metrics_utils.py → utils/metrics_utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# Functions for metrics calculation
import numpy as np
import pandas as pd
from sklearn.metrics import mean_squared_error, roc_auc_score


Expand Down Expand Up @@ -130,3 +129,64 @@ def gini_lgb(preds, dtrain):
def auc_lgb(preds, dtrain):
actuals = np.array(dtrain.get_label())
return 'auc', roc_auc_score(actuals, preds), True

# Average Precision (AP@k) and Mean Average Precision (MAP@k)
# from https://github.com/benhamner/Metrics/blob/master/Python/ml_metrics/average_precision.py


def apk(actual, predicted, k=5):
"""
Computes the average precision at k.
This function computes the average precision at k between two lists of
items.
Parameters
----------
actual : list
A list of elements that are to be predicted (order doesn't matter)
predicted : list
A list of predicted elements (order does matter)
k : int, optional
The maximum number of predicted elements
Returns
-------
score : double
The average precision at k over the input lists
"""
if len(predicted) > k:
predicted = predicted[:k]

score = 0.0
num_hits = 0.0

for ind, pred in enumerate(predicted):
if pred in actual and pred not in predicted[:ind]:
num_hits += 1.0
score += num_hits / (ind + 1.0)

if not actual:
return 0.0

return score / min(len(actual), k)


def mapk(actual, predicted, k=5):
"""
Computes the mean average precision at k.
This function computes the mean average prescision at k between two lists
of lists of items.
Parameters
----------
actual : list
A list of lists of elements that are to be predicted
(order doesn't matter in the lists)
predicted : list
A list of lists of predicted elements
(order matters in the lists)
k : int, optional
The maximum number of predicted elements
Returns
-------
score : double
The mean average precision at k over the input lists
"""
return np.mean([apk(act, pred, k) for act, pred in zip(actual, predicted)])
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit 8d4503f

Please sign in to comment.