After training a model we need a measure to check its performance, their are many scoring metric on which we can score the model"s performance. Out of many metric we will be using accuracy to measure our models performance. We will also be using cross validation to test the model on multiple sets of data.
So this is the recipe on How we can check model"s accuracy using cross validation in Python
from sklearn.model_selection import cross_val_score from sklearn.tree import DecisionTreeClassifier from sklearn import datasets
We have imported various modules from differnt libraries such as cross_val_score, DecisionTreeClassifier and datasets.
We have used an inbuilt Wine dataset. We have stored data in X and target in y.
Wine = datasets.load_wine()
X = Wine.data
y = Wine.target
We are using DecisionTreeClassifier as a model to train the data. We are training the model with cross_validation which will train the data on different training set and it will calculate accuracy for all the test train split.
We are printing the accuracy for all the splits in cross validation. We have passed model, data, target and cv an parameters. cv signifies the number of splits we want while performing cross validation. We are also printing mean and standard deviation of average precision.
dtree = DecisionTreeClassifier()
print(); print(cross_val_score(dtree, X, y, scoring="accuracy", cv = 7))
mean_score = cross_val_score(dtree, X, y, scoring="accuracy", cv = 7).mean()
std_score = cross_val_score(dtree, X, y, scoring="accuracy", cv = 7).std()
So the output comes as
[0.92592593 0.84615385 0.80769231 0.88 0.88 0.88 0.79166667] 0.8647110297110298 0.05775178587574378