alvas alvas - 6 months ago 24
Python Question

Where is the score function in scikit-learn classifiers located?

When running cross-validation within

scikit-learn
, all classifiers would have a factory function
score()
which I can easily check the accuracy of the classifier, e.g. from http://scikit-learn.org/stable/modules/cross_validation.html

>>> import numpy as np
>>> from sklearn import cross_validation
>>> from sklearn import datasets
>>> from sklearn import svm

>>> iris = datasets.load_iris()
>>> iris.data.shape, iris.target.shape
((150, 4), (150,))
>>> X_train, X_test, y_train, y_test = cross_validation.train_test_split(
... iris.data, iris.target, test_size=0.4, random_state=0)

>>> X_train.shape, y_train.shape
((90, 4), (90,))
>>> X_test.shape, y_test.shape
((60, 4), (60,))

>>> clf = svm.SVC(kernel='linear', C=1).fit(X_train, y_train)
>>> clf.score(X_test, y_test)
0.96...


After digging into the github repo for
scikit-learn
, I still can't figure out where is the function for the
clf.score()
function.

There is this link but it doesn't contain the
score()
, https://github.com/scikit-learn/scikit-learn/blob/master/sklearn/svm/classes.py

Where is the
score()
function for
sklearn
classifiers located?


I could implement my own score function easily, but the aim is to build my library so that it is coherent with the
sklearn
classifiers, not to come up with my own scoring function =)

Answer

The default score() method for scikit-learn classifiers is the accuracy score, and is defined in the ClassifierMixin class. This mixin is a parent class of most (all?) of scikit-learn's built-in classifiers.

If you're writing your own classifier, I would suggest inheriting from this mixin and BaseEstimator as well, so that you'll get the scoring and other features for your model automatically.

Comments