Phone

+919997782184

Email

support@roboticswithpython.com

Geeks of Coding

Join us on Telegram

Home Forums Assignment courserra IBM AI Engineering Professional Certificate Introduction to Deep Learning & Neural Networks with Keras WEEK 5 – Peer-graded Assignment: Build a Regression Model in Keras – GRADED

Viewing 0 reply threads
  • Author
    Posts
    • #1184
      Yash AroraYash Arora
      Keymaster

      WEEK 5 Assignment – Introduction to Deep Learning & Neural Networks with Keras

      Click to Project URL GitHub Repository

      To download Dataset – https://cocl.us/concrete_data
      CODE –
      #Importing Required Packages

      import pandas as pd
      import numpy as np

      Importing Convulation

      concrete_data = pd.read_csv('https://cocl.us/concrete_data')
      concrete_data.head()

      concrete_data.shape
      concrete_data.describe()
      concrete_data.isnull().sum()
      Cleaning and Normalizing Data

      concrete_data_columns = concrete_data.columns
      predictors = concrete_data[concrete_data_columns[concrete_data_columns != 'Strength']] # all columns except Strength
      target = concrete_data['Strength'] # Strength column

      predictors.head()
      target.head()

      n_cols = predictors.shape[1] # number of predictors
      n_cols

      Import Keras
      import keras
      Import Useful Packages

      from keras.models import Sequential
      from keras.layers import Dense
      # 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

      from sklearn.model_selection import train_test_split
      X_train, X_test, y_train, y_test = train_test_split(predictors, target, test_size=0.3, random_state=42)
      Train and Test the Network

      # build the model
      model = regression_model()
      # fit the model
      epochs = 50
      model.fit(X_train, y_train, epochs=epochs, verbose=1)
      loss_val = model.evaluate(X_test, y_test)
      y_pred = model.predict(X_test)
      loss_val

      from sklearn.metrics import mean_squared_error

      mean_square_error = mean_squared_error(y_test, y_pred)
      mean = np.mean(mean_square_error)
      standard_deviation = np.std(mean_square_error)
      print(mean, standard_deviation)
      total_mean_squared_errors = 50
      epochs = 50
      mean_squared_errors = []
      for i in range(0, total_mean_squared_errors):
          X_train, X_test, y_train, y_test = train_test_split(predictors, target, test_size=0.3, random_state=i)
          model.fit(X_train, y_train, epochs=epochs, verbose=0)
          MSE = model.evaluate(X_test, y_test, verbose=0)
          print("MSE "+str(i+1)+": "+str(MSE))
          y_pred = model.predict(X_test)
          mean_square_error = mean_squared_error(y_test, y_pred)
          mean_squared_errors.append(mean_square_error)
      
      mean_squared_errors = np.array(mean_squared_errors)
      mean = np.mean(mean_squared_errors)
      standard_deviation = np.std(mean_squared_errors)
      
      print('\n')
      print("Below is the mean and standard deviation of " +str(total_mean_squared_errors) + " mean squared errors without normalized data. Total number of epochs for each training is: " +str(epochs) + "\n")
      print("Mean: "+str(mean))
      print("Standard Deviation: "+str(standard_deviation))
Viewing 0 reply threads
  • You must be logged in to reply to this topic.