<jupyter_start><jupyter_text>Peer-graded Assignment: Build a Regression Model in Keras  Part A - Build a baseline model  Use the Keras library to build a neural network with the following:- One hidden layer of 10 nodes, and a ReLU activation function- Use the adam optimizer and the mean squared error as the loss function.1. Randomly split the data into a training and test sets by holding 30% of the data for testing. You can use the train_test_splithelper function from Scikit-learn.2. Train the model on the training data using 50 epochs.3. Evaluate the model on the test data and compute the mean squared error between the predicted concrete strength and the actual concrete strength. You can use the mean_squared_error function from Scikit-learn.4. Repeat steps 1 - 3, 50 times, i.e., create a list of 50 mean squared errors.5. Report the mean and the standard deviation of the mean squared errors.<jupyter_code>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<jupyter_output>Using TensorFlow backend.
/home/jupyterlab/conda/envs/python/lib/python3.6/site-packages/tensorflow/python/framework/dtypes.py:519: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  _np_qint8 = np.dtype([("qint8", np.int8, 1)])
/home/jupyterlab/conda/envs/python/lib/python3.6/site-packages/tensorflow/python/framework/dtypes.py:520: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  _np_quint8 = np.dtype([("quint8", np.uint8, 1)])
/home/jupyterlab/conda/envs/python/lib/python3.6/site-packages/tensorflow/python/framework/dtypes.py:521: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  _np_qint16 = np.dtype([("qint16", np.int16, 1)])
/home/jupyterlab/con[...]<jupyter_text>Perpare data<jupyter_code>concrete_data = pd.read_csv('https://s3-api.us-geo.objectstorage.softlayer.net/cf-courses-data/CognitiveClass/DL0101EN/labs/data/concrete_data.csv')
concrete_data.head()

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()

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

n_cols = predictors_norm.shape[1] # number of predictors
predictors.head()
target.head()
predictors_norm.head()<jupyter_output><empty_output><jupyter_text>Establish model<jupyter_code># define regression model
# One hidden layer of 10 nodes, and a ReLU activation function
# Use the adam optimizer and the mean squared error as the loss function.

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

# build the model
model = regression_model()

a_results= [];

# Repeat steps 1 - 3, 50 times, i.e., create a list of 50 mean squared errors.
for i in range(50):
    # slow, so we add a print to indicate that we are still running
    print(i) 
    # Split the data, holding 30% of the data for testing
    x_train, x_test, y_train, y_test = train_test_split(predictors, target, test_size=0.30, shuffle= True)
    # train 
    model.fit(x_train, y_train, epochs=50, verbose=0)
    # evaluate
    score = model.evaluate(x_test, y_test, verbose=0)
    a_results.append(score)

a_results
# Report the mean and the standard deviation of the mean squared errors.
a_mean = np.mean( a_results )
a_stddev = np.std( a_results )
print( '-'*80 )
print( f' For part A, mean is {a_mean} , stddev is {a_stddev} ' )
print( '-'*80 )<jupyter_output>--------------------------------------------------------------------------------
 For part A, mean is 58.916268356718284 , stddev is 39.24250209779374 
--------------------------------------------------------------------------------<jupyter_text>Part B - Normalize the dataRepeat Part A but use a **normalized** version of the data. Recall that one way to normalize the data is by subtracting the mean from the individual predictors and dividing by the standard deviation.How does the mean of the mean squared errors compare to that from Step A?<jupyter_code>b_results= [];

# Repeat steps 1 - 3, 50 times, i.e., create a list of 50 mean squared errors.
for i in range(50):
    # slow, so we add a print to indicate that we are still running
    print(i) 
    # Split the data, holding 30% of the data for testing
    x_train, x_test, y_train, y_test = train_test_split(predictors_norm, target, test_size=0.30, shuffle= True)
    # train 
    model.fit(x_train, y_train, epochs=50, verbose=0)
    # evaluate
    score = model.evaluate(x_test, y_test, verbose=0)
    b_results.append(score)

b_results
# Report the mean and the standard deviation of the mean squared errors.
b_mean = np.mean( b_results )
b_stddev = np.std( b_results )
print( '-'*80 )
print( f' For part B, mean is {b_mean} , stddev is {b_stddev} ' )
print( '-'*80 )<jupyter_output>--------------------------------------------------------------------------------
 For part B, mean is 44.99501803574053 , stddev is 46.59437401248902 
--------------------------------------------------------------------------------<jupyter_text>Part C - Increate the number of epochsRepeat Part B but use **100 epochs** this time for training.How does the mean of the mean squared errors compare to that from Step B?<jupyter_code>c_results= [];

# Repeat steps 1 - 3, 50 times, i.e., create a list of 50 mean squared errors.
for i in range(50):
    # slow, so we add a print to indicate that we are still running
    print(i)
    # Split the data, holding 30% of the data for testing
    x_train, x_test, y_train, y_test = train_test_split(predictors_norm, target, test_size=0.30, shuffle= True)
    # train 
    model.fit(x_train, y_train, epochs=100, verbose=0)
    # evaluate
    score = model.evaluate(x_test, y_test, verbose=0)
    c_results.append(score)

c_results
# Report the mean and the standard deviation of the mean squared errors.
c_mean = np.mean( c_results )
c_stddev = np.std( c_results )
print( '-'*80 )
print( f' For part C, mean is {b_mean} , stddev is {b_stddev} ' )
print( '-'*80 )<jupyter_output>--------------------------------------------------------------------------------
 For part C, mean is 44.99501803574053 , stddev is 46.59437401248902 
--------------------------------------------------------------------------------<jupyter_text>Part D. Increase the number of hidden layersRepeat part B but use a neural network with the following instead:- **Three hidden layers***, each of 10 nodes and ReLU activation function.How does the mean of the mean squared errors compare to that from Step B?<jupyter_code>def d_regression_model():
    # create model
    model = Sequential()
    # Three hidden layers
    model.add(Dense(10, activation='relu', input_shape=(n_cols,)))
    model.add(Dense(10, activation='relu'))
    model.add(Dense(10, activation='relu'))
    model.add(Dense(1))
    
    # compile model
    model.compile(optimizer='adam', loss='mean_squared_error')
    return model

d_model = d_regression_model()

d_results= [];

# Repeat steps 1 - 3, 50 times, i.e., create a list of 50 mean squared errors.
for i in range(50):
    # slow, so we add a print to indicate that we are still running
    print(i)
    # Split the data, holding 30% of the data for testing
    x_train, x_test, y_train, y_test = train_test_split(predictors_norm, target, test_size=0.30, shuffle= True)
    # train 
    model.fit(x_train, y_train, epochs=50, verbose=0)
    # evaluate
    score = model.evaluate(x_test, y_test, verbose=0)
    d_results.append(score)

d_results
# Report the mean and the standard deviation of the mean squared errors.
d_mean = np.mean( d_results )
d_stddev = np.std( d_results )
print( '-'*80 )
print( f' For part D, mean is {d_mean} , stddev is {d_stddev} ' )
print( '-'*80 )<jupyter_output>--------------------------------------------------------------------------------
 For part D, mean is 22.93449048607095 , stddev is 2.694321805291805 
--------------------------------------------------------------------------------
{'fraction_non_alphanumeric': 0.16747854326142425, 'fraction_numerical': 0.03433078172117838, 'mean_word_length': 3.0426629160806375, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 20, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '515703', 'license': 'MIT', 'n_tokens_mistral': 2883, 'n_tokens_neox': 2620, 'n_words': 925, 'path': 'Coursera/IBM Python 01/Course03/KerasAssignment.ipynb', 'repo_name': 'brianshen1990/KeepLearning', 'size': '8622'}