A note about get_scorer in sklearn

The version of scikit-learn is 0.21.3. The arguments are different when using accuracy_score directly and when get_scorer ('accuracy'). https://github.com/scikit-learn/scikit-learn/blob/95d4f0841/sklearn/metrics/_scorer.py#L393-L398

 $ python
Python 3.6.10 |Anaconda, Inc.| (default, Mar 25 2020, 23:51:54)
[GCC 7.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from sklearn.metrics import get_scorer, accuracy_score
>>> from sklearn.linear_model import LogisticRegression
>>> accuracy_score([1, 2, 3], [4, 5, 3])
0.3333333333333333
>>> get_scorer('accuracy')([1, 2, 3], [4, 5, 3])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: __call__() missing 1 required positional argument: 'y_true'
>>> estimator = LogisticRegression().fit([[10], [12], [13]], [1, 2, 3])
/home/hidetomo/.pyenv/versions/miniconda3-4.3.30/lib/python3.6/site-packages/sklearn/linear_model/logistic.py:432: FutureWarning: Default solver will be changed to 'lbfgs' in 0.22. Specify a solver to silence this warning.
  FutureWarning)
/home/hidetomo/.pyenv/versions/miniconda3-4.3.30/lib/python3.6/site-packages/sklearn/linear_model/logistic.py:469: FutureWarning: Default multi_class will be changed to 'auto' in 0.22. Specify the multi_class option to silence this warning.
  "this warning.", FutureWarning)
>>> estimator.predict([[10], [11], [12]])
array([3, 3, 3])
>>> get_scorer('accuracy')(estimator, [[10], [12], [13]], [1, 2, 3])
0.3333333333333333

Recommended Posts

A note about get_scorer in sklearn
A note about __call__
A note about subprocess
A note about mprotect (2)
Data analysis in Python: A note about line_profiler
A note about KornShell (ksh)
A note about TensorFlow Introduction
A note about [python] __debug__
Python: A Note About Classes 1 "Abstract"
A note about mock (Python mock library)
A note about doing the Pyramid tutorial
Note about awk
Just a note
A note on optimizing blackbox functions in Python
A memo about writing merge sort in Python
A note about the python version of python virtualenv
A simple Pub / Sub program note in Python
Think about building a Python 3 environment in a Mac environment
[Note] About the role of underscore "_" in Python
A note about the new style base class
A note about checking modifiers with Max Plus
A memorandum about matplotlib
Note about pointers (Go)
A memorandum about Nan.
dict in dict Makes a dict a dict
Things to note when initializing a list in Python
A story about reflecting Discord activity in Slack Status
A note on handling variables in Python recursive functions
A story about a Linux beginner passing LPIC101 in a week
About __all__ in python
A reminder about the implementation of recommendations in Python
About sklearn compliant model
[Note] A story about trying to override a class method with two underscores in Python 3 series.
A note on the default behavior of collate_fn in PyTorch
A note about hitting the Facebook API with the Python SDK
A story about how to specify a relative path in python.
A note for embedding the scripting language in a bash script
Note 2 for embedding the scripting language in a bash script
A story about competing with a friend in Othello AI Preparation
Errors that often occur in django Part 1 About template.exceptions.TemplateDoesNotExist (Note)
A note on touching Microsoft's face recognition API in Python
A note on how to load a virtual environment in PyCharm
[Note] Import of a file in the parent directory in Python
A story about trying to implement a private variable in Python.
A story about a tragedy happening by exchanging commands in chat
Take a screenshot in Python
Create a dictionary in Python
Collaborate in a remote environment
Think about architecture in python
Python Input Note in AtCoder
A memorandum about correlation [Python]
Make a bookmarklet in Python
A memorandum about Python mock
(Note) Django in Vagrant environment
A little more about FIFO
Draw a heart in Python
A small note following printf
About "for _ in range ():" in python
How about Anaconda for building a machine learning environment in Python?
About psd-tools, a library that can process psd files in Python
A story about a build error in a shared library that references libusb