To test how our model is performing we need a scoring metric and for classifier we can use recall score. Here we will using cross validation to split the data into various set and test the model on a single set while training it on other.
So this is the recipe on how we can check model"s recall score using cross validation in Python.
from sklearn.model_selection import cross_val_score from sklearn.tree import DecisionTreeClassifier from sklearn import datasets
We have only imported cross_val_score, DecisionTreeClassifier and datasets which is needed.
We have imported an inbuilt breast cancer dataset to train the model. We have stored data in X and target in y.
cancer = datasets.load_breast_cancer()
X = cancer.data
y = cancer.target
We have used DecisionTreeClassifier as a model and used cross validation. In cross validation we have passed model, scoring metric as recall and cv as 5.
we have calculated mean and standard deviation of cross validation score.
dtree = DecisionTreeClassifier()
print(cross_val_score(dtree, X, y, scoring="recall", cv = 5))
mean_score = cross_val_score(dtree, X, y, scoring="recall", cv = 5).mean()
std_score = cross_val_score(dtree, X, y, scoring="recall", cv = 5).std()
So the output comes
[0.90277778 0.93055556 0.95774648 0.95774648 0.84507042] 0.9160015649452269 0.03188194241586345