<jupyter_start><jupyter_text><jupyter_code><jupyter_output><empty_output><jupyter_text>Import required libraries<jupyter_code>import pandas as pd
import numpy as np<jupyter_output><empty_output><jupyter_text>Load source 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()<jupyter_output><empty_output><jupyter_text>Check for missing data<jupyter_code>concrete_data.isna().any()<jupyter_output><empty_output><jupyter_text>Separate target and predictors (pop in the strength column from concrete_data)<jupyter_code>target = concrete_data.pop('Strength')
target.head()<jupyter_output><empty_output><jupyter_text>Check that concrete_data does not contain strength information<jupyter_code>concrete_data.head()<jupyter_output><empty_output><jupyter_text>Build the model with * Three hidden layers, each of 10 nodes and a ReLU activation function* Use the adam optimizer and the mean squared error as the loss function<jupyter_code>from keras.models import Sequential
from keras.layers import Dense

def build_model(input_shape):
    model = Sequential()
    model.add(Dense(10, activation='relu', input_shape=(input_shape,)))
    model.add(Dense(10, activation='relu'))
    model.add(Dense(10, activation='relu'))
    model.add(Dense(1))

    model.compile(loss='mean_squared_error',
                 optimizer='adam')
    model.summary()
    return model<jupyter_output><empty_output><jupyter_text>1. Randomly split the data into a training and test sets by holding 30% of the data for testing.<jupyter_code>from sklearn.model_selection import train_test_split

X_train, X_test, y_train, y_test = train_test_split(concrete_data, target, test_size=0.30)
print(X_train.shape, X_test.shape, y_train.shape, y_test.shape)<jupyter_output>(721, 8) (309, 8) (721,) (309,)<jupyter_text>Normalize the train data<jupyter_code>train_means = X_train.mean()
train_stds = X_train.std()
X_train_norm = (X_train - train_means) / train_stds
X_test_norm = (X_test - train_means) / train_stds
X_train_norm.head()
X_test_norm.head()<jupyter_output><empty_output><jupyter_text>Build model<jupyter_code>concrete_model = build_model(X_train_norm.shape[1])<jupyter_output>_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
dense_1 (Dense)              (None, 10)                90        
_________________________________________________________________
dense_2 (Dense)              (None, 10)                110       
_________________________________________________________________
dense_3 (Dense)              (None, 10)                110       
_________________________________________________________________
dense_4 (Dense)              (None, 1)                 11        
=================================================================
Total params: 321
Trainable params: 321
Non-trainable params: 0
_________________________________________________________________<jupyter_text>2. Train the model on the training data using 50 epochs<jupyter_code>concrete_model.fit(X_train_norm, y_train, epochs=50, verbose=0)<jupyter_output><empty_output><jupyter_text>3. Evaluate the model on the test data and compute the mean squared error between the predicted concrete strength and the actual concrete strength Use the model to predict strength<jupyter_code>y_hat = concrete_model.predict(X_test_norm)
# print(y_hat)
from sklearn.metrics import mean_squared_error
mse = mean_squared_error(y_test, y_hat)
mse<jupyter_output><empty_output><jupyter_text>4. Repeat steps 1 - 3, 50 times<jupyter_code>mse_list = list()
for r in range(0, 50):
    # 1
    X_train, X_test, y_train, y_test = train_test_split(concrete_data, target, test_size=0.30)
    train_means = X_train.mean()
    train_stds = X_train.std()
    X_train_norm = (X_train - train_means) / train_stds
    X_test_norm = (X_test - train_means) / train_stds
    # 2 
    concrete_model.fit(X_train_norm, y_train, epochs=50, verbose=0)
    # 3
    y_hat = concrete_model.predict(X_test_norm)
    mse = mean_squared_error(y_test, y_hat)
    mse_list.append(mse)<jupyter_output><empty_output><jupyter_text>5. Report the mean and the standard deviation of the mean squared errors.<jupyter_code>print("Mean of the 50 runs = {} /// Standard deviation = {}".format(sum(mse_list)/len(mse_list), np.std(mse_list)))<jupyter_output>Mean of the 50 runs = 38.621805991374515 /// Standard deviation = 22.676398014814378
{'fraction_non_alphanumeric': 0.11988747024453582, 'fraction_numerical': 0.027266825362475655, 'mean_word_length': 5.565340909090909, 'pattern_counts': {'":': 0, '<': 54, '<?xml version=': 0, '>': 54, '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': '14', 'dir': 'jupyter-structured-clean-dedup', 'id': '169545', 'license': 'MIT', 'n_tokens_mistral': 1586, 'n_tokens_neox': 1451, 'n_words': 302, 'path': 'GH_DL010EN_5_4.ipynb', 'repo_name': 'veeransr/MAP2020', 'size': '4621'}