{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"button": false,
"new_sheet": false,
"run_control": {
"read_only": false
}
},
"source": [
"# Regression Models with Keras\n",
"---"
]
},
{
"cell_type": "markdown",
"metadata": {
"button": false,
"new_sheet": false,
"run_control": {
"read_only": false
}
},
"source": [
"## Introduction"
]
},
{
"cell_type": "markdown",
"metadata": {
"button": false,
"new_sheet": false,
"run_control": {
"read_only": false
}
},
"source": [
"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 notebook , 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."
]
},
{
"cell_type": "markdown",
"metadata": {
"button": false,
"new_sheet": false,
"run_control": {
"read_only": false
}
},
"source": [
"## Download and Clean Dataset"
]
},
{
"cell_type": "markdown",
"metadata": {
"button": false,
"new_sheet": false,
"run_control": {
"read_only": false
}
},
"source": [
"Let's start by importing the pandas and the Numpy libraries."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"button": false,
"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,
"new_sheet": false,
"run_control": {
"read_only": false
}
},
"source": [
"The dataset is about the compressive strength of different samples of concrete based on the volumes of the different ingredients that were used to make them. Ingredients include:\n",
"
- Cement
\n",
"- Blast Furnace Slag
\n",
"- Fly Ash
\n",
"- Water
\n",
"- Superplasticizer
\n",
"- Coarse Aggregate
\n",
"- Fine Aggregate
"
]
},
{
"cell_type": "markdown",
"metadata": {
"button": false,
"new_sheet": false,
"run_control": {
"read_only": false
}
},
"source": [
"Let's download the data and read it into a pandas dataframe."
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"button": false,
"new_sheet": false,
"run_control": {
"read_only": false
}
},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" Cement | \n",
" Blast Furnace Slag | \n",
" Fly Ash | \n",
" Water | \n",
" Superplasticizer | \n",
" Coarse Aggregate | \n",
" Fine Aggregate | \n",
" Age | \n",
" Strength | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" 540.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 162.0 | \n",
" 2.5 | \n",
" 1040.0 | \n",
" 676.0 | \n",
" 28 | \n",
" 79.99 | \n",
"
\n",
" \n",
" 1 | \n",
" 540.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 162.0 | \n",
" 2.5 | \n",
" 1055.0 | \n",
" 676.0 | \n",
" 28 | \n",
" 61.89 | \n",
"
\n",
" \n",
" 2 | \n",
" 332.5 | \n",
" 142.5 | \n",
" 0.0 | \n",
" 228.0 | \n",
" 0.0 | \n",
" 932.0 | \n",
" 594.0 | \n",
" 270 | \n",
" 40.27 | \n",
"
\n",
" \n",
" 3 | \n",
" 332.5 | \n",
" 142.5 | \n",
" 0.0 | \n",
" 228.0 | \n",
" 0.0 | \n",
" 932.0 | \n",
" 594.0 | \n",
" 365 | \n",
" 41.05 | \n",
"
\n",
" \n",
" 4 | \n",
" 198.6 | \n",
" 132.4 | \n",
" 0.0 | \n",
" 192.0 | \n",
" 0.0 | \n",
" 978.4 | \n",
" 825.5 | \n",
" 360 | \n",
" 44.30 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"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://s3-api.us-geo.objectstorage.softlayer.net/cf-courses-data/CognitiveClass/DL0101EN/labs/data/concrete_data.csv')\n",
"concrete_data.head()"
]
},
{
"cell_type": "markdown",
"metadata": {
"button": false,
"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,
"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,
"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,
"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,
"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,
"new_sheet": false,
"run_control": {
"read_only": false
}
},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" Cement | \n",
" Blast Furnace Slag | \n",
" Fly Ash | \n",
" Water | \n",
" Superplasticizer | \n",
" Coarse Aggregate | \n",
" Fine Aggregate | \n",
" Age | \n",
" Strength | \n",
"
\n",
" \n",
" \n",
" \n",
" count | \n",
" 1030.000000 | \n",
" 1030.000000 | \n",
" 1030.000000 | \n",
" 1030.000000 | \n",
" 1030.000000 | \n",
" 1030.000000 | \n",
" 1030.000000 | \n",
" 1030.000000 | \n",
" 1030.000000 | \n",
"
\n",
" \n",
" mean | \n",
" 281.167864 | \n",
" 73.895825 | \n",
" 54.188350 | \n",
" 181.567282 | \n",
" 6.204660 | \n",
" 972.918932 | \n",
" 773.580485 | \n",
" 45.662136 | \n",
" 35.817961 | \n",
"
\n",
" \n",
" std | \n",
" 104.506364 | \n",
" 86.279342 | \n",
" 63.997004 | \n",
" 21.354219 | \n",
" 5.973841 | \n",
" 77.753954 | \n",
" 80.175980 | \n",
" 63.169912 | \n",
" 16.705742 | \n",
"
\n",
" \n",
" min | \n",
" 102.000000 | \n",
" 0.000000 | \n",
" 0.000000 | \n",
" 121.800000 | \n",
" 0.000000 | \n",
" 801.000000 | \n",
" 594.000000 | \n",
" 1.000000 | \n",
" 2.330000 | \n",
"
\n",
" \n",
" 25% | \n",
" 192.375000 | \n",
" 0.000000 | \n",
" 0.000000 | \n",
" 164.900000 | \n",
" 0.000000 | \n",
" 932.000000 | \n",
" 730.950000 | \n",
" 7.000000 | \n",
" 23.710000 | \n",
"
\n",
" \n",
" 50% | \n",
" 272.900000 | \n",
" 22.000000 | \n",
" 0.000000 | \n",
" 185.000000 | \n",
" 6.400000 | \n",
" 968.000000 | \n",
" 779.500000 | \n",
" 28.000000 | \n",
" 34.445000 | \n",
"
\n",
" \n",
" 75% | \n",
" 350.000000 | \n",
" 142.950000 | \n",
" 118.300000 | \n",
" 192.000000 | \n",
" 10.200000 | \n",
" 1029.400000 | \n",
" 824.000000 | \n",
" 56.000000 | \n",
" 46.135000 | \n",
"
\n",
" \n",
" max | \n",
" 540.000000 | \n",
" 359.400000 | \n",
" 200.100000 | \n",
" 247.000000 | \n",
" 32.200000 | \n",
" 1145.000000 | \n",
" 992.600000 | \n",
" 365.000000 | \n",
" 82.600000 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"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,
"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,
"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,
"new_sheet": false,
"run_control": {
"read_only": false
}
},
"source": [
"#### Split data into predictors and target"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The target variable in this problem is the concrete sample strength. Therefore, our predictors will be all the other columns."
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"button": false,
"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,
"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,
"new_sheet": false,
"run_control": {
"read_only": false
}
},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" Cement | \n",
" Blast Furnace Slag | \n",
" Fly Ash | \n",
" Water | \n",
" Superplasticizer | \n",
" Coarse Aggregate | \n",
" Fine Aggregate | \n",
" Age | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" 540.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 162.0 | \n",
" 2.5 | \n",
" 1040.0 | \n",
" 676.0 | \n",
" 28 | \n",
"
\n",
" \n",
" 1 | \n",
" 540.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 162.0 | \n",
" 2.5 | \n",
" 1055.0 | \n",
" 676.0 | \n",
" 28 | \n",
"
\n",
" \n",
" 2 | \n",
" 332.5 | \n",
" 142.5 | \n",
" 0.0 | \n",
" 228.0 | \n",
" 0.0 | \n",
" 932.0 | \n",
" 594.0 | \n",
" 270 | \n",
"
\n",
" \n",
" 3 | \n",
" 332.5 | \n",
" 142.5 | \n",
" 0.0 | \n",
" 228.0 | \n",
" 0.0 | \n",
" 932.0 | \n",
" 594.0 | \n",
" 365 | \n",
"
\n",
" \n",
" 4 | \n",
" 198.6 | \n",
" 132.4 | \n",
" 0.0 | \n",
" 192.0 | \n",
" 0.0 | \n",
" 978.4 | \n",
" 825.5 | \n",
" 360 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"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,
"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,
"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,
"new_sheet": false,
"run_control": {
"read_only": false
}
},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" Cement | \n",
" Blast Furnace Slag | \n",
" Fly Ash | \n",
" Water | \n",
" Superplasticizer | \n",
" Coarse Aggregate | \n",
" Fine Aggregate | \n",
" Age | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" 2.476712 | \n",
" -0.856472 | \n",
" -0.846733 | \n",
" -0.916319 | \n",
" -0.620147 | \n",
" 0.862735 | \n",
" -1.217079 | \n",
" -0.279597 | \n",
"
\n",
" \n",
" 1 | \n",
" 2.476712 | \n",
" -0.856472 | \n",
" -0.846733 | \n",
" -0.916319 | \n",
" -0.620147 | \n",
" 1.055651 | \n",
" -1.217079 | \n",
" -0.279597 | \n",
"
\n",
" \n",
" 2 | \n",
" 0.491187 | \n",
" 0.795140 | \n",
" -0.846733 | \n",
" 2.174405 | \n",
" -1.038638 | \n",
" -0.526262 | \n",
" -2.239829 | \n",
" 3.551340 | \n",
"
\n",
" \n",
" 3 | \n",
" 0.491187 | \n",
" 0.795140 | \n",
" -0.846733 | \n",
" 2.174405 | \n",
" -1.038638 | \n",
" -0.526262 | \n",
" -2.239829 | \n",
" 5.055221 | \n",
"
\n",
" \n",
" 4 | \n",
" -0.790075 | \n",
" 0.678079 | \n",
" -0.846733 | \n",
" 0.488555 | \n",
" -1.038638 | \n",
" 0.070492 | \n",
" 0.647569 | \n",
" 4.976069 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"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": "markdown",
"metadata": {},
"source": [
"Let's save the number of predictors to *n_cols* since we will need this number when building our network."
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"button": false,
"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,
"new_sheet": false,
"run_control": {
"read_only": false
}
},
"source": [
"## Import Keras"
]
},
{
"cell_type": "markdown",
"metadata": {
"button": false,
"new_sheet": false,
"run_control": {
"read_only": false
}
},
"source": [
"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. "
]
},
{
"cell_type": "markdown",
"metadata": {
"button": false,
"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,
"new_sheet": false,
"run_control": {
"read_only": false
}
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"Using Theano backend.\n",
"WARNING (theano.configdefaults): g++ not available, if using conda: `conda install m2w64-toolchain`\n",
"C:\\Users\\kambo\\Anaconda3\\lib\\site-packages\\theano\\configdefaults.py:560: UserWarning: DeprecationWarning: there is no c++ compiler.This is deprecated and with Theano 0.11 a c++ compiler will be mandatory\n",
" warnings.warn(\"DeprecationWarning: there is no c++ compiler.\"\n",
"WARNING (theano.configdefaults): g++ not detected ! Theano will be unable to execute optimized C-implementations (for both CPU and GPU) and will default to Python implementations. Performance will be severely degraded. To remove this warning, set Theano flags cxx to an empty string.\n",
"WARNING (theano.tensor.blas): Using NumPy C-API based implementation for BLAS functions.\n"
]
}
],
"source": [
"import keras"
]
},
{
"cell_type": "markdown",
"metadata": {
"button": false,
"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,
"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,
"new_sheet": false,
"run_control": {
"read_only": false
}
},
"source": [
"## Build a Neural Network"
]
},
{
"cell_type": "markdown",
"metadata": {
"button": false,
"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,
"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": {},
"source": [
"The above function create a model that has two hidden layers, each of 50 hidden units."
]
},
{
"cell_type": "markdown",
"metadata": {
"button": false,
"new_sheet": false,
"run_control": {
"read_only": false
}
},
"source": [
"## Train and Test the Network"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let's call the function now to create our model."
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {
"button": false,
"new_sheet": false,
"run_control": {
"read_only": false
}
},
"outputs": [],
"source": [
"# build the model\n",
"model = regression_model()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Next, we will train and test the model at the same time using the *fit* method. We will leave out 30% of the data for validation and we will train the model for 100 epochs."
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {
"collapsed": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Train on 721 samples, validate on 309 samples\n",
"Epoch 1/100\n",
" - 26s - loss: 1659.8173 - val_loss: 1155.1372\n",
"Epoch 2/100\n",
" - 28s - loss: 1544.7331 - val_loss: 1045.9335\n",
"Epoch 3/100\n",
" - 28s - loss: 1344.0200 - val_loss: 872.9507\n",
"Epoch 4/100\n",
" - 28s - loss: 1042.0318 - val_loss: 643.4252\n",
"Epoch 5/100\n",
" - 28s - loss: 675.1464 - val_loss: 414.3786\n",
"Epoch 6/100\n",
" - 28s - loss: 388.2152 - val_loss: 252.5833\n",
"Epoch 7/100\n",
" - 28s - loss: 259.4572 - val_loss: 184.6644\n",
"Epoch 8/100\n",
" - 27s - loss: 228.8623 - val_loss: 165.0791\n",
"Epoch 9/100\n",
" - 27s - loss: 214.0551 - val_loss: 162.5311\n",
"Epoch 10/100\n",
" - 28s - loss: 203.4362 - val_loss: 160.4506\n",
"Epoch 11/100\n",
" - 27s - loss: 195.8458 - val_loss: 155.8130\n",
"Epoch 12/100\n",
" - 28s - loss: 188.8086 - val_loss: 158.2724\n",
"Epoch 13/100\n",
" - 28s - loss: 183.8785 - val_loss: 155.4936\n",
"Epoch 14/100\n",
" - 29s - loss: 177.3869 - val_loss: 154.4414\n",
"Epoch 15/100\n",
" - 27s - loss: 172.4479 - val_loss: 153.5304\n",
"Epoch 16/100\n",
" - 29s - loss: 168.6921 - val_loss: 154.3388\n",
"Epoch 17/100\n",
" - 29s - loss: 164.4362 - val_loss: 154.8373\n",
"Epoch 18/100\n",
" - 28s - loss: 161.6059 - val_loss: 152.6080\n",
"Epoch 19/100\n",
" - 30s - loss: 158.3440 - val_loss: 153.0470\n",
"Epoch 20/100\n",
" - 27s - loss: 155.8933 - val_loss: 154.6291\n",
"Epoch 21/100\n",
" - 27s - loss: 153.7243 - val_loss: 154.1524\n",
"Epoch 22/100\n",
" - 27s - loss: 151.7872 - val_loss: 155.1355\n",
"Epoch 23/100\n",
" - 28s - loss: 149.5842 - val_loss: 156.2538\n",
"Epoch 24/100\n",
" - 26s - loss: 148.1771 - val_loss: 156.2502\n",
"Epoch 25/100\n",
" - 27s - loss: 145.9553 - val_loss: 157.5960\n",
"Epoch 26/100\n",
" - 27s - loss: 144.4943 - val_loss: 156.7449\n",
"Epoch 27/100\n",
" - 29s - loss: 142.8318 - val_loss: 159.8286\n",
"Epoch 28/100\n",
" - 34s - loss: 141.3612 - val_loss: 157.7329\n",
"Epoch 29/100\n",
" - 28s - loss: 139.9080 - val_loss: 160.9182\n",
"Epoch 30/100\n",
" - 30s - loss: 138.7736 - val_loss: 159.0990\n",
"Epoch 31/100\n",
" - 27s - loss: 137.0320 - val_loss: 160.5245\n",
"Epoch 32/100\n",
" - 28s - loss: 135.0703 - val_loss: 158.6432\n",
"Epoch 33/100\n",
" - 28s - loss: 133.3164 - val_loss: 160.8005\n",
"Epoch 34/100\n",
" - 30s - loss: 132.2437 - val_loss: 159.3557\n",
"Epoch 35/100\n",
" - 28s - loss: 129.6403 - val_loss: 159.0260\n",
"Epoch 36/100\n",
" - 28s - loss: 127.7831 - val_loss: 158.5421\n",
"Epoch 37/100\n",
" - 28s - loss: 126.4286 - val_loss: 157.2492\n",
"Epoch 38/100\n",
" - 28s - loss: 123.7727 - val_loss: 157.4679\n",
"Epoch 39/100\n",
" - 30s - loss: 121.7114 - val_loss: 152.2848\n",
"Epoch 40/100\n",
" - 28s - loss: 120.4351 - val_loss: 152.9987\n",
"Epoch 41/100\n",
" - 27s - loss: 117.1655 - val_loss: 149.8424\n",
"Epoch 42/100\n",
" - 30s - loss: 115.0706 - val_loss: 147.3483\n",
"Epoch 43/100\n",
" - 27s - loss: 112.2152 - val_loss: 149.2031\n",
"Epoch 44/100\n",
" - 26s - loss: 109.6747 - val_loss: 146.8172\n",
"Epoch 45/100\n",
" - 26s - loss: 107.0343 - val_loss: 144.4249\n",
"Epoch 46/100\n",
" - 26s - loss: 104.3647 - val_loss: 144.4551\n",
"Epoch 47/100\n",
" - 26s - loss: 101.1888 - val_loss: 138.8628\n",
"Epoch 48/100\n",
" - 26s - loss: 99.0146 - val_loss: 141.6793\n",
"Epoch 49/100\n",
" - 26s - loss: 95.3248 - val_loss: 135.5863\n",
"Epoch 50/100\n",
" - 27s - loss: 93.0188 - val_loss: 139.8666\n",
"Epoch 51/100\n",
" - 26s - loss: 90.0793 - val_loss: 132.0471\n",
"Epoch 52/100\n",
" - 26s - loss: 87.3171 - val_loss: 128.5291\n",
"Epoch 53/100\n",
" - 26s - loss: 85.1340 - val_loss: 128.2870\n",
"Epoch 54/100\n",
" - 26s - loss: 82.3866 - val_loss: 132.8614\n",
"Epoch 55/100\n",
" - 26s - loss: 78.8102 - val_loss: 126.8343\n",
"Epoch 56/100\n",
" - 26s - loss: 75.8817 - val_loss: 124.7485\n",
"Epoch 57/100\n",
" - 26s - loss: 73.1608 - val_loss: 124.0847\n",
"Epoch 58/100\n",
" - 26s - loss: 70.7985 - val_loss: 124.8666\n",
"Epoch 59/100\n",
" - 26s - loss: 69.6016 - val_loss: 121.8648\n",
"Epoch 60/100\n",
" - 26s - loss: 66.2208 - val_loss: 118.5892\n",
"Epoch 61/100\n",
" - 26s - loss: 63.8712 - val_loss: 118.3993\n",
"Epoch 62/100\n",
" - 26s - loss: 62.0159 - val_loss: 118.3694\n",
"Epoch 63/100\n",
" - 27s - loss: 60.2788 - val_loss: 115.1703\n",
"Epoch 64/100\n",
" - 28s - loss: 58.6102 - val_loss: 123.4627\n",
"Epoch 65/100\n",
" - 26s - loss: 57.0550 - val_loss: 116.9638\n",
"Epoch 66/100\n",
" - 27s - loss: 54.8055 - val_loss: 117.1133\n",
"Epoch 67/100\n",
" - 26s - loss: 53.7087 - val_loss: 122.1768\n",
"Epoch 68/100\n",
" - 26s - loss: 52.4141 - val_loss: 113.2900\n",
"Epoch 69/100\n",
" - 26s - loss: 51.1901 - val_loss: 115.5460\n",
"Epoch 70/100\n",
" - 26s - loss: 49.7567 - val_loss: 123.7369\n",
"Epoch 71/100\n",
" - 26s - loss: 48.4878 - val_loss: 119.4672\n",
"Epoch 72/100\n",
" - 26s - loss: 48.0708 - val_loss: 126.7205\n",
"Epoch 73/100\n",
" - 26s - loss: 46.5131 - val_loss: 116.9735\n",
"Epoch 74/100\n",
" - 26s - loss: 45.0752 - val_loss: 127.5061\n",
"Epoch 75/100\n",
" - 26s - loss: 44.5273 - val_loss: 117.3193\n",
"Epoch 76/100\n",
" - 26s - loss: 44.0764 - val_loss: 123.8022\n",
"Epoch 77/100\n",
" - 26s - loss: 43.1431 - val_loss: 122.7337\n",
"Epoch 78/100\n",
" - 26s - loss: 42.2949 - val_loss: 118.5015\n",
"Epoch 79/100\n",
" - 26s - loss: 41.1128 - val_loss: 126.5660\n",
"Epoch 80/100\n",
" - 26s - loss: 40.9091 - val_loss: 121.6107\n",
"Epoch 81/100\n",
" - 27s - loss: 40.4070 - val_loss: 124.8993\n",
"Epoch 82/100\n",
" - 26s - loss: 39.0727 - val_loss: 127.3001\n",
"Epoch 83/100\n",
" - 27s - loss: 39.3522 - val_loss: 135.9323\n",
"Epoch 84/100\n",
" - 26s - loss: 38.3252 - val_loss: 123.5979\n",
"Epoch 85/100\n",
" - 26s - loss: 37.8179 - val_loss: 120.8265\n",
"Epoch 86/100\n",
" - 26s - loss: 37.0650 - val_loss: 124.4024\n",
"Epoch 87/100\n",
" - 26s - loss: 36.4486 - val_loss: 124.7457\n",
"Epoch 88/100\n",
" - 26s - loss: 35.7913 - val_loss: 126.7669\n",
"Epoch 89/100\n",
" - 26s - loss: 35.5917 - val_loss: 122.8151\n",
"Epoch 90/100\n",
" - 26s - loss: 35.3068 - val_loss: 123.2664\n",
"Epoch 91/100\n",
" - 26s - loss: 34.5228 - val_loss: 126.3293\n",
"Epoch 92/100\n",
" - 26s - loss: 33.9879 - val_loss: 123.9175\n",
"Epoch 93/100\n",
" - 26s - loss: 33.8183 - val_loss: 121.4793\n",
"Epoch 94/100\n",
" - 26s - loss: 33.1778 - val_loss: 121.3896\n",
"Epoch 95/100\n",
" - 27s - loss: 33.0860 - val_loss: 117.4870\n",
"Epoch 96/100\n",
" - 26s - loss: 32.5264 - val_loss: 123.1972\n",
"Epoch 97/100\n",
" - 28s - loss: 32.0510 - val_loss: 126.1518\n",
"Epoch 98/100\n",
" - 28s - loss: 31.7597 - val_loss: 118.8847\n",
"Epoch 99/100\n",
" - 26s - loss: 31.4620 - val_loss: 124.0847\n",
"Epoch 100/100\n",
" - 28s - loss: 30.9700 - val_loss: 131.9380\n"
]
},
{
"data": {
"text/plain": [
""
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# fit the model\n",
"model.fit(predictors_norm, target, validation_split=0.3, epochs=100, verbose=2)"
]
},
{
"cell_type": "markdown",
"metadata": {
"button": false,
"new_sheet": false,
"run_control": {
"read_only": false
}
},
"source": [
"You can refer to this [link](https://keras.io/models/sequential/) to learn about other functions that you can use for prediction or evaluation."
]
},
{
"cell_type": "markdown",
"metadata": {
"button": false,
"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": {},
"source": [
"## Thanks for reading :)\n",
"\n",
"Created by [Alex Aklson](https://www.linkedin.com/in/aklson/) and modified by [Tarun kamboj](https://www.linkedin.com/in/kambojtarun/)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"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.7.6"
}
},
"nbformat": 4,
"nbformat_minor": 4
}