Skip to content

Instantly share code, notes, and snippets.

@searope
Last active June 7, 2022 03:52
Show Gist options
  • Select an option

  • Save searope/71a4dbebc4b1356e87fe9fa2c021b7ae to your computer and use it in GitHub Desktop.

Select an option

Save searope/71a4dbebc4b1356e87fe9fa2c021b7ae to your computer and use it in GitHub Desktop.
helper class to make custom mentrics easier in sklearn
import numpy as np
from sklearn.preprocessing import QuantileTransformer
from sklearn.metrics._scorer import _BaseScorer
class FuncScorer(_BaseScorer):
def _score(self, method_caller, estimator, X, y_true, sample_weight=None):
"""Evaluate predicted target values for X relative to y_true.
Parameters
----------
method_caller : callable
Returns predictions given an estimator, method name, and other
arguments, potentially caching results.
estimator : object
Trained estimator to use for scoring. Must have a `predict`
method; the output of that is used to compute the score.
X : {array-like, sparse matrix}
Test data that will be fed to estimator.predict.
y_true : array-like
Gold standard target values for X.
sample_weight : array-like of shape (n_samples,), default=None
Sample weights.
Returns
-------
score : float
Score function applied to prediction of estimator on X.
"""
y_pred = method_caller(estimator, "predict", X)
if sample_weight is not None:
return self._sign * self._score_func(estimator, X, y_true, y_pred, sample_weight=sample_weight, **self._kwargs)
else:
return self._sign * self._score_func(estimator, X, y_true, y_pred, **self._kwargs)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment