Cross-validation in sklearn
Cross_val_score function
Say we trained a model using Support Vector Machine (SVM):
from sklearn.model_selection import cross_val_score
clf = svm.SVC(kernel='linear', C=1)
scores = cross_val_score(clf, X, y, cv=5)
print(scores)
print("%0.2f accuracy with a standard deviation of %0.2f" % (scores.mean(), scores.std()))You can also choose another metric, for example:
scores = cross_val_score(clf,X,y,cv=5, scoring='f1_macro')Cross_validate function
- ð Sklearn Docs - sklearn.model_selection.cross_validate This function is used when you want to specify multiple metrics for evaulation either/or you want more metadata like fit-times and score-times.
Multiple Metrics
scoring = ['precision_macro', 'recall_macro']
clf = svm.SVC(kernel='linear', C=1)
scores = cross_validate(clf, X, y, scoring=scoring)Other techniques
There are a lot of techniques implemented in the sklearn library, you can read more about them here.