Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save XingLiangLondon/9a94073d8313c43421acaf709e0fd806 to your computer and use it in GitHub Desktop.
Save XingLiangLondon/9a94073d8313c43421acaf709e0fd806 to your computer and use it in GitHub Desktop.
Created on Skills Network Labs
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"button": false,
"deletable": true,
"new_sheet": false,
"run_control": {
"read_only": false
}
},
"source": [
"<a href=\"https://cognitiveclass.ai\"><img src = \"https://ibm.box.com/shared/static/9gegpsmnsoo25ikkbl4qzlvlyjbgxs5x.png\" width = 400> </a>\n",
"\n",
"<h1 align=center><font size = 5>Regression Models with Keras</font></h1>"
]
},
{
"cell_type": "markdown",
"metadata": {
"button": false,
"deletable": true,
"new_sheet": false,
"run_control": {
"read_only": false
}
},
"source": [
"## Introduction"
]
},
{
"cell_type": "markdown",
"metadata": {
"button": false,
"deletable": true,
"new_sheet": false,
"run_control": {
"read_only": false
}
},
"source": [
"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. \n",
"\n",
"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.\n",
"\n",
"So, in this lab, you will learn how to use the Keras library to build a regression model."
]
},
{
"cell_type": "markdown",
"metadata": {
"button": false,
"deletable": true,
"new_sheet": false,
"run_control": {
"read_only": false
}
},
"source": [
"## Table of Contents\n",
"\n",
"<div class=\"alert alert-block alert-info\" style=\"margin-top: 20px\">\n",
"\n",
"<font size = 3> \n",
"1. <a href=\"#item1\">Download and Clean Dataset</a> \n",
"2. <a href=\"#item2\">Import Keras</a> \n",
"3. <a href=\"#item3\">Build a Neural Network</a> \n",
"4. <a href=\"#item4\">Train and Test the Network</a> \n",
"</font>\n",
"</div>"
]
},
{
"cell_type": "markdown",
"metadata": {
"button": false,
"deletable": true,
"new_sheet": false,
"run_control": {
"read_only": false
}
},
"source": [
"<a id=\"item1\"></a>"
]
},
{
"cell_type": "markdown",
"metadata": {
"button": false,
"deletable": true,
"new_sheet": false,
"run_control": {
"read_only": false
}
},
"source": [
"## Download and Clean Dataset"
]
},
{
"cell_type": "markdown",
"metadata": {
"button": false,
"deletable": true,
"new_sheet": false,
"run_control": {
"read_only": false
}
},
"source": [
"Let's start by importing the <em>pandas</em> and the Numpy libraries."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"button": false,
"deletable": true,
"new_sheet": false,
"run_control": {
"read_only": false
}
},
"outputs": [],
"source": [
"import pandas as pd\n",
"import numpy as np"
]
},
{
"cell_type": "markdown",
"metadata": {
"button": false,
"deletable": true,
"new_sheet": false,
"run_control": {
"read_only": false
}
},
"source": [
"We will be playing around with the same dataset that we used in the videos.\n",
"\n",
"<strong>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:</strong>\n",
"\n",
"<strong>1. Cement</strong>\n",
"\n",
"<strong>2. Blast Furnace Slag</strong>\n",
"\n",
"<strong>3. Fly Ash</strong>\n",
"\n",
"<strong>4. Water</strong>\n",
"\n",
"<strong>5. Superplasticizer</strong>\n",
"\n",
"<strong>6. Coarse Aggregate</strong>\n",
"\n",
"<strong>7. Fine Aggregate</strong>"
]
},
{
"cell_type": "markdown",
"metadata": {
"button": false,
"deletable": true,
"new_sheet": false,
"run_control": {
"read_only": false
}
},
"source": [
"Let's download the data and read it into a <em>pandas</em> dataframe."
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"button": false,
"deletable": true,
"new_sheet": false,
"run_control": {
"read_only": false
}
},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>Cement</th>\n",
" <th>Blast Furnace Slag</th>\n",
" <th>Fly Ash</th>\n",
" <th>Water</th>\n",
" <th>Superplasticizer</th>\n",
" <th>Coarse Aggregate</th>\n",
" <th>Fine Aggregate</th>\n",
" <th>Age</th>\n",
" <th>Strength</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>540.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>162.0</td>\n",
" <td>2.5</td>\n",
" <td>1040.0</td>\n",
" <td>676.0</td>\n",
" <td>28</td>\n",
" <td>79.99</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>540.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>162.0</td>\n",
" <td>2.5</td>\n",
" <td>1055.0</td>\n",
" <td>676.0</td>\n",
" <td>28</td>\n",
" <td>61.89</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>332.5</td>\n",
" <td>142.5</td>\n",
" <td>0.0</td>\n",
" <td>228.0</td>\n",
" <td>0.0</td>\n",
" <td>932.0</td>\n",
" <td>594.0</td>\n",
" <td>270</td>\n",
" <td>40.27</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>332.5</td>\n",
" <td>142.5</td>\n",
" <td>0.0</td>\n",
" <td>228.0</td>\n",
" <td>0.0</td>\n",
" <td>932.0</td>\n",
" <td>594.0</td>\n",
" <td>365</td>\n",
" <td>41.05</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>198.6</td>\n",
" <td>132.4</td>\n",
" <td>0.0</td>\n",
" <td>192.0</td>\n",
" <td>0.0</td>\n",
" <td>978.4</td>\n",
" <td>825.5</td>\n",
" <td>360</td>\n",
" <td>44.30</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" Cement Blast Furnace Slag Fly Ash Water Superplasticizer \\\n",
"0 540.0 0.0 0.0 162.0 2.5 \n",
"1 540.0 0.0 0.0 162.0 2.5 \n",
"2 332.5 142.5 0.0 228.0 0.0 \n",
"3 332.5 142.5 0.0 228.0 0.0 \n",
"4 198.6 132.4 0.0 192.0 0.0 \n",
"\n",
" Coarse Aggregate Fine Aggregate Age Strength \n",
"0 1040.0 676.0 28 79.99 \n",
"1 1055.0 676.0 28 61.89 \n",
"2 932.0 594.0 270 40.27 \n",
"3 932.0 594.0 365 41.05 \n",
"4 978.4 825.5 360 44.30 "
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"concrete_data = pd.read_csv('https://ibm.box.com/shared/static/svl8tu7cmod6tizo6rk0ke4sbuhtpdfx.csv')\n",
"concrete_data.head()"
]
},
{
"cell_type": "markdown",
"metadata": {
"button": false,
"deletable": true,
"new_sheet": false,
"run_control": {
"read_only": false
}
},
"source": [
"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. "
]
},
{
"cell_type": "markdown",
"metadata": {
"button": false,
"deletable": true,
"new_sheet": false,
"run_control": {
"read_only": false
}
},
"source": [
"#### Let's check how many data points we have."
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"button": false,
"deletable": true,
"new_sheet": false,
"run_control": {
"read_only": false
}
},
"outputs": [
{
"data": {
"text/plain": [
"(1030, 9)"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"concrete_data.shape"
]
},
{
"cell_type": "markdown",
"metadata": {
"button": false,
"deletable": true,
"new_sheet": false,
"run_control": {
"read_only": false
}
},
"source": [
"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."
]
},
{
"cell_type": "markdown",
"metadata": {
"button": false,
"deletable": true,
"new_sheet": false,
"run_control": {
"read_only": false
}
},
"source": [
"Let's check the dataset for any missing values."
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"button": false,
"deletable": true,
"new_sheet": false,
"run_control": {
"read_only": false
}
},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>Cement</th>\n",
" <th>Blast Furnace Slag</th>\n",
" <th>Fly Ash</th>\n",
" <th>Water</th>\n",
" <th>Superplasticizer</th>\n",
" <th>Coarse Aggregate</th>\n",
" <th>Fine Aggregate</th>\n",
" <th>Age</th>\n",
" <th>Strength</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>count</th>\n",
" <td>1030.000000</td>\n",
" <td>1030.000000</td>\n",
" <td>1030.000000</td>\n",
" <td>1030.000000</td>\n",
" <td>1030.000000</td>\n",
" <td>1030.000000</td>\n",
" <td>1030.000000</td>\n",
" <td>1030.000000</td>\n",
" <td>1030.000000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>mean</th>\n",
" <td>281.167864</td>\n",
" <td>73.895825</td>\n",
" <td>54.188350</td>\n",
" <td>181.567282</td>\n",
" <td>6.204660</td>\n",
" <td>972.918932</td>\n",
" <td>773.580485</td>\n",
" <td>45.662136</td>\n",
" <td>35.817961</td>\n",
" </tr>\n",
" <tr>\n",
" <th>std</th>\n",
" <td>104.506364</td>\n",
" <td>86.279342</td>\n",
" <td>63.997004</td>\n",
" <td>21.354219</td>\n",
" <td>5.973841</td>\n",
" <td>77.753954</td>\n",
" <td>80.175980</td>\n",
" <td>63.169912</td>\n",
" <td>16.705742</td>\n",
" </tr>\n",
" <tr>\n",
" <th>min</th>\n",
" <td>102.000000</td>\n",
" <td>0.000000</td>\n",
" <td>0.000000</td>\n",
" <td>121.800000</td>\n",
" <td>0.000000</td>\n",
" <td>801.000000</td>\n",
" <td>594.000000</td>\n",
" <td>1.000000</td>\n",
" <td>2.330000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>25%</th>\n",
" <td>192.375000</td>\n",
" <td>0.000000</td>\n",
" <td>0.000000</td>\n",
" <td>164.900000</td>\n",
" <td>0.000000</td>\n",
" <td>932.000000</td>\n",
" <td>730.950000</td>\n",
" <td>7.000000</td>\n",
" <td>23.710000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>50%</th>\n",
" <td>272.900000</td>\n",
" <td>22.000000</td>\n",
" <td>0.000000</td>\n",
" <td>185.000000</td>\n",
" <td>6.400000</td>\n",
" <td>968.000000</td>\n",
" <td>779.500000</td>\n",
" <td>28.000000</td>\n",
" <td>34.445000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>75%</th>\n",
" <td>350.000000</td>\n",
" <td>142.950000</td>\n",
" <td>118.300000</td>\n",
" <td>192.000000</td>\n",
" <td>10.200000</td>\n",
" <td>1029.400000</td>\n",
" <td>824.000000</td>\n",
" <td>56.000000</td>\n",
" <td>46.135000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>max</th>\n",
" <td>540.000000</td>\n",
" <td>359.400000</td>\n",
" <td>200.100000</td>\n",
" <td>247.000000</td>\n",
" <td>32.200000</td>\n",
" <td>1145.000000</td>\n",
" <td>992.600000</td>\n",
" <td>365.000000</td>\n",
" <td>82.600000</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" Cement Blast Furnace Slag Fly Ash Water \\\n",
"count 1030.000000 1030.000000 1030.000000 1030.000000 \n",
"mean 281.167864 73.895825 54.188350 181.567282 \n",
"std 104.506364 86.279342 63.997004 21.354219 \n",
"min 102.000000 0.000000 0.000000 121.800000 \n",
"25% 192.375000 0.000000 0.000000 164.900000 \n",
"50% 272.900000 22.000000 0.000000 185.000000 \n",
"75% 350.000000 142.950000 118.300000 192.000000 \n",
"max 540.000000 359.400000 200.100000 247.000000 \n",
"\n",
" Superplasticizer Coarse Aggregate Fine Aggregate Age \\\n",
"count 1030.000000 1030.000000 1030.000000 1030.000000 \n",
"mean 6.204660 972.918932 773.580485 45.662136 \n",
"std 5.973841 77.753954 80.175980 63.169912 \n",
"min 0.000000 801.000000 594.000000 1.000000 \n",
"25% 0.000000 932.000000 730.950000 7.000000 \n",
"50% 6.400000 968.000000 779.500000 28.000000 \n",
"75% 10.200000 1029.400000 824.000000 56.000000 \n",
"max 32.200000 1145.000000 992.600000 365.000000 \n",
"\n",
" Strength \n",
"count 1030.000000 \n",
"mean 35.817961 \n",
"std 16.705742 \n",
"min 2.330000 \n",
"25% 23.710000 \n",
"50% 34.445000 \n",
"75% 46.135000 \n",
"max 82.600000 "
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"concrete_data.describe()"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"button": false,
"deletable": true,
"new_sheet": false,
"run_control": {
"read_only": false
}
},
"outputs": [
{
"data": {
"text/plain": [
"Cement 0\n",
"Blast Furnace Slag 0\n",
"Fly Ash 0\n",
"Water 0\n",
"Superplasticizer 0\n",
"Coarse Aggregate 0\n",
"Fine Aggregate 0\n",
"Age 0\n",
"Strength 0\n",
"dtype: int64"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"concrete_data.isnull().sum()"
]
},
{
"cell_type": "markdown",
"metadata": {
"button": false,
"deletable": true,
"new_sheet": false,
"run_control": {
"read_only": false
}
},
"source": [
"The data looks very clean and is ready to be used to build our model."
]
},
{
"cell_type": "markdown",
"metadata": {
"button": false,
"deletable": true,
"new_sheet": false,
"run_control": {
"read_only": false
}
},
"source": [
"#### Split data into predictors and target"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"button": false,
"deletable": true,
"new_sheet": false,
"run_control": {
"read_only": false
}
},
"outputs": [],
"source": [
"concrete_data_columns = concrete_data.columns\n",
"\n",
"predictors = concrete_data[concrete_data_columns[concrete_data_columns != 'Strength']] # all columns except Strength\n",
"target = concrete_data['Strength'] # Strength column"
]
},
{
"cell_type": "markdown",
"metadata": {
"button": false,
"deletable": true,
"new_sheet": false,
"run_control": {
"read_only": false
}
},
"source": [
"<a id=\"item2\"></a>"
]
},
{
"cell_type": "markdown",
"metadata": {
"button": false,
"deletable": true,
"new_sheet": false,
"run_control": {
"read_only": false
}
},
"source": [
"Let's do a quick sanity check of the predictors and the target dataframes."
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"button": false,
"deletable": true,
"new_sheet": false,
"run_control": {
"read_only": false
}
},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>Cement</th>\n",
" <th>Blast Furnace Slag</th>\n",
" <th>Fly Ash</th>\n",
" <th>Water</th>\n",
" <th>Superplasticizer</th>\n",
" <th>Coarse Aggregate</th>\n",
" <th>Fine Aggregate</th>\n",
" <th>Age</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>540.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>162.0</td>\n",
" <td>2.5</td>\n",
" <td>1040.0</td>\n",
" <td>676.0</td>\n",
" <td>28</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>540.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>162.0</td>\n",
" <td>2.5</td>\n",
" <td>1055.0</td>\n",
" <td>676.0</td>\n",
" <td>28</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>332.5</td>\n",
" <td>142.5</td>\n",
" <td>0.0</td>\n",
" <td>228.0</td>\n",
" <td>0.0</td>\n",
" <td>932.0</td>\n",
" <td>594.0</td>\n",
" <td>270</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>332.5</td>\n",
" <td>142.5</td>\n",
" <td>0.0</td>\n",
" <td>228.0</td>\n",
" <td>0.0</td>\n",
" <td>932.0</td>\n",
" <td>594.0</td>\n",
" <td>365</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>198.6</td>\n",
" <td>132.4</td>\n",
" <td>0.0</td>\n",
" <td>192.0</td>\n",
" <td>0.0</td>\n",
" <td>978.4</td>\n",
" <td>825.5</td>\n",
" <td>360</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" Cement Blast Furnace Slag Fly Ash Water Superplasticizer \\\n",
"0 540.0 0.0 0.0 162.0 2.5 \n",
"1 540.0 0.0 0.0 162.0 2.5 \n",
"2 332.5 142.5 0.0 228.0 0.0 \n",
"3 332.5 142.5 0.0 228.0 0.0 \n",
"4 198.6 132.4 0.0 192.0 0.0 \n",
"\n",
" Coarse Aggregate Fine Aggregate Age \n",
"0 1040.0 676.0 28 \n",
"1 1055.0 676.0 28 \n",
"2 932.0 594.0 270 \n",
"3 932.0 594.0 365 \n",
"4 978.4 825.5 360 "
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"predictors.head()"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"button": false,
"deletable": true,
"new_sheet": false,
"run_control": {
"read_only": false
}
},
"outputs": [
{
"data": {
"text/plain": [
"0 79.99\n",
"1 61.89\n",
"2 40.27\n",
"3 41.05\n",
"4 44.30\n",
"Name: Strength, dtype: float64"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"target.head()"
]
},
{
"cell_type": "markdown",
"metadata": {
"button": false,
"deletable": true,
"new_sheet": false,
"run_control": {
"read_only": false
}
},
"source": [
"Finally, the last step is to normalize the data by substracting the mean and dividing by the standard deviation."
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"button": false,
"deletable": true,
"new_sheet": false,
"run_control": {
"read_only": false
}
},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>Cement</th>\n",
" <th>Blast Furnace Slag</th>\n",
" <th>Fly Ash</th>\n",
" <th>Water</th>\n",
" <th>Superplasticizer</th>\n",
" <th>Coarse Aggregate</th>\n",
" <th>Fine Aggregate</th>\n",
" <th>Age</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>2.476712</td>\n",
" <td>-0.856472</td>\n",
" <td>-0.846733</td>\n",
" <td>-0.916319</td>\n",
" <td>-0.620147</td>\n",
" <td>0.862735</td>\n",
" <td>-1.217079</td>\n",
" <td>-0.279597</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>2.476712</td>\n",
" <td>-0.856472</td>\n",
" <td>-0.846733</td>\n",
" <td>-0.916319</td>\n",
" <td>-0.620147</td>\n",
" <td>1.055651</td>\n",
" <td>-1.217079</td>\n",
" <td>-0.279597</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>0.491187</td>\n",
" <td>0.795140</td>\n",
" <td>-0.846733</td>\n",
" <td>2.174405</td>\n",
" <td>-1.038638</td>\n",
" <td>-0.526262</td>\n",
" <td>-2.239829</td>\n",
" <td>3.551340</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>0.491187</td>\n",
" <td>0.795140</td>\n",
" <td>-0.846733</td>\n",
" <td>2.174405</td>\n",
" <td>-1.038638</td>\n",
" <td>-0.526262</td>\n",
" <td>-2.239829</td>\n",
" <td>5.055221</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>-0.790075</td>\n",
" <td>0.678079</td>\n",
" <td>-0.846733</td>\n",
" <td>0.488555</td>\n",
" <td>-1.038638</td>\n",
" <td>0.070492</td>\n",
" <td>0.647569</td>\n",
" <td>4.976069</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" Cement Blast Furnace Slag Fly Ash Water Superplasticizer \\\n",
"0 2.476712 -0.856472 -0.846733 -0.916319 -0.620147 \n",
"1 2.476712 -0.856472 -0.846733 -0.916319 -0.620147 \n",
"2 0.491187 0.795140 -0.846733 2.174405 -1.038638 \n",
"3 0.491187 0.795140 -0.846733 2.174405 -1.038638 \n",
"4 -0.790075 0.678079 -0.846733 0.488555 -1.038638 \n",
"\n",
" Coarse Aggregate Fine Aggregate Age \n",
"0 0.862735 -1.217079 -0.279597 \n",
"1 1.055651 -1.217079 -0.279597 \n",
"2 -0.526262 -2.239829 3.551340 \n",
"3 -0.526262 -2.239829 5.055221 \n",
"4 0.070492 0.647569 4.976069 "
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"predictors_norm = (predictors - predictors.mean()) / predictors.std()\n",
"predictors_norm.head()"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"button": false,
"deletable": true,
"new_sheet": false,
"run_control": {
"read_only": false
}
},
"outputs": [],
"source": [
"n_cols = predictors_norm.shape[1] # number of predictors"
]
},
{
"cell_type": "markdown",
"metadata": {
"button": false,
"deletable": true,
"new_sheet": false,
"run_control": {
"read_only": false
}
},
"source": [
"<a id=\"item1\"></a>"
]
},
{
"cell_type": "markdown",
"metadata": {
"button": false,
"deletable": true,
"new_sheet": false,
"run_control": {
"read_only": false
}
},
"source": [
"## Import Keras"
]
},
{
"cell_type": "markdown",
"metadata": {
"button": false,
"deletable": true,
"new_sheet": false,
"run_control": {
"read_only": false
}
},
"source": [
"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."
]
},
{
"cell_type": "markdown",
"metadata": {
"button": false,
"deletable": true,
"new_sheet": false,
"run_control": {
"read_only": false
}
},
"source": [
"#### Let's go ahead and import the Keras library"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"button": false,
"deletable": true,
"new_sheet": false,
"run_control": {
"read_only": false
}
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"Using TensorFlow backend.\n"
]
}
],
"source": [
"import keras"
]
},
{
"cell_type": "markdown",
"metadata": {
"button": false,
"deletable": true,
"new_sheet": false,
"run_control": {
"read_only": false
}
},
"source": [
"As you can see, the TensorFlow backend was used to install the Keras library."
]
},
{
"cell_type": "markdown",
"metadata": {
"button": false,
"deletable": true,
"new_sheet": false,
"run_control": {
"read_only": false
}
},
"source": [
"Let's import the rest of the packages from the Keras library that we will need to build our regressoin model."
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {
"button": false,
"deletable": true,
"new_sheet": false,
"run_control": {
"read_only": false
}
},
"outputs": [],
"source": [
"from keras.models import Sequential\n",
"from keras.layers import Dense"
]
},
{
"cell_type": "markdown",
"metadata": {
"button": false,
"deletable": true,
"new_sheet": false,
"run_control": {
"read_only": false
}
},
"source": [
"## Build a Neural Network"
]
},
{
"cell_type": "markdown",
"metadata": {
"button": false,
"deletable": true,
"new_sheet": false,
"run_control": {
"read_only": false
}
},
"source": [
"Let's define a function that defines our regression model for us so that we can conveniently call it to create our model."
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {
"button": false,
"deletable": true,
"new_sheet": false,
"run_control": {
"read_only": false
}
},
"outputs": [],
"source": [
"# define regression model\n",
"def regression_model():\n",
" # create model\n",
" model = Sequential()\n",
" model.add(Dense(50, activation='relu', input_shape=(n_cols,)))\n",
" model.add(Dense(50, activation='relu'))\n",
" model.add(Dense(1))\n",
" \n",
" # compile model\n",
" model.compile(optimizer='adam', loss='mean_squared_error')\n",
" return model"
]
},
{
"cell_type": "markdown",
"metadata": {
"button": false,
"deletable": true,
"new_sheet": false,
"run_control": {
"read_only": false
}
},
"source": [
"<a id=\"item4\"></a>"
]
},
{
"cell_type": "markdown",
"metadata": {
"button": false,
"deletable": true,
"new_sheet": false,
"run_control": {
"read_only": false
}
},
"source": [
"## Train and Test the Network"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {
"button": false,
"deletable": true,
"new_sheet": false,
"run_control": {
"read_only": false
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Train on 721 samples, validate on 309 samples\n",
"Epoch 1/100\n",
" - 0s - loss: 1612.2530 - val_loss: 1098.4232\n",
"Epoch 2/100\n",
" - 0s - loss: 1464.5335 - val_loss: 952.6200\n",
"Epoch 3/100\n",
" - 0s - loss: 1241.3266 - val_loss: 749.3463\n",
"Epoch 4/100\n",
" - 0s - loss: 929.5631 - val_loss: 516.4680\n",
"Epoch 5/100\n",
" - 0s - loss: 595.3714 - val_loss: 322.8164\n",
"Epoch 6/100\n",
" - 0s - loss: 345.7127 - val_loss: 218.1733\n",
"Epoch 7/100\n",
" - 0s - loss: 241.9237 - val_loss: 186.9792\n",
"Epoch 8/100\n",
" - 0s - loss: 214.0006 - val_loss: 177.1079\n",
"Epoch 9/100\n",
" - 0s - loss: 201.9990 - val_loss: 172.7512\n",
"Epoch 10/100\n",
" - 0s - loss: 193.2856 - val_loss: 169.9733\n",
"Epoch 11/100\n",
" - 0s - loss: 187.7408 - val_loss: 165.3970\n",
"Epoch 12/100\n",
" - 0s - loss: 181.9701 - val_loss: 168.3767\n",
"Epoch 13/100\n",
" - 0s - loss: 177.3558 - val_loss: 167.7300\n",
"Epoch 14/100\n",
" - 0s - loss: 172.9343 - val_loss: 166.0738\n",
"Epoch 15/100\n",
" - 0s - loss: 169.7046 - val_loss: 166.3891\n",
"Epoch 16/100\n",
" - 0s - loss: 167.0230 - val_loss: 166.4667\n",
"Epoch 17/100\n",
" - 0s - loss: 163.9813 - val_loss: 165.2622\n",
"Epoch 18/100\n",
" - 0s - loss: 161.3216 - val_loss: 165.7875\n",
"Epoch 19/100\n",
" - 0s - loss: 159.1548 - val_loss: 166.5582\n",
"Epoch 20/100\n",
" - 0s - loss: 156.9407 - val_loss: 162.4378\n",
"Epoch 21/100\n",
" - 0s - loss: 154.9361 - val_loss: 164.9678\n",
"Epoch 22/100\n",
" - 0s - loss: 152.9178 - val_loss: 164.2015\n",
"Epoch 23/100\n",
" - 0s - loss: 152.7401 - val_loss: 166.5489\n",
"Epoch 24/100\n",
" - 0s - loss: 149.4660 - val_loss: 165.5534\n",
"Epoch 25/100\n",
" - 0s - loss: 148.0674 - val_loss: 164.3353\n",
"Epoch 26/100\n",
" - 0s - loss: 145.9749 - val_loss: 164.2341\n",
"Epoch 27/100\n",
" - 0s - loss: 144.5105 - val_loss: 166.3867\n",
"Epoch 28/100\n",
" - 0s - loss: 143.0748 - val_loss: 167.1426\n",
"Epoch 29/100\n",
" - 0s - loss: 141.4705 - val_loss: 166.8514\n",
"Epoch 30/100\n",
" - 0s - loss: 140.3103 - val_loss: 167.4628\n",
"Epoch 31/100\n",
" - 0s - loss: 138.9434 - val_loss: 167.2527\n",
"Epoch 32/100\n",
" - 0s - loss: 138.1197 - val_loss: 168.2040\n",
"Epoch 33/100\n",
" - 0s - loss: 136.9442 - val_loss: 167.6996\n",
"Epoch 34/100\n",
" - 0s - loss: 135.3494 - val_loss: 166.9168\n",
"Epoch 35/100\n",
" - 0s - loss: 134.7190 - val_loss: 168.5217\n",
"Epoch 36/100\n",
" - 0s - loss: 133.5853 - val_loss: 167.0278\n",
"Epoch 37/100\n",
" - 0s - loss: 132.4575 - val_loss: 169.5053\n",
"Epoch 38/100\n",
" - 0s - loss: 131.0187 - val_loss: 170.1467\n",
"Epoch 39/100\n",
" - 0s - loss: 130.2394 - val_loss: 167.8656\n",
"Epoch 40/100\n",
" - 0s - loss: 129.1054 - val_loss: 168.4096\n",
"Epoch 41/100\n",
" - 0s - loss: 127.8292 - val_loss: 168.1074\n",
"Epoch 42/100\n",
" - 0s - loss: 126.7262 - val_loss: 169.9109\n",
"Epoch 43/100\n",
" - 0s - loss: 125.7834 - val_loss: 166.2336\n",
"Epoch 44/100\n",
" - 0s - loss: 124.5426 - val_loss: 169.7456\n",
"Epoch 45/100\n",
" - 0s - loss: 123.3714 - val_loss: 164.9693\n",
"Epoch 46/100\n",
" - 0s - loss: 121.5866 - val_loss: 168.9084\n",
"Epoch 47/100\n",
" - 0s - loss: 121.0801 - val_loss: 165.4620\n",
"Epoch 48/100\n",
" - 0s - loss: 118.9527 - val_loss: 164.8855\n",
"Epoch 49/100\n",
" - 0s - loss: 117.6361 - val_loss: 167.0626\n",
"Epoch 50/100\n",
" - 0s - loss: 117.0560 - val_loss: 169.9320\n",
"Epoch 51/100\n",
" - 0s - loss: 116.0361 - val_loss: 161.5849\n",
"Epoch 52/100\n",
" - 0s - loss: 113.4627 - val_loss: 170.1854\n",
"Epoch 53/100\n",
" - 0s - loss: 112.3316 - val_loss: 163.8026\n",
"Epoch 54/100\n",
" - 0s - loss: 110.9526 - val_loss: 164.4559\n",
"Epoch 55/100\n",
" - 0s - loss: 109.1979 - val_loss: 160.5587\n",
"Epoch 56/100\n",
" - 0s - loss: 107.1818 - val_loss: 159.5543\n",
"Epoch 57/100\n",
" - 0s - loss: 105.4939 - val_loss: 159.7702\n",
"Epoch 58/100\n",
" - 0s - loss: 103.5527 - val_loss: 159.2782\n",
"Epoch 59/100\n",
" - 0s - loss: 101.6429 - val_loss: 155.3195\n",
"Epoch 60/100\n",
" - 0s - loss: 99.6941 - val_loss: 159.5196\n",
"Epoch 61/100\n",
" - 0s - loss: 97.6220 - val_loss: 155.2364\n",
"Epoch 62/100\n",
" - 0s - loss: 95.7710 - val_loss: 158.1826\n",
"Epoch 63/100\n",
" - 0s - loss: 92.3755 - val_loss: 152.2515\n",
"Epoch 64/100\n",
" - 0s - loss: 89.7371 - val_loss: 151.7381\n",
"Epoch 65/100\n",
" - 0s - loss: 86.7943 - val_loss: 154.8205\n",
"Epoch 66/100\n",
" - 0s - loss: 84.6312 - val_loss: 147.7635\n",
"Epoch 67/100\n",
" - 0s - loss: 81.2704 - val_loss: 148.9938\n",
"Epoch 68/100\n",
" - 0s - loss: 79.1945 - val_loss: 140.8644\n",
"Epoch 69/100\n",
" - 0s - loss: 76.4249 - val_loss: 142.7201\n",
"Epoch 70/100\n",
" - 0s - loss: 73.8421 - val_loss: 145.6469\n",
"Epoch 71/100\n",
" - 0s - loss: 70.5340 - val_loss: 136.6724\n",
"Epoch 72/100\n",
" - 0s - loss: 68.5333 - val_loss: 139.7123\n",
"Epoch 73/100\n",
" - 0s - loss: 65.9052 - val_loss: 136.0082\n",
"Epoch 74/100\n",
" - 0s - loss: 64.2660 - val_loss: 134.1041\n",
"Epoch 75/100\n",
" - 0s - loss: 62.0962 - val_loss: 132.5490\n",
"Epoch 76/100\n",
" - 0s - loss: 59.9629 - val_loss: 129.3196\n",
"Epoch 77/100\n",
" - 0s - loss: 58.5403 - val_loss: 136.4573\n",
"Epoch 78/100\n",
" - 0s - loss: 56.3605 - val_loss: 126.1083\n",
"Epoch 79/100\n",
" - 0s - loss: 55.0615 - val_loss: 134.0168\n",
"Epoch 80/100\n",
" - 0s - loss: 53.5301 - val_loss: 130.1896\n",
"Epoch 81/100\n",
" - 0s - loss: 52.2453 - val_loss: 131.2161\n",
"Epoch 82/100\n",
" - 0s - loss: 51.4025 - val_loss: 131.0418\n",
"Epoch 83/100\n",
" - 0s - loss: 50.2441 - val_loss: 124.9425\n",
"Epoch 84/100\n",
" - 0s - loss: 48.5690 - val_loss: 123.2909\n",
"Epoch 85/100\n",
" - 0s - loss: 47.2288 - val_loss: 125.9528\n",
"Epoch 86/100\n",
" - 0s - loss: 46.5487 - val_loss: 124.3209\n",
"Epoch 87/100\n",
" - 0s - loss: 45.7390 - val_loss: 121.9548\n",
"Epoch 88/100\n",
" - 0s - loss: 45.6506 - val_loss: 119.6992\n",
"Epoch 89/100\n",
" - 0s - loss: 44.5934 - val_loss: 127.1858\n",
"Epoch 90/100\n",
" - 0s - loss: 43.0524 - val_loss: 130.6790\n",
"Epoch 91/100\n",
" - 0s - loss: 42.3558 - val_loss: 121.0691\n",
"Epoch 92/100\n",
" - 0s - loss: 42.1868 - val_loss: 117.4569\n",
"Epoch 93/100\n",
" - 0s - loss: 41.1597 - val_loss: 123.7384\n",
"Epoch 94/100\n",
" - 0s - loss: 40.3556 - val_loss: 120.7109\n",
"Epoch 95/100\n",
" - 0s - loss: 39.9573 - val_loss: 132.3233\n",
"Epoch 96/100\n",
" - 0s - loss: 39.8003 - val_loss: 122.2085\n",
"Epoch 97/100\n",
" - 0s - loss: 38.5462 - val_loss: 119.0885\n",
"Epoch 98/100\n",
" - 0s - loss: 38.3340 - val_loss: 120.4175\n",
"Epoch 99/100\n",
" - 0s - loss: 37.7566 - val_loss: 120.3234\n",
"Epoch 100/100\n",
" - 0s - loss: 36.9914 - val_loss: 119.4162\n"
]
},
{
"data": {
"text/plain": [
"<keras.callbacks.History at 0x7fdc7a81ee10>"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# build the model\n",
"model = regression_model()\n",
"\n",
"# fit the model\n",
"model.fit(predictors_norm, target, validation_split=0.3, epochs=100, verbose=2)"
]
},
{
"cell_type": "markdown",
"metadata": {
"button": false,
"deletable": true,
"new_sheet": false,
"run_control": {
"read_only": false
}
},
"source": [
"<strong>You can refer to this [link](https://keras.io/models/sequential/) to learn about other functions that you can use for prediction or evaluation.</strong>"
]
},
{
"cell_type": "markdown",
"metadata": {
"button": false,
"deletable": true,
"new_sheet": false,
"run_control": {
"read_only": false
}
},
"source": [
"Feel free to vary the following and note what impact each change has on the model's performance:\n",
"\n",
"1. Increase or decreate number of neurons in hidden layers\n",
"2. Add more hidden layers\n",
"3. Increase number of epochs"
]
},
{
"cell_type": "markdown",
"metadata": {
"button": false,
"deletable": true,
"new_sheet": false,
"run_control": {
"read_only": false
}
},
"source": [
"### Thank you for completing this lab!\n",
"\n",
"This notebook was created by [Alex Aklson](https://www.linkedin.com/in/aklson/). I hope you found this lab interesting and educational. Feel free to contact me if you have any questions!"
]
},
{
"cell_type": "markdown",
"metadata": {
"button": false,
"deletable": true,
"new_sheet": false,
"run_control": {
"read_only": false
}
},
"source": [
"This notebook is part of a course on **edX** called *Deep Learning Fundamentals with Keras*. If you accessed this notebook outside the course, you can take this course online by clicking [here](http://cocl.us/DL0101EN_edX_Week3_LAB1)."
]
},
{
"cell_type": "markdown",
"metadata": {
"button": false,
"deletable": true,
"new_sheet": false,
"run_control": {
"read_only": false
}
},
"source": [
"<hr>\n",
"\n",
"Copyright &copy; 2018 [IBM Developer Skills Network](https://cognitiveclass.ai/?utm_source=bducopyrightlink&utm_medium=dswb&utm_campaign=bdu). This notebook and its source code are released under the terms of the [MIT License](https://bigdatauniversity.com/mit-license/)."
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python",
"language": "python",
"name": "conda-env-python-py"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.10"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment