# <!-- TITLE --> [BHPD1] - Regression with a Dense Network (DNN)
# <!-- TITLE --> [BHPD1] - Regression with a Dense Network (DNN)
<!-- DESC --> Simple example of a regression with the dataset Boston Housing Prices Dataset (BHPD)
<!-- DESC --> Simple example of a regression with the dataset Boston Housing Prices Dataset (BHPD)
<!-- AUTHOR : Jean-Luc Parouty (CNRS/SIMaP) -->
<!-- AUTHOR : Jean-Luc Parouty (CNRS/SIMaP) -->
## Objectives :
## Objectives :
- Predicts **housing prices** from a set of house features.
- Predicts **housing prices** from a set of house features.
- Understanding the **principle** and the **architecture** of a regression with a **dense neural network**
- Understanding the **principle** and the **architecture** of a regression with a **dense neural network**
The **[Boston Housing Prices Dataset](https://www.cs.toronto.edu/~delve/data/boston/bostonDetail.html)** consists of price of houses in various places in Boston.
The **[Boston Housing Prices Dataset](https://www.cs.toronto.edu/~delve/data/boston/bostonDetail.html)** consists of price of houses in various places in Boston.
Alongside with price, the dataset also provide theses informations :
Alongside with price, the dataset also provide theses informations :
- CRIM: This is the per capita crime rate by town
- CRIM: This is the per capita crime rate by town
- ZN: This is the proportion of residential land zoned for lots larger than 25,000 sq.ft
- ZN: This is the proportion of residential land zoned for lots larger than 25,000 sq.ft
- INDUS: This is the proportion of non-retail business acres per town
- INDUS: This is the proportion of non-retail business acres per town
- CHAS: This is the Charles River dummy variable (this is equal to 1 if tract bounds river; 0 otherwise)
- CHAS: This is the Charles River dummy variable (this is equal to 1 if tract bounds river; 0 otherwise)
- NOX: This is the nitric oxides concentration (parts per 10 million)
- NOX: This is the nitric oxides concentration (parts per 10 million)
- RM: This is the average number of rooms per dwelling
- RM: This is the average number of rooms per dwelling
- AGE: This is the proportion of owner-occupied units built prior to 1940
- AGE: This is the proportion of owner-occupied units built prior to 1940
- DIS: This is the weighted distances to five Boston employment centers
- DIS: This is the weighted distances to five Boston employment centers
- RAD: This is the index of accessibility to radial highways
- RAD: This is the index of accessibility to radial highways
- TAX: This is the full-value property-tax rate per 10,000 dollars
- TAX: This is the full-value property-tax rate per 10,000 dollars
- PTRATIO: This is the pupil-teacher ratio by town
- PTRATIO: This is the pupil-teacher ratio by town
- B: This is calculated as 1000(Bk — 0.63)^2, where Bk is the proportion of people of African American descent by town
- B: This is calculated as 1000(Bk — 0.63)^2, where Bk is the proportion of people of African American descent by town
- LSTAT: This is the percentage lower status of the population
- LSTAT: This is the percentage lower status of the population
- MEDV: This is the median value of owner-occupied homes in 1000 dollars
- MEDV: This is the median value of owner-occupied homes in 1000 dollars
## What we're going to do :
## What we're going to do :
- Retrieve data
- Retrieve data
- Preparing the data
- Preparing the data
- Build a model
- Build a model
- Train the model
- Train the model
- Evaluate the result
- Evaluate the result
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
## Step 1 - Import and init
## Step 1 - Import and init
%% Cell type:code id: tags:
%% Cell type:code id: tags:
``` python
``` python
importtensorflowastf
importtensorflowastf
fromtensorflowimportkeras
fromtensorflowimportkeras
importnumpyasnp
importnumpyasnp
importmatplotlib.pyplotasplt
importmatplotlib.pyplotasplt
importpandasaspd
importpandasaspd
importos,sys
importos,sys
sys.path.append('..')
sys.path.append('..')
importfidle.pwkaspwk
importfidle.pwkaspwk
datasets_dir=pwk.init('BHPD1')
datasets_dir=pwk.init('BHPD1')
```
```
%% Output
%% Output
<br>**FIDLE 2020 - Practical Work Module**
<br>**FIDLE 2020 - Practical Work Module**
Version : 2.0.1
Version : 2.0.1
Notebook id : BHPD1
Notebook id : BHPD1
Run time : Thursday 14 January 2021, 10:57:04
Run time : Thursday 14 January 2021, 10:57:04
TensorFlow version : 2.2.0
TensorFlow version : 2.2.0
Keras version : 2.3.0-tf
Keras version : 2.3.0-tf
Datasets dir : /home/pjluc/datasets/fidle
Datasets dir : /home/pjluc/datasets/fidle
Run dir : ./run
Run dir : ./run
Update keras cache : False
Update keras cache : False
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
## Step 2 - Retrieve data
## Step 2 - Retrieve data
### 2.1 - Option 1 : From Keras
### 2.1 - Option 1 : From Keras
Boston housing is a famous historic dataset, so we can get it directly from [Keras datasets](https://www.tensorflow.org/api_docs/python/tf/keras/datasets)
Boston housing is a famous historic dataset, so we can get it directly from [Keras datasets](https://www.tensorflow.org/api_docs/python/tf/keras/datasets)
# <!-- TITLE --> [BHPD2] - Regression with a Dense Network (DNN) - Advanced code
# <!-- TITLE --> [BHPD2] - Regression with a Dense Network (DNN) - Advanced code
<!-- DESC --> A more advanced implementation of the precedent example
<!-- DESC --> A more advanced implementation of the precedent example
<!-- AUTHOR : Jean-Luc Parouty (CNRS/SIMaP) -->
<!-- AUTHOR : Jean-Luc Parouty (CNRS/SIMaP) -->
## Objectives :
## Objectives :
- Predicts **housing prices** from a set of house features.
- Predicts **housing prices** from a set of house features.
- Understanding the principle and the architecture of a regression with a dense neural network with backup and restore of the trained model.
- Understanding the principle and the architecture of a regression with a dense neural network with backup and restore of the trained model.
The **[Boston Housing Prices Dataset](https://www.cs.toronto.edu/~delve/data/boston/bostonDetail.html)** consists of price of houses in various places in Boston.
The **[Boston Housing Prices Dataset](https://www.cs.toronto.edu/~delve/data/boston/bostonDetail.html)** consists of price of houses in various places in Boston.
Alongside with price, the dataset also provide these information :
Alongside with price, the dataset also provide these information :
- CRIM: This is the per capita crime rate by town
- CRIM: This is the per capita crime rate by town
- ZN: This is the proportion of residential land zoned for lots larger than 25,000 sq.ft
- ZN: This is the proportion of residential land zoned for lots larger than 25,000 sq.ft
- INDUS: This is the proportion of non-retail business acres per town
- INDUS: This is the proportion of non-retail business acres per town
- CHAS: This is the Charles River dummy variable (this is equal to 1 if tract bounds river; 0 otherwise)
- CHAS: This is the Charles River dummy variable (this is equal to 1 if tract bounds river; 0 otherwise)
- NOX: This is the nitric oxides concentration (parts per 10 million)
- NOX: This is the nitric oxides concentration (parts per 10 million)
- RM: This is the average number of rooms per dwelling
- RM: This is the average number of rooms per dwelling
- AGE: This is the proportion of owner-occupied units built prior to 1940
- AGE: This is the proportion of owner-occupied units built prior to 1940
- DIS: This is the weighted distances to five Boston employment centers
- DIS: This is the weighted distances to five Boston employment centers
- RAD: This is the index of accessibility to radial highways
- RAD: This is the index of accessibility to radial highways
- TAX: This is the full-value property-tax rate per 10,000 dollars
- TAX: This is the full-value property-tax rate per 10,000 dollars
- PTRATIO: This is the pupil-teacher ratio by town
- PTRATIO: This is the pupil-teacher ratio by town
- B: This is calculated as 1000(Bk — 0.63)^2, where Bk is the proportion of people of African American descent by town
- B: This is calculated as 1000(Bk — 0.63)^2, where Bk is the proportion of people of African American descent by town
- LSTAT: This is the percentage lower status of the population
- LSTAT: This is the percentage lower status of the population
- MEDV: This is the median value of owner-occupied homes in 1000 dollars
- MEDV: This is the median value of owner-occupied homes in 1000 dollars
## What we're going to do :
## What we're going to do :
- (Retrieve data)
- (Retrieve data)
- (Preparing the data)
- (Preparing the data)
- (Build a model)
- (Build a model)
- Train and save the model
- Train and save the model
- Restore saved model
- Restore saved model
- Evaluate the model
- Evaluate the model
- Make some predictions
- Make some predictions
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
## Step 1 - Import and init
## Step 1 - Import and init
%% Cell type:code id: tags:
%% Cell type:code id: tags:
``` python
``` python
importtensorflowastf
importtensorflowastf
fromtensorflowimportkeras
fromtensorflowimportkeras
importnumpyasnp
importnumpyasnp
importmatplotlib.pyplotasplt
importmatplotlib.pyplotasplt
importpandasaspd
importpandasaspd
importos,sys
importos,sys
fromIPython.displayimportMarkdown
fromIPython.displayimportMarkdown
fromimportlibimportreload
fromimportlibimportreload
sys.path.append('..')
sys.path.append('..')
importfidle.pwkaspwk
importfidle.pwkaspwk
datasets_dir=pwk.init('BHPD2')
datasets_dir=pwk.init('BHPD2')
```
```
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
## Step 2 - Retrieve data
## Step 2 - Retrieve data
### 2.1 - Option 1 : From Keras
### 2.1 - Option 1 : From Keras
Boston housing is a famous historic dataset, so we can get it directly from [Keras datasets](https://www.tensorflow.org/api_docs/python/tf/keras/datasets)
Boston housing is a famous historic dataset, so we can get it directly from [Keras datasets](https://www.tensorflow.org/api_docs/python/tf/keras/datasets)