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

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

This recipe helps you check models Average precision score 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 Average 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 Average precision score 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.datasets import make_classification from sklearn import datasets ```

We have imported various modules from differnt 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 average precision for all the test train split.

We are printing the average 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 average precision. ``` dtree = DecisionTreeClassifier() print(cross_val_score(dtree, X, y, scoring="average_precision", cv = 7)) mean_score = cross_val_score(dtree, X, y, scoring="average_precision", cv = 7).mean() std_score = cross_val_score(dtree, X, y, scoring="average_precision", cv = 7).std() print(mean_score) print(std_score) ``` So the output comes as

```[0.92239851 0.90536365 0.90846709 0.97351446 0.91296365 0.95900582
0.91565723]

0.9261532204977229

0.02232220312858589
```

#### Relevant Projects

##### Predict Census Income using Deep Learning Models
In this project, we are going to work on Deep Learning using H2O to predict Census income.

##### Data Science Project on Wine Quality Prediction in R
In this R data science project, we will explore wine dataset to assess red wine quality. The objective of this data science project is to explore which chemical properties will influence the quality of red wines.

##### 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.

##### Machine Learning or Predictive Models in IoT - Energy Prediction Use Case
In this machine learning and IoT project, we are going to test out the experimental data using various predictive models and train the models and break the energy usage.

##### Predict Credit Default | Give Me Some Credit Kaggle
In this data science project, you will predict borrowers chance of defaulting on credit loans by building a credit score prediction model.

##### Data Science Project-TalkingData AdTracking Fraud Detection
Machine Learning Project in R-Detect fraudulent click traffic for mobile app ads using R data science programming language.

##### 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.

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

##### Perform Time series modelling using Facebook Prophet
In this project, we are going to talk about Time Series Forecasting to predict the electricity requirement for a particular house using Prophet.

##### Deep Learning with Keras in R to Predict Customer Churn
In this deep learning project, we will predict customer churn using Artificial Neural Networks and learn how to model an ANN in R with the keras deep learning package.