top of page

Concrete Strength - Keras Regression

  • Writer: saman aboutorab
    saman aboutorab
  • Jan 4, 2024
  • 1 min read

Updated: Jan 8, 2024

In this project, you will build a regression model using the Keras library to model the same data about concrete compressive strength. We will build a regression model using the deep learning Keras library, and then you will experiment with increasing the number of training epochs and changing number of hidden layers and you will see how changing these parameters impacts the performance of the model.

The dataset is about the compressive strength of different samples of concrete based on the volumes of the different ingredients that were used to make them. Ingredients include:

  1. Cement

  2. Blast Furnace Slag

  3. Fly Ash

  4. Water

  5. Superplasticizer

  6. Coarse Aggregate

  7. Fine Aggregate






Import Libraries

import pandas as pd
import numpy as np
import keras
from keras.models import Sequential
from keras.layers import Dense
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_squared_error

Dataset

concrete_data = pd.read_csv('concrete_data.csv')
concrete_data.head()
concrete_data.shape

(1030,9)

concrete_data.describe()

Split data into predictors and target

concrete_data_columns = concrete_data.columns

target = concrete_data['Strength']
predictors = concrete_data[concrete_data_columns[concrete_data_columns != 'Strength']]

n_cols = predictors.shape[1]

Build a Neural Network

# define regression model
def regression_model():
  
  #Create model
  model = Sequential()
  model.add(Dense(10, activation='relu', input_shape=(n_cols,)))
  model.add(Dense(1))
    
  #compile model  
  model.compile(optimizer='adam', loss='mean_squared_error')

  return model

Train, fit and predict with model

mse_list = []
for i in range(50):
    #Split the data into 30% test and 70% training data
    x_train, x_test, y_train, y_test = train_test_split(predictors, target, test_size = 0.3)
    
    #Fit the model to training data
    model = regression_model()
    model.fit(x_train, y_train, epochs=50)
    
    #Predict the target of testing data
    y_hat = model.predict(x_test)
    
    #Evaluate the error of model
    mse = mean_squared_error(y_test, y_hat)
    mse_list.append(mse)

Evaluating the model with Mean Squared Error

np.mean(mse_list)

265.008

np.std(mse_list)

214.878

Normalization

predictors = (predictors - predictors.mean()) / predictors.std()

Train, fit and predict with model

mse_list_normalized = []

for i in range(50):
    print(f"Sample number:{i}")
    x_train, x_test, y_train, y_test = train_test_split(predictors, target, test_size = 0.3)
    
    model = regression_model()
    model.fit(x_train, y_train, epochs=50)
    
    y_hat = model.predict(x_test)
    mse = mean_squared_error(y_hat, y_test)
    mse_list_normalized.append(mse)

Evaluating the normalized model with Mean Squared Error

np.mean(mse_list_normalized)

388.830

np.std(mse_list_normalized)

121.690


Same model with Normalization and 100 epochs

mse_list_100epoches = []
for i in range(50):
    x_train, x_test, y_train, y_test = train_test_split(predictors, target, test_size = 0.3)
    
    model = regression_model()
    model.fit(x_train, y_train, epochs=100)
    
    y_hat = model.predict(x_test)
    
    mse = mean_squared_error(y_test, y_hat)
    mse_list_100epoches.append(mse)

np.mean(mse_list_100epoches)

152.056

np.std(mse_list_100epoches)

98.534


Conclusion


It can be seen that when we are normalizing the X data, the mean of mse is decreasing compared to non normalized (491.591), which shows we have lower error in general. Also, as the standard deviation is decreasing compared to non normalized (782.818), which shows we have a more uniform error list closer to it's mean.

It can be seen that when we are increasing epochs, the mean of mse is decreasing compared to 50 epochs (376.3), which shows we have lower error in general. Also, as the standard deviation is decreasing compared to 50 epochs (93.75), which shows we have a more uniform error list closer to it's mean.


Reference:

This notebook is part of a course on Coursera called AI Capstone Project with Deep Learning. If you accessed this notebook outside the course, you can take this course online by clicking here.

Copyright © 2020 IBM Developer Skills Network. This notebook and its source code are released under the terms of the MIT License.

Comments


bottom of page