Construction of NN
Construction of NN
Construction of NN
# https://www.tensorflow.org/api.docs/python/tf/keras/datasets/boston_housing
# http://lib.cmu.edu/datasets/boston
1
import seaborn as sns # for plotting
# tf.debugging.set_log_device_placement(False)
[8]: all_ds.head()
[8]: crim zn indus chas nox rm age dis rad tax ptratio \
0 0.00632 18.0 2.31 0 0.538 6.575 65.2 4.0900 1 296 15.3
1 0.02731 0.0 7.07 0 0.469 6.421 78.9 4.9671 2 242 17.8
2 0.02729 0.0 7.07 0 0.469 7.185 61.1 4.9671 2 242 17.8
3 0.03237 0.0 2.18 0 0.458 6.998 45.8 6.0622 3 222 18.7
4 0.06905 0.0 2.18 0 0.458 7.147 54.2 6.0622 3 222 18.7
b lstat medv
0 396.90 4.98 24.0
1 396.90 9.14 21.6
2 392.83 4.03 34.7
3 394.63 2.94 33.4
4 396.90 5.33 36.2
[10]: all_ds.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 506 entries, 0 to 505
Data columns (total 14 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 crim 506 non-null float64
1 zn 506 non-null float64
2 indus 506 non-null float64
3 chas 506 non-null int64
4 nox 506 non-null float64
5 rm 506 non-null float64
6 age 506 non-null float64
7 dis 506 non-null float64
8 rad 506 non-null int64
9 tax 506 non-null int64
10 ptratio 506 non-null float64
2
11 b 506 non-null float64
12 lstat 506 non-null float64
13 medv 506 non-null float64
dtypes: float64(11), int64(3)
memory usage: 55.5 KB
3
indus 0
chas 0
nox 0
rm 0
age 0
dis 0
rad 0
tax 0
ptratio 0
b 0
lstat 0
medv 0
dtype: int64
After removing all rows with Null values: crim zn indus chas nox
rm age dis rad tax \
0 0.00632 18.0 2.31 0 0.538 6.575 65.2 4.0900 1 296
1 0.02731 0.0 7.07 0 0.469 6.421 78.9 4.9671 2 242
2 0.02729 0.0 7.07 0 0.469 7.185 61.1 4.9671 2 242
3 0.03237 0.0 2.18 0 0.458 6.998 45.8 6.0622 3 222
4 0.06905 0.0 2.18 0 0.458 7.147 54.2 6.0622 3 222
.. … … … … … … … … … …
501 0.06263 0.0 11.93 0 0.573 6.593 69.1 2.4786 1 273
502 0.04527 0.0 11.93 0 0.573 6.120 76.7 2.2875 1 273
503 0.06076 0.0 11.93 0 0.573 6.976 91.0 2.1675 1 273
504 0.10959 0.0 11.93 0 0.573 6.794 89.3 2.3889 1 273
505 0.04741 0.0 11.93 0 0.573 6.030 80.8 2.5050 1 273
4
ptratio b lstat medv
0 15.3 396.90 4.98 24.0
1 17.8 396.90 9.14 21.6
2 17.8 392.83 4.03 34.7
3 18.7 394.63 2.94 33.4
4 18.7 396.90 5.33 36.2
.. … … … …
501 21.0 391.99 9.67 22.4
502 21.0 396.90 9.08 20.6
503 21.0 396.90 5.64 23.9
504 21.0 393.45 6.48 22.0
505 21.0 396.90 7.88 11.9
5
4 18.7 396.90 5.33 36.2
5 18.7 394.12 5.21 28.7
6 15.2 395.60 12.43 22.9
7 15.2 396.90 19.15 27.1
8 15.2 386.63 29.93 16.5
9 15.2 386.71 17.10 18.9
10 15.2 392.52 20.45 15.0
11 15.2 396.90 13.27 18.9
12 15.2 390.50 15.71 21.7
13 21.0 396.90 8.26 20.4
14 21.0 380.02 10.26 18.2
15 21.0 395.62 8.47 19.9
16 21.0 386.85 6.58 23.1
17 21.0 386.75 14.67 17.5
18 21.0 288.99 11.69 20.2
19 21.0 390.95 11.28 18.2
6
.. … … … …
110 20.9 393.49 13.00 21.7
300 14.8 390.86 6.07 24.8
179 17.8 396.90 5.04 37.2
379 20.2 393.74 21.78 10.2
458 20.2 272.21 16.23 14.9
[21]: # Split data into 60% train and 40% test <- can be revised.
train_dataset, temp_test_dataset = train_test_split(all_ds_90pct, test_size = 0.
↪4)
7
[30]: print(f"Validation dataset: {valid_dataset.shape}")
8
[33]: print(f"train_stats: {train_stats}")
lstat
count 273.000000
mean 12.450110
std 6.945146
min 1.730000
25% 6.750000
50% 11.450000
75% 16.290000
max 37.970000
9
b 273.0 363.179048 82.884919 2.52000 377.51000 391.4300
lstat 273.0 12.450110 6.945146 1.73000 6.75000 11.4500
75% max
crim 3.32105 88.9762
zn 18.00000 95.0000
indus 18.10000 25.6500
chas 0.00000 1.0000
nox 0.62400 0.8710
rm 6.57900 8.7800
age 93.60000 100.0000
dis 5.40070 12.1265
rad 8.00000 24.0000
tax 437.00000 666.0000
ptratio 20.20000 22.0000
b 396.90000 396.9000
lstat 16.29000 37.9700
[39]: train_stats = {
"mean": train_dataset.mean(),
"std": train_dataset.std()
}
10
[47]: print(f"norm(test_dataset): {normed_test_data.shape}")
11
[55]: print(f"normed_train_data: {normed_train_data.head(10,)}")
12
307 -0.313586 -0.264705 -1.070154 0.003302 0.406841 -0.708424
.. … … … … … …
465 -0.366355 1.726372 1.632815 0.814615 -0.347217 0.241880
291 0.564681 -0.616071 -0.930135 0.363886 0.406841 -1.280046
216 -0.345647 -0.498949 -0.741414 -0.898157 0.357374 0.152609
431 -0.810619 1.726372 1.632815 0.814615 -3.400487 1.042439
11 1.068753 -0.498949 -0.528343 -1.439032 0.406841 0.118052
/home/user/.local/lib/python3.10/site-
packages/keras/src/layers/core/dense.py:86: UserWarning: Do not pass an
`input_shape`/`input_dim` argument to a layer. When using Sequential models,
prefer using an `Input(shape)` object as the first layer in the model instead.
super().__init__(activity_regularizer=activity_regularizer, **kwargs)
Model: "sequential"
����������������������������������������������������������������������������
� Layer (type) � Output Shape � Param # �
����������������������������������������������������������������������������
� l1_Input (Dense) � (None, 273, 1) � 14 �
����������������������������������������������������������������������������
� l2_Hidden_1 (Dense) � (None, 273, 50) � 100 �
����������������������������������������������������������������������������
� l3_Hidden_2 (Dense) � (None, 273, 50) � 2,550 �
����������������������������������������������������������������������������
13
� l4_Output (Dense) � (None, 273, 1) � 51 �
����������������������������������������������������������������������������
model.summary(): None
[ ]:
14