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.

Access Linear Regression ML Project for Beginners with Source Code

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
​

Download Materials


What Users are saying..

profile image

Ray han

Tech Leader | Stanford / Yale University
linkedin profile url

I think that they are fantastic. I attended Yale and Stanford and have worked at Honeywell,Oracle, and Arthur Andersen(Accenture) in the US. I have taken Big Data and Hadoop,NoSQL, Spark, Hadoop... Read More

Relevant Projects

Natural language processing Chatbot application using NLTK for text classification
In this NLP AI application, we build the core conversational engine for a chatbot. We use the popular NLTK text classification library to achieve this.

Learn Hyperparameter Tuning for Neural Networks with PyTorch
In this Deep Learning Project, you will learn how to optimally tune the hyperparameters (learning rate, epochs, dropout, early stopping) of a neural network model in PyTorch to improve model performance.

MLOps AWS Project on Topic Modeling using Gunicorn Flask
In this project we will see the end-to-end machine learning development process to design, build and manage reproducible, testable, and evolvable machine learning models by using AWS

Build Customer Propensity to Purchase Model in Python
In this machine learning project, you will learn to build a machine learning model to estimate customer propensity to purchase.

Classification Projects on Machine Learning for Beginners - 2
Learn to implement various ensemble techniques to predict license status for a given business.

Time Series Forecasting Project-Building ARIMA Model in Python
Build a time series ARIMA model in Python to forecast the use of arrival rate density to support staffing decisions at call centres.

Build Regression (Linear,Ridge,Lasso) Models in NumPy Python
In this machine learning regression project, you will learn to build NumPy Regression Models (Linear Regression, Ridge Regression, Lasso Regression) from Scratch.

Deploy Transformer BART Model for Text summarization on GCP
Learn to Deploy a Machine Learning Model for the Abstractive Text Summarization on Google Cloud Platform (GCP)

PyTorch Project to Build a LSTM Text Classification Model
In this PyTorch Project you will learn how to build an LSTM Text Classification model for Classifying the Reviews of an App .

Build PowerBI Dashboard for Water Quality Sensor Data Analysis
In this PowerBI Project, you will learn to build a PowerBI Dashboard to analyze and visualize water quality sensor data from various European countries.

OSZAR »