12
$\begingroup$

I understand cross_validate and how it works, but now I am confused about what cross_val_score actually does. Can anyone give me some example?

$\endgroup$
12
$\begingroup$

cross_val_score is a helper function on the estimator and the dataset.

Would explain it with an example:

>>> from sklearn.model_selection import cross_val_score
>>> clf = svm.SVC(kernel='linear', C=1)
>>> scores = cross_val_score(clf, iris.data, iris.target, cv=5)
>>> scores                                              
array([ 0.96...,  1.  ...,  0.96...,  0.96...,  1.        ])

This example demonstrates how to estimate the accuracy of a linear kernel support vector machine on the iris dataset by splitting the data, fitting a model and computing the score 5 consecutive times (with different splits each time)

The cross_validate function differs from cross_val_score in two ways -

  1. It allows specifying multiple metrics for evaluation.
  2. It returns a dict containing training scores, fit-times and score-times in addition to the test score.

Note: When the cv argument is an integer, cross_val_score uses the KFold or StratifiedKFold strategies by default, the latter being used if the estimator derives from ClassifierMixin

You can go through this link for better understanding

Different examples using Cross_val_score, you can go through about its different implementations.

| improve this answer | |
$\endgroup$
-1
$\begingroup$
  1. cross_val_score: calculate score for each CV split
  2. cross_validate: calculate one or more scores and timings for each CV split
| improve this answer | |
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.