How to check models accuracy using cross validation in Python?
MODEL SELECTION DATA CLEANING PYTHON DATA MUNGING MACHINE LEARNING RECIPES PANDAS CHEATSHEET     ALL TAGS

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

0

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 = Wine.data y = Wine.target

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
 0.79166667]

0.8647110297110298

0.05775178587574378

Relevant Projects

Data Science Project in Python on BigMart Sales Prediction
The goal of this data science project is to build a predictive model and find out the sales of each product at a given Big Mart store.

Machine Learning Project to Forecast Rossmann Store Sales
In this machine learning project you will work on creating a robust prediction model of Rossmann's daily sales using store, promotion, and competitor data.

Credit Card Fraud Detection as a Classification Problem
In this data science project, we will predict the credit card fraud in the transactional dataset using some of the predictive models.

Learn to prepare data for your next machine learning project
Text data requires special preparation before you can start using it for any machine learning project.In this ML project, you will learn about applying Machine Learning models to create classifiers and learn how to make sense of textual data.

Choosing the right Time Series Forecasting Methods
There are different time series forecasting methods to forecast stock price, demand etc. In this machine learning project, you will learn to determine which forecasting method to be used when and how to apply with time series forecasting example.

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.

German Credit Dataset Analysis to Classify Loan Applications
In this data science project, you will work with German credit dataset using classification techniques like Decision Tree, Neural Networks etc to classify loan applications using R.

Customer Market Basket Analysis using Apriori and Fpgrowth algorithms
In this data science project, you will learn how to perform market basket analysis with the application of Apriori and FP growth algorithms based on the concept of association rule learning.

Build a Collaborative Filtering Recommender System in Python
Use the Amazon Reviews/Ratings dataset of 2 Million records to build a recommender system using memory-based collaborative filtering in Python.

Mercari Price Suggestion Challenge Data Science Project
Data Science Project in Python- Build a machine learning algorithm that automatically suggests the right product prices.