Building Regression Model using Keras: Part 2

3 minute read

In part 2, the network properties will again be increased to achieve a higher model accuracy. Later, the model will be saved, load and predict new concrete strength with new user-defined parameters.

Credit: IBM Cognitive Class

Building a Concrete Compressive Strength Model using Keras Framework

import pandas as pd
import numpy as np
url = 'https://s3-api.us-geo.objectstorage.softlayer.net/cf-courses-data/CognitiveClass/DL0101EN/labs/data/concrete_data.csv'
df = pd.read_csv(url)
df.head()
Cement Blast Furnace Slag Fly Ash Water Superplasticizer Coarse Aggregate Fine Aggregate Age Strength
0 540.0 0.0 0.0 162.0 2.5 1040.0 676.0 28 79.99
1 540.0 0.0 0.0 162.0 2.5 1055.0 676.0 28 61.89
2 332.5 142.5 0.0 228.0 0.0 932.0 594.0 270 40.27
3 332.5 142.5 0.0 228.0 0.0 932.0 594.0 365 41.05
4 198.6 132.4 0.0 192.0 0.0 978.4 825.5 360 44.30

MODEL - Construct Model to Predict and Forecast.

Split Data to Predictors and Target

X = df.iloc[:,:-1]
X.head()
Cement Blast Furnace Slag Fly Ash Water Superplasticizer Coarse Aggregate Fine Aggregate Age
0 540.0 0.0 0.0 162.0 2.5 1040.0 676.0 28
1 540.0 0.0 0.0 162.0 2.5 1055.0 676.0 28
2 332.5 142.5 0.0 228.0 0.0 932.0 594.0 270
3 332.5 142.5 0.0 228.0 0.0 932.0 594.0 365
4 198.6 132.4 0.0 192.0 0.0 978.4 825.5 360
y = df.iloc[:,-1]
y.head()
0    79.99
1    61.89
2    40.27
3    41.05
4    44.30
Name: Strength, dtype: float64

Normalizing Data

X_norm = (X - X.mean()) / X.std()
X_norm.head()
Cement Blast Furnace Slag Fly Ash Water Superplasticizer Coarse Aggregate Fine Aggregate Age
0 2.476712 -0.856472 -0.846733 -0.916319 -0.620147 0.862735 -1.217079 -0.279597
1 2.476712 -0.856472 -0.846733 -0.916319 -0.620147 1.055651 -1.217079 -0.279597
2 0.491187 0.795140 -0.846733 2.174405 -1.038638 -0.526262 -2.239829 3.551340
3 0.491187 0.795140 -0.846733 2.174405 -1.038638 -0.526262 -2.239829 5.055221
4 -0.790075 0.678079 -0.846733 0.488555 -1.038638 0.070492 0.647569 4.976069

Save number of feature columns, n_cols to use later in model development.

n_cols = X.shape[1]
n_cols
8

Importing Libraries

from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_squared_error
from sklearn.metrics import r2_score

import keras

from keras.models import Sequential
from keras.layers import Dense
Using TensorFlow backend.

Building the Model

Network Properties:

  • Hidden Layers: 7
  • Nodes: 25
  • Activation Function: ReLU
  • Optimizer: Adam
  • Loss Function: Mean Squared Error
  • Epochs: 250
mse = []
r2 = []

for i in range(50):
    
    #Split Data to Train and Test Set
    X_train, X_test, y_train, y_test = train_test_split(X_norm, y, test_size = 0.3)

    #Create model
    model = Sequential()
    model.add(Dense(25, activation='relu', input_shape=(n_cols,)))
    model.add(Dense(25, activation='relu'))
    model.add(Dense(25, activation='relu'))
    model.add(Dense(25, activation='relu'))
    model.add(Dense(25, activation='relu'))
    model.add(Dense(25, activation='relu'))
    model.add(Dense(25, activation='relu'))
    model.add(Dense(1))

    #Compile model
    model.compile(optimizer='adam', loss='mean_squared_error')

    #fit the model
    model.fit(X_train, y_train, epochs=250, verbose=0)

    #predict output on test set
    y_pred = model.predict(X_test)
    
    mse.append(mean_squared_error(y_test, y_pred))
    r2.append(r2_score(y_test, y_pred))
print('mse_Mean: {:.2f}'.format(np.mean(mse)))
print('mse_StdDev: {:.2f}'.format(np.std(mse)))
mse_Mean: 31.73
mse_StdDev: 5.62
print('R^2_Mean: {:.2f}'.format(np.mean(r2)))
print('R^2_StdDev: {:.2f}'.format(np.std(r2)))
R^2_Mean: 0.89
R^2_StdDev: 0.02
from IPython.display import HTML, display
import tabulate

tabletest = [['MSE: Mean','MSE: StdDev','R^2: Mean','R^2: StdDev'],
         [round(np.mean(mse),2), round(np.std(mse),2), round(np.mean(r2),2), round(np.std(r2),2)]]

display(HTML(tabulate.tabulate(tabletest, tablefmt='html')))
MSE: MeanMSE: StdDevR^2: MeanR^2: StdDev
31.73 5.62 0.89 0.02

Comparing the results from Part 1:

  • mean squared error has gone down from 112.99 to 31.73
  • R^2 has gone up from 0.59 to 0.89

which means the overall accuracy has gone up compared from the previous run.

SAVING MODEL

model.save('keras_reg.h5')

Predict New Concrete Strength Value with Pretrained Model

Import Module

import numpy as np
from keras.models import load_model

Load Pretrained Model

keras_reg = load_model('keras_reg.h5')

Insert New Parameters

#Cement
x1 = 1540.0

#Blast Furnace Slag
x2 = 250.0

#Fly Ash
x3 = 0.0

#Water
x4 = 162.0

#Superplasticizer
x5 = 2.5

#Coarse Aggregate
x6 = 1055.0

#Fine Aggregate
x7 = 676.0

#Age
x8 = 450
X = np.array([[x1, x2, x3, x4, x5, x6, x7, x8]])
X_norm = (X - X.mean()) / X.std()
strength_pred = keras_reg.predict(X_norm)

New Prediction

print('The predicted concrete strength is {:.2f}'.format(strength_pred[0][0]))
The predicted concrete strength is 56.26