How to check models precision score using cross validation in Python?

This recipe helps you check models precision score 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 precision 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 precision score using cross validation in Python.

Learn How to Build a Multi Class Text Classification Model using BERT

Step 1 - Import the library

from sklearn.model_selection import cross_val_score from sklearn.tree import DecisionTreeClassifier from sklearn.datasets import make_classification from sklearn import datasets

We have imported various modules from different libraries such as cross_val_score, DecisionTreeClassifier, datasets and make_classification.

Step 2 - Setting up the Data

We have used an inbuilt cancer dataset. We have stored data in X and target in y. cancer = datasets.load_breast_cancer() X = cancer.data y = cancer.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 precision for all the test train split. We are printing the precision 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 precision. dtree = DecisionTreeClassifier() print(cross_val_score(dtree, X, y, scoring="precision", cv = 7)) mean_score = cross_val_score(dtree, X, y, scoring="precision", cv = 7).mean() std_score = cross_val_score(dtree, X, y, scoring="precision", cv = 7).std() print(mean_score) print(std_score) So the output comes as

[0.94117647 0.90566038 0.94       0.94339623 0.96078431 0.97959184
 0.95833333]

0.9489445085248625

0.022393630261369132

Download Materials

What Users are saying..

profile image

Gautam Vermani

Data Consultant at Confidential
linkedin profile url

Having worked in the field of Data Science, I wanted to explore how I can implement projects in other domains, So I thought of connecting with ProjectPro. A project that helped me absorb this topic... Read More

Relevant Projects

Build a Speech-Text Transcriptor with Nvidia Quartznet Model
In this Deep Learning Project, you will leverage transfer learning from Nvidia QuartzNet pre-trained models to develop a speech-to-text transcriptor.

Learn to Build Generative Models Using PyTorch Autoencoders
In this deep learning project, you will learn how to build a Generative Model using Autoencoders in PyTorch

Medical Image Segmentation Deep Learning Project
In this deep learning project, you will learn to implement Unet++ models for medical image segmentation to detect and classify colorectal polyps.

Topic modelling using Kmeans clustering to group customer reviews
In this Kmeans clustering machine learning project, you will perform topic modelling in order to group customer reviews based on recurring patterns.

NLP Project for Beginners on Text Processing and Classification
This Project Explains the Basic Text Preprocessing and How to Build a Classification Model in Python

Azure Text Analytics for Medical Search Engine Deployment
Microsoft Azure Project - Use Azure text analytics cognitive service to deploy a machine learning model into Azure Databricks

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.

Ecommerce product reviews - Pairwise ranking and sentiment analysis
This project analyzes a dataset containing ecommerce product reviews. The goal is to use machine learning models to perform sentiment analysis on product reviews and rank them based on relevance. Reviews play a key role in product recommendation systems.

Create Your First Chatbot with RASA NLU Model and Python
Learn the basic aspects of chatbot development and open source conversational AI RASA to create a simple AI powered chatbot on your own.

MLOps Project to Deploy Resume Parser Model on Paperspace
In this MLOps project, you will learn how to deploy a Resume Parser Streamlit Application on Paperspace Private Cloud.