alvas alvas - 1 year ago 95
Python Question

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

When running cross-validation within

, all classifiers would have a factory function
which I can easily check the accuracy of the classifier, e.g. from

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

>>> iris = datasets.load_iris()
((150, 4), (150,))
>>> X_train, X_test, y_train, y_test = cross_validation.train_test_split(
...,, 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)

After digging into the github repo for
, I still can't figure out where is the function for the

There is this link but it doesn't contain the

Where is the
function for
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
classifiers, not to come up with my own scoring function =)

Answer Source

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.