Keras

Keras Regression Prediction using MPL

Keras Regression Prediction using MPL

This section will help you learn to create a simple regression prediction model with the help of MPL. To represent Regression MPL, you need to follow the steps described below:

Step – 1: Import required modules
The necessary modules required to create a regression prediction model are as follows:

import keras 

from keras.datasets import boston_housing 
from sklearn.preprocessing import scale
from keras.layers import Dense 
from keras.callbacks import EarlyStopping 
from sklearn import preprocessing 
from keras.optimizers import RMSprop

Step – 2: Load input data
Here we will import our dataset:

(model_train, model2_train), (model_test, model2_test) = data_set.load_data()

Step – 3:  Process the loaded data

Now, to process the loaded data, you can refer to the following code snippet:

scaled_model_train = preprocessing.scale(model_train) 
scaler_data = preprocessing.StandardScaler().fit(model_train) 
scaled_model_test = scaler.transform(model_test)

Step – 4: Create and compile the model
First, we will create the actual model with the following code:

model = Sequential() 
model.add(Dense(64, kernel_initializer = 'normal', activation = 'relu',
input_shape = (8,))) 
model.add(Dense(64, activation = 'relu')) model.add(Dense(1))

Now, we will compile the model using the code below:

model.compile(
   loss = 'mse', 
   optimizer = RMSprop(), 
   metrics = ['mean_absolute_error']
)

Step – 5: Now train the model

history = model.fit(
   scaled_model_train, model_train,    
   batch_size = 128, 
   epochs = 350, 
   verbose = 1, 
   validation_split = 0.2, 
   callbacks = [EarlyStopping(monitor = 'val_loss', patience = 20)]


)

history = model.fit(
   scaled_model_train, model_train,    
   batch_size = 128, 
   epochs = 350, 
   verbose = 1, 
   validation_split = 0.2, 
   callbacks = [EarlyStopping(monitor = 'val_loss', patience = 20)]
)

Step – 6: Model Evaluation

score = model.evaluate(scaled_model_test, model2_test, verbose = 0) 
print('The total test loss is:', score[0]) 
print('The total test accuracy is:', score[1])

Step – 7:  Predict from the model

prediction = model.predict(scaled_model_test) 
print(prediction.flatten()) 
print(model2_test)
​​​​​​​

This way you can implement CNN with the help of these steps and create regression prediction. 

Top course recommendations for you

    Arduino vs Raspberry Pi
    2 hrs
    Beginner
    7.3K+ Learners
    4.55  (616)
    Joins in SQL
    2 hrs
    Beginner
    10.6K+ Learners
    4.42  (603)
    Design Thinking for Beginners
    1 hrs
    Beginner
    14.8K+ Learners
    4.4  (1513)
    Database Management System
    1 hrs
    Beginner
    41.4K+ Learners
    4.39  (5258)
    Become a Web Developer
    1 hrs
    Beginner
    17.8K+ Learners
    4.4  (352)
    .NET Fundamentals
    2 hrs
    Beginner
    24.1K+ Learners
    4.41  (1907)