<jupyter_start><jupyter_text>Regression Models with Keras  Introduction As we discussed in the videos, despite the popularity of more powerful libraries such as PyToch and TensorFlow, they are not easy to use and have a steep learning curve. So, for people who are just starting to learn deep learning, there is no better library to use other than the keras library. Keras is a high-level API for building deep learning models. It has gained favor for its ease of use and syntactic simplicity facilitating fast development. As you will see in this lab and the other labs in this course, building a very complex deep learning network can be achieved with Keras with only few lines of code. You will appreciate Keras even more, once you learn how to build deep models using PyTorch and TensorFlow in the other courses.So, in this lab, you will learn how to use the Keras library to build a regression model.  Table of Contents 1. Download and Clean Dataset  2. Import Keras  3. Build a Neural Network  4. Train and Test the Network     Download and Clean Dataset Let's start by importing the pandas and the Numpy libraries.<jupyter_code>import pandas as pd
import numpy as np<jupyter_output><empty_output><jupyter_text>We will be playing around with the same dataset that we used in the videos.The dataset is about the compressive strength of different samples of concrete based on the volumes of the different materials that were used to make them. Ingredients include:1. Cement2. Blast Furnace Slag3. Fly Ash4. Water5. Superplasticizer6. Coarse Aggregate7. Fine Aggregate Let's download the data and read it into a pandas dataframe.<jupyter_code>concrete_data = pd.read_csv('https://ibm.box.com/shared/static/svl8tu7cmod6tizo6rk0ke4sbuhtpdfx.csv')
concrete_data.head()<jupyter_output><empty_output><jupyter_text>So the first concrete sample has 540 cubic meter of cement, 0 cubic meter of blast furnace slag, 0 cubic meter of fly ash, 162 cubic meter of water, 2.5 cubic meter of superplaticizer, 1040 cubic meter of coarse aggregate, 676 cubic meter of fine aggregate. Such a concrete mix which is 28 days old, has a compressive strength of 79.99 MPa.   Let's check how many data points we have.<jupyter_code>concrete_data.shape<jupyter_output><empty_output><jupyter_text>So, there are approximately 1000 samples to train our model on. Because of the few samples, we have to be careful not to overfit the training data. Let's check the dataset for any missing values.<jupyter_code>concrete_data.describe()
concrete_data.isnull().sum()<jupyter_output><empty_output><jupyter_text>The data looks very clean and is ready to be used to build our model.  Split data into predictors and target<jupyter_code>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<jupyter_output><empty_output><jupyter_text>Let's do a quick sanity check of the predictors and the target dataframes.<jupyter_code>predictors.head()
target.head()<jupyter_output><empty_output><jupyter_text>Finally, the last step is to normalize the data by substracting the mean and dividing by the standard deviation.<jupyter_code>predictors_norm = (predictors - predictors.mean()) / predictors.std()
predictors_norm.head()
n_cols = predictors_norm.shape[1] # number of predictors<jupyter_output><empty_output><jupyter_text>Import Keras Recall from the videos that Keras normally runs on top of a low-level library such as TensorFlow. This means that to be able to use the Keras library, you will have to install TensorFlow first and when you import the Keras library, it will be explicitly displayed what backend was used to install the Keras library. In CC Labs, we used TensorFlow as the backend to install Keras, so it should clearly print that when we import Keras.  Let's go ahead and import the Keras library<jupyter_code>import keras<jupyter_output>Using TensorFlow backend.<jupyter_text>As you can see, the TensorFlow backend was used to install the Keras library. Let's import the rest of the packages from the Keras library that we will need to build our regressoin model.<jupyter_code>from keras.models import Sequential
from keras.layers import Dense<jupyter_output><empty_output><jupyter_text>Build a Neural Network Let's define a function that defines our regression model for us so that we can conveniently call it to create our model.<jupyter_code># define regression model
def regression_model():
    # create model
    model = Sequential()
    model.add(Dense(50, activation='relu', input_shape=(n_cols,)))
    model.add(Dense(50, activation='relu'))
    model.add(Dense(1))
    
    # compile model
    model.compile(optimizer='adam', loss='mean_squared_error')
    return model<jupyter_output><empty_output><jupyter_text>Train and Test the Network<jupyter_code># build the model
model = regression_model()

# fit the model
model.fit(predictors_norm, target, validation_split=0.3, epochs=100, verbose=2)<jupyter_output>Train on 721 samples, validate on 309 samples
Epoch 1/100
 - 0s - loss: 1630.2452 - val_loss: 1135.5810
Epoch 2/100
 - 0s - loss: 1482.0181 - val_loss: 1014.8576
Epoch 3/100
 - 0s - loss: 1260.6026 - val_loss: 841.4739
Epoch 4/100
 - 0s - loss: 952.2047 - val_loss: 623.6588
Epoch 5/100
 - 0s - loss: 620.5551 - val_loss: 410.0146
Epoch 6/100
 - 0s - loss: 368.3221 - val_loss: 270.4695
Epoch 7/100
 - 0s - loss: 264.8196 - val_loss: 203.3870
Epoch 8/100
 - 0s - loss: 234.6755 - val_loss: 180.4489
Epoch 9/100
 - 0s - loss: 219.8861 - val_loss: 168.0982
Epoch 10/100
 - 0s - loss: 206.9611 - val_loss: 162.0971
Epoch 11/100
 - 0s - loss: 198.4312 - val_loss: 157.3207
Epoch 12/100
 - 0s - loss: 190.4224 - val_loss: 152.8289
Epoch 13/100
 - 0s - loss: 183.7115 - val_loss: 150.4358
Epoch 14/100
 - 0s - loss: 178.8201 - val_loss: 148.7234
Epoch 15/100
 - 0s - loss: 173.4976 - val_loss: 147.7538
Epoch 16/100
 - 0s - loss: 169.4321 - val_loss: 148.8126
Epoch 17/100
 - 0s - loss: 165.8842 - val_loss[...]
{'fraction_non_alphanumeric': 0.0719115949200066, 'fraction_numerical': 0.06448952663697839, 'mean_word_length': 4.661998132586368, 'pattern_counts': {'":': 0, '<': 43, '<?xml version=': 0, '>': 43, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '11', 'dir': 'jupyter-structured-clean-dedup', 'id': '331695', 'license': 'MIT', 'n_tokens_mistral': 2176, 'n_tokens_neox': 1840, 'n_words': 757, 'path': 'EdX/IBM DL0101EN - Deep Learning Fundamentals with Keras/DL0101EN-3-1-Regression-with-Keras-py-v1.0.ipynb', 'repo_name': 'helpthx/Path_through_Data_Science_2019', 'size': '6063'}