How to use Regression Metrics in Python?
MACHINE LEARNING RECIPES DATA CLEANING PYTHON DATA MUNGING PANDAS CHEATSHEET     ALL TAGS

# How to use Regression Metrics in Python?

This recipe helps you use Regression Metrics in Python

## Recipe Objective

In a dataset after applying a regression model how to evaluate it. There are many metrics that we can use. We will be using mean absolute error , mean squared error and R squared.

So this is the recipe on how we we can use Regression Metrics in Python.

## Step 1 - Import the library

``` from sklearn import datasets from sklearn import tree, model_selection from sklearn.model_selection import train_test_split ```

We have imported datasets, tree, model_selection and test_train_split which will be needed for the dataset.

## Step 2 - Setting up the Data

We have imported inbuilt wine dataset and stored data in x and target in y. We have used to split the data by test train split. Then we have used model_selection.KFold. ``` seed = 42 dataset = datasets.load_wine() X = dataset.data y = dataset.target X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25) kfold = model_selection.KFold(n_splits=10, random_state=seed) ```

## Step 3 - Training model and calculating Metrics

Here we will be using DecisionTreeRegressior as a model ``` model = tree.DecisionTreeRegressor() ``` Now we will be calculating different metrics. We will be using cross validation score to calculate the metrices. So we will be printing the mean and standard deviation of all the scores.

• Calculating Mean Absolute Error
• ``` scoring = "neg_mean_absolute_error" results = model_selection.cross_val_score(model, X_train, y_train, cv=kfold, scoring=scoring) print("Mean Absolute Error: ", results.mean()); print("Standard Deviation: ", results.std()) ```
• Calculating Mean squared error
• ``` scoring = "neg_mean_squared_error" results = model_selection.cross_val_score(model, X_train, y_train, cv=kfold, scoring=scoring) print(); print("Mean Squared Error: ", results.mean()); print("Standard Deviation: ", results.std()) ```
• Calculating R squared value
• ``` scoring = "r2" results = model_selection.cross_val_score(model, X_train, y_train, cv=kfold, scoring=scoring) print(); print("R squared val: ", results.mean()); print("Standard Deviation: ", results.std()) ```
So the output comes as:

```Mean Absolute Error:  -0.12692307692307692
Standard Deviation:  0.09994715425303413

Mean Squared Error:  -0.13351648351648354
Standard Deviation:  0.10845352186546801

R squared val:  0.7997306366386379
Standard Deviation:  0.13923964626776147
​
```

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

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

##### Avocado Machine Learning Project Python for Price Prediction
In this ML Project, you will use the Avocado dataset to build a machine learning model to predict the average price of avocado which is continuous in nature based on region and varieties of avocado.

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

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

##### Walmart Sales Forecasting Data Science Project
Data Science Project in R-Predict the sales for each department using historical markdown data from the Walmart dataset containing data of 45 Walmart stores.

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

##### Build a Similar Images Finder with Python, Keras, and Tensorflow
Build your own image similarity application using Python to search and find images of products that are similar to any given product. You will implement the K-Nearest Neighbor algorithm to find products with maximum similarity.

##### Build OCR from Scratch Python using YOLO and Tesseract
In this deep learning project, you will learn how to build your custom OCR (optical character recognition) from scratch by using Google Tesseract and YOLO to read the text from any images.

##### Customer Churn Prediction Analysis using Ensemble Techniques
In this machine learning churn project, we implement a churn prediction model in python using ensemble techniques.