How to check models accuracy using cross validation in Python?

How to check models accuracy using cross validation in Python?

How to check models accuracy using cross validation in Python?

This recipe helps you check models accuracy using cross validation in Python

Recipe Objective

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

Step 1 - Import the library

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.

Step 2 - Setting up the Data

We have used an inbuilt Wine dataset. We have stored data in X and target in y. Wine = datasets.load_wine() X = y =

Step 3 - Model and its accuracy

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() print(mean_score) print(std_score) So the output comes as

[0.92592593 0.84615385 0.80769231 0.88       0.88       0.88



Download Materials

Relevant Projects

Human Activity Recognition Using Multiclass Classification in Python
In this human activity recognition project, we use multiclass classification machine learning techniques to analyse fitness dataset from a smartphone tracker.

Resume parsing with Machine learning - NLP with Python OCR and Spacy
In this machine learning resume parser example we use the popular Spacy NLP python library for OCR and text classification.

Predict Macro Economic Trends using Kaggle Financial Dataset
In this machine learning project, you will uncover the predictive value in an uncertain world by using various artificial intelligence, machine learning, advanced regression and feature transformation techniques.

Predict Churn for a Telecom company using Logistic Regression
Machine Learning Project in R- Predict the customer churn of telecom sector and find out the key drivers that lead to churn. Learn how the logistic regression model using R can be used to identify the customer churn in telecom dataset.

Build a Music Recommendation Algorithm using KKBox's Dataset
Music Recommendation Project using Machine Learning - Use the KKBox dataset to predict the chances of a user listening to a song again after their very first noticeable listening event.

Machine Learning project for Retail Price Optimization
In this machine learning pricing project, we implement a retail price optimization algorithm using regression trees. This is one of the first steps to building a dynamic pricing model.

Churn Prediction in Telecom using Machine Learning in R
Estimating churners before they discontinue using a product or service is extremely important. In this ML project, you will develop a churn prediction model in telecom to predict customers who are most likely subject to churn.

House Price Prediction Project using Machine Learning
Use the Zillow dataset to follow a test-driven approach and build a regression machine learning model to predict the price of the house based on other variables.

Image Segmentation using Mask R-CNN with Tensorflow
In this Deep Learning Project on Image Segmentation Python, you will learn how to implement the Mask R-CNN model for early fire detection.

Data Science Project - Instacart Market Basket Analysis
Data Science Project - Build a recommendation engine which will predict the products to be purchased by an Instacart consumer again.