How to perform basic regression using keras model?

This recipe helps you perform basic regression using keras model

Recipe Objective

In machine learning, our main motive is to create a model that can relate the dependent variable(i.e target) with the independent variable(i.e. data). The most common model to do this is regression analysis. Regression fits the best possible curve on the training data set so that it can predict the target using the same curve.

So this recipe is a short example of How to perform basic regression using keras model?

Step 1 - Import the library

import pandas as pd import numpy as np from keras.datasets import mnist from sklearn.model_selection import train_test_split from keras.models import Sequential from keras.layers import Dense from keras.layers import Dropout

We have imported pandas, numpy, mnist(which is the dataset), train_test_split, Sequential, Dense and Dropout. We will use these later in the recipe.

Step 2 - Loading the Dataset

Here we have used the inbuilt mnist dataset and stored the train data in X_train and y_train. We have used X_test and y_test to store the test data. (X_train, y_train), (X_test, y_test) = mnist.load_data()

Step 3 - Creating Regression Model

We have created an object model for sequential model. We can use two args i.e layers and name. model = Sequential() Now, We are adding the layers by using 'add'. We can specify the type of layer, activation function to be used and many other things while adding the layer.
Here we are making regression model so we are making the linear stack of layers. We are using the activation function as 'relu' that is rectified linear unit, it has a advantage of being non linear also. model.add(Dense(512, activation='relu')) model.add(Dropout(0.5)) model.add(Dense(256, activation='relu')) model.add(Dropout(0.25)) model.add(Dense(10))

Step 4 - Compiling the model

We can compile a model by using compile attribute. Let us first look at its parameters before using it.

  • optimizer : In this, we can pass the optimizer we want to use. There is various optimizer like SGD, Adam etc.
  • loss : In this, we can pass a loss function which we want for the model
  • metrics : In this, we can pass the metric on which we want the model to be scored

model.compile(optimizer='Adam', loss='categorical_crossentropy', metrics=['accuracy'])

Step 5 - Fitting the model

We can fit a model on the data we have and can use the model after that. Here we are using the data which we have splitted i.e the training data for fitting the model.
While fitting we can pass various parameters like batch_size, epochs, verbose, validation_data and so on. model.fit(X_train, y_train, batch_size=128, epochs=2, verbose=1, validation_data=(X_test, y_test) model.summary()

Step 6 - Evaluating the model

After fitting a model we want to evaluate the model. Here we are using model.evaluate to evaluate the model and it will give us the loss and the accuracy. Here we have also printed the score. score = model.evaluate(X_test, y_test, verbose=0) print('Test loss:', score[0]) print('Test accuracy:', score[1])

Step 7 - Predicting the output

Finally we are predicting the output for this we are using another part of the data that we get from test_train_split i.e. test data. We will use it and predict the output. y_pred = model.predict(X_test) print(y_pred) As an output we get:

Epoch 1/2
469/469 [==============================] - 7s 14ms/step - loss: 0.3174 - accuracy: 0.9033 - val_loss: 0.1212 - val_accuracy: 0.9630
Epoch 2/2
469/469 [==============================] - 6s 14ms/step - loss: 0.1560 - accuracy: 0.9534 - val_loss: 0.0918 - val_accuracy: 0.9720
Test loss: 0.09184003621339798
Test accuracy: 0.972000002861023

[[8.92436292e-10 1.32853462e-09 6.39653945e-06 ... 9.99989152e-01
  1.79315840e-09 2.44941958e-07]
 [9.11153306e-11 1.03196271e-05 9.99982357e-01 ... 1.89035987e-09
  9.82423032e-09 8.40081246e-14]
 [1.10766098e-06 9.99514341e-01 1.26151179e-04 ... 1.44331687e-04
  4.99823145e-05 6.05678633e-06]
 ...
 [2.03985762e-09 1.29704825e-08 2.95020914e-08 ... 1.23884201e-05
  6.87194824e-06 1.75449488e-04]
 [5.91818647e-08 1.97798578e-08 7.46679774e-10 ... 5.06311437e-09
  1.96506153e-04 1.14137793e-08]
 [1.13083731e-09 5.45665553e-12 2.54836174e-09 ... 3.70580059e-13
  6.02386641e-10 3.15489106e-12]]

What Users are saying..

profile image

Anand Kumpatla

Sr Data Scientist @ Doubleslash Software Solutions Pvt Ltd
linkedin profile url

ProjectPro is a unique platform and helps many people in the industry to solve real-life problems with a step-by-step walkthrough of projects. A platform with some fantastic resources to gain... Read More

Relevant Projects

Time Series Classification Project for Elevator Failure Prediction
In this Time Series Project, you will predict the failure of elevators using IoT sensor data as a time series classification machine learning problem.

Mastering A/B Testing: A Practical Guide for Production
In this A/B Testing for Machine Learning Project, you will gain hands-on experience in conducting A/B tests, analyzing statistical significance, and understanding the challenges of building a solution for A/B testing in a production environment.

Isolation Forest Model and LOF for Anomaly Detection in Python
Credit Card Fraud Detection Project - Build an Isolation Forest Model and Local Outlier Factor (LOF) in Python to identify fraudulent credit card transactions.

PyCaret Project to Build and Deploy an ML App using Streamlit
In this PyCaret Project, you will build a customer segmentation model with PyCaret and deploy the machine learning application using Streamlit.

Forecasting Business KPI's with Tensorflow and Python
In this machine learning project, you will use the video clip of an IPL match played between CSK and RCB to forecast key performance indicators like the number of appearances of a brand logo, the frames, and the shortest and longest area percentage in the video.

Stock Price Prediction Project using LSTM and RNN
Learn how to predict stock prices using RNN and LSTM models. Understand deep learning concepts and apply them to real-world financial data for accurate forecasting.

Build a Logistic Regression Model in Python from Scratch
Regression project to implement logistic regression in python from scratch on streaming app data.

Build CI/CD Pipeline for Machine Learning Projects using Jenkins
In this project, you will learn how to create a CI/CD pipeline for a search engine application using Jenkins.

AWS MLOps Project for ARCH and GARCH Time Series Models
Build and deploy ARCH and GARCH time series forecasting models in Python on AWS .

Build Piecewise and Spline Regression Models in Python
In this Regression Project, you will learn how to build a piecewise and spline regression model from scratch in Python to predict the points scored by a sports team.