check_scoring#

sklearn.metrics.check_scoring(estimator=None,scoring=None,*,allow_none=False,raise_exc=True)[source]#

Determine scorer from user options.

A TypeError will be thrown if the estimator cannot be scored.

Parameters:
estimatorestimator object implementing ‘fit’ or None, default=None

The object to use to fit the data. IfNone, then this function may errordepending onallow_none.

scoringstr, callable, list, tuple, set, or dict, default=None

Scorer to use. Ifscoring represents a single score, one can use:

Ifscoring represents multiple scores, one can use:

  • a list, tuple or set of unique strings;

  • a callable returning a dictionary where the keys are the metric names and thevalues are the metric scorers;

  • a dictionary with metric names as keys and callables a values. The callablesneed to have the signaturecallable(estimator,X,y).

allow_nonebool, default=False

Whether to return None or raise an error if noscoring is specified and theestimator has noscore method.

raise_excbool, default=True

Whether to raise an exception (if a subset of the scorers in multimetric scoringfails) or to return an error code.

  • If set toTrue, raises the failing scorer’s exception.

  • If set toFalse, a formatted string of the exception details is passed asresult of the failing scorer(s).

This applies ifscoring is list, tuple, set, or dict. Ignored ifscoring isa str or a callable.

Added in version 1.6.

Returns:
scoringcallable

A scorer callable object / function with signaturescorer(estimator,X,y).

Examples

>>>fromsklearn.datasetsimportload_iris>>>fromsklearn.metricsimportcheck_scoring>>>fromsklearn.treeimportDecisionTreeClassifier>>>X,y=load_iris(return_X_y=True)>>>classifier=DecisionTreeClassifier(max_depth=2).fit(X,y)>>>scorer=check_scoring(classifier,scoring='accuracy')>>>scorer(classifier,X,y)0.96...
>>>fromsklearn.metricsimportmake_scorer,accuracy_score,mean_squared_log_error>>>X,y=load_iris(return_X_y=True)>>>y*=-1>>>clf=DecisionTreeClassifier().fit(X,y)>>>scoring={..."accuracy":make_scorer(accuracy_score),..."mean_squared_log_error":make_scorer(mean_squared_log_error),...}>>>scoring_call=check_scoring(estimator=clf,scoring=scoring,raise_exc=False)>>>scores=scoring_call(clf,X,y)>>>scores{'accuracy': 1.0, 'mean_squared_log_error': 'Traceback ...'}
On this page

This Page