-
Notifications
You must be signed in to change notification settings - Fork 0
/
linearregressiononevariable.py
334 lines (258 loc) · 11.4 KB
/
linearregressiononevariable.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
# -*- coding: utf-8 -*-
"""linearRegressionOneVariable.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1uTNR9vVi3YUBGsFdBAJDP-mRV-Dd-mF8
"""
from __future__ import absolute_import, division, print_function, unicode_literals
import pathlib
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import seaborn as sns
from datetime import datetime
# Commented out IPython magic to ensure Python compatibility.
try:
# %tensorflow_version only exists in Colab.
# %tensorflow_version 2.x
except Exception:
pass
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
print(tf.__version__)
# Load the TensorBoard notebook extension.
# %load_ext tensorboard
# Clear logs from previous calls
!rm -rf ./logs/
# Check
!ls
# Download the daset with keras.utils.get_file
import ssl
ssl._create_default_https_context = ssl._create_unverified_context
dataset_path = keras.utils.get_file("housing.data", "https://archive.ics.uci.edu/ml/machine-learning-databases/housing/housing.data")
"""## Boston house prices dataset
#### Characteristics
* Number of Instances: 506
* The first 13 features are numeric/categorical predictive features.
* The last one (attribute 14): Median Value is the target variable.
#### Attributes
1. CRIM per capita crime rate by town
2. ZN proportion of residential land zoned for lots over 25,000 sq.ft.
3. INDUS proportion of non-retail business acres per town
4. CHAS Charles River dummy variable (= 1 if tract bounds river; 0 otherwise)
5. NOX nitric oxides concentration (parts per 10 million)
6. RM average number of rooms per dwelling
7. AGE proportion of owner-occupied units built prior to 1940
8. DIS weighted distances to five Boston employment centres
9. RAD index of accessibility to radial highways
10. TAX full-value property-tax rate per \$10,000
11. PTRATIO pupil-teacher ratio by town
12. B 1000(Bk - 0.63)^2 where Bk is the proportion of blacks by town
13. LSTAT \% lower status of the population
14. MEDV Median value of owner-occupied homes in \$1000’s [target attribute]
"""
column_names = ['CRIM','ZN','INDUS','CHAS','NOX',
'RM', 'AGE', 'DIS','RAD','TAX','PTRATION', 'B', 'LSTAT', 'MEDV']
raw_dataset = pd.read_csv(dataset_path, names=column_names,
na_values = "?", comment='\t',
sep=" ", skipinitialspace=True)
# Create a dataset instant
dataset = raw_dataset.copy()
# This function returns last n rows from the object
# based on position.
dataset.tail(n=10)
"""### Data processing (train/test split)"""
# Split data into train/test
# p = training data portion
p=0.8
trainDataset = dataset.sample(frac=p,random_state=0)
testDataset = dataset.drop(trainDataset.index)
"""## linear regression with one variable
Here, we desire to model the relationship between the dependent variable and the independent variable. In the linear regression with one variable, *we only have **one** independent variable*.
* Independent variable: 'RM'
* Dependent variable: 'MEDV'
In a simple word, we want to **predict** the Median value of owner-occupied homes in $1000’s [target attribute] based on the average number of rooms per dwelling (RM).
### Plot dependecy to one variable (linear regression with one variable)
Here we want to plot the MEDV against RM, i.e, visualize how MEDV is changed by changing RM. Basically we have $MEDV=f(RM)$ and we desire to estimate the function $f(.)$ using a linear regression.
"""
# Visual representation of training data
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
# With .pop() command, the associated columns are extracted.
x = trainDataset['RM']
y = trainDataset['MEDV']
ax.scatter(x, y, edgecolors=(0, 0, 0))
ax.set_xlabel('RM')
ax.set_ylabel('MEDV')
plt.show()
"""### Split train/test data and labels for linear regression for one variable experiments
We can use two approaches to access the columns:
1. **Pop command:** It returns an item and drops it from the frame. After using trainDataset.pop('RM'), the 'RM' column does not exist in the trainDataset frame anymore!
2. Using the **indexing with labels**. Example trainDataset['RM']
We use approach **(2)**.
"""
# Pop command return item and drop it from frame.
# After using trainDataset.pop('RM'), the 'RM' column
# does not exist in the trainDataset frame anymore!
trainInput = trainDataset['RM']
trainTarget = trainDataset['MEDV']
testInput = testDataset['RM']
testTarget = testDataset['MEDV']
"""### Data normalization/standardization
*It is not needed for simple linear regression (linear regression with one variable).*
1. **Standardization**: Standardizing the features around the center and 0 with a standard deviation of 1. Assume we have features that have different units. So just becasue of the scaling do not contribute equally to the analysis and create misleading result. Formula: $\hat{X}=\frac{X-\mu}{\sigma}$
2. **Normalization**: Normalization aims to put the values of different features to a common scale (usually [0,1] or [-1,1]). This is used when features have different ranges but the same units. **Example**: Assume we have an RGB image. *Each channel has a different range but all channels have the same units: image pixel*! Formula: $\hat{X}=\frac{X-X_{min}}{X_{max}-X_{min}}$
### Create Model
1. The architecture of the model
2. Defining the optimizer
3. Compile the model and return the graph
Assume we desire to find the parameters (**W**) that predict the
output y from x in a linear fashion:
$y = w_1 x + w_0$
The above can be defined with the following dense layer:
*layers.Dense(1, use_bias=True, input_shape=(1,))*
"""
# We don't specify anything for activation -> no activation is applied (ie. "linear" activation: a(x) = x)
# Check: https://www.tensorflow.org/api_docs/python/tf/keras/layers/Dense
def linear_model():
model = keras.Sequential([
layers.Dense(1, use_bias=True, input_shape=(1,), name='layer')
])
# Using adam optimizer
optimizer = tf.keras.optimizers.Adam(
learning_rate=0.01, beta_1=0.9, beta_2=0.99, epsilon=1e-05, amsgrad=False,
name='Adam')
# Check: https://www.tensorflow.org/api_docs/python/tf/keras/Model
# loss: String (name of objective function), objective function or tf.keras.losses.Loss instance. See tf.keras.losses.
# optimizer: String (name of optimizer) or optimizer instance. See tf.keras.optimizers.
# metrics: List of metrics to be evaluated by the model during training and testing
model.compile(loss='mse', optimizer=optimizer, metrics=['mae','mse'])
return model
# Create model instant
model = linear_model()
# Model plot
tf.keras.utils.plot_model(
model, to_file='model.png', show_shapes=True, show_layer_names=True,
rankdir='TB', expand_nested=False, dpi=100
)
# Print the model summary
model.summary()
"""### Training loop
Fit the model to the data
* n_epochs: number of epochs
* validation_split: keep a portion of training data for unbiased validation
* verbose: set to 0 as we want a short summary and not all the details!!
* callbacks: A callback is a tool to customize the behavior of a the model during training, testing, etc.
"""
# params
n_epochs = 4000
batch_size = 256
n_idle_epochs = 100
n_epochs_log = 200
n_samples_save = n_epochs_log * trainInput.shape[0]
print('Checkpoint is saved for each {} samples'.format(n_samples_save))
# A mechanism that stops training if the validation loss is not improving for more than n_idle_epochs.
#See https://www.tensorflow.org/api_docs/python/tf/keras/callbacks/EarlyStopping for details.
earlyStopping = tf.keras.callbacks.EarlyStopping(monitor='val_loss', patience=n_idle_epochs, min_delta=0.001)
# Creating a custom callback to print the log after a certain number of epochs
# Check: https://www.tensorflow.org/api_docs/python/tf/keras/callbacks
predictions_list = []
class NEPOCHLogger(tf.keras.callbacks.Callback):
def __init__(self,per_epoch=100):
'''
display: Number of batches to wait before outputting loss
'''
self.seen = 0
self.per_epoch = per_epoch
def on_epoch_end(self, epoch, logs=None):
if epoch % self.per_epoch == 0:
print('Epoch {}, loss {:.2f}, val_loss {:.2f}, mae {:.2f}, val_mae {:.2f}, mse {:.2f}, val_mse {:.2f}'\
.format(epoch, logs['loss'], logs['val_loss'],logs['mae'], logs['val_mae'],logs['mse'], logs['val_mse']))
# Call the object
log_display = NEPOCHLogger(per_epoch=n_epochs_log)
# Include the epoch in the file name (uses `str.format`)
import os
checkpoint_path = "training/cp-{epoch:05d}.ckpt"
checkpoint_dir = os.path.dirname(checkpoint_path)
# Create a callback that saves the model's weights every n_samples_save
checkpointCallback = tf.keras.callbacks.ModelCheckpoint(
filepath=checkpoint_path,
verbose=1,
save_weights_only=True,
save_freq=n_samples_save)
# Save the weights using the `checkpoint_path` format
model.save_weights(checkpoint_path.format(epoch=0))
# Define the Keras TensorBoard callback.
logdir="logs/fit/" + datetime.now().strftime("%Y%m%d-%H%M%S")
tensorboard_callback = keras.callbacks.TensorBoard(log_dir=logdir)
history = model.fit(
trainInput, trainTarget, batch_size=batch_size,
epochs=n_epochs, validation_split = 0.1, verbose=0, callbacks=[earlyStopping,log_display,tensorboard_callback,checkpointCallback])
# %tensorboard --logdir logs
import numpy as np
import pandas as pd
import seaborn as sns
# The fit model returns the history object for each Keras model
# Let's explore what is inside history
print('keys:', history.history.keys())
# Returning the desired values for plotting and turn to numpy array
mae = np.asarray(history.history['mae'])
val_mae = np.asarray(history.history['val_mae'])
# Creating the data frame
num_values = (len(mae))
values = np.zeros((num_values,2), dtype=float)
values[:,0] = mae
values[:,1] = val_mae
# Using pandas to frame the data
steps = pd.RangeIndex(start=0,stop=num_values)
data = pd.DataFrame(values, steps, columns=["training-mae", "val-mae"])
# Plotting
sns.set(style="whitegrid")
sns.lineplot(data=data, palette="tab10", linewidth=2.5)
predictions = model.predict(testInput).flatten()
a = plt.axes(aspect='equal')
plt.scatter(predictions, testTarget, edgecolors=(0, 0, 0))
plt.xlabel('True Values')
plt.ylabel('Predictions')
lims = [0, 50]
plt.xlim(lims)
plt.ylim(lims)
_ = plt.plot(lims, lims)
"""### Track the model improvement progression"""
# Get the saved checkpoint files
checkpoints = []
for f_name in os.listdir(checkpoint_dir):
if f_name.startswith('cp-'):
file_with_no_ext = os.path.splitext(f_name)[0]
checkpoints.append(file_with_no_ext)
# Return unique list elements
checkpoints = list(set(checkpoints))
print('checkpoints:',checkpoints)
# Load all model checkpoints and evaluate for each
count = 0
model_improvement_progress = False
if model_improvement_progress:
for checkpoint in checkpoints:
count += 1
# Call model instant
model = linear_model()
# Restore the weights
path = os.path.join('training',checkpoint)
model.load_weights(path)
# Access to layer weights
layer = model.get_layer('layer')
w1,w0 = layer.get_weights()
w1 = float(w1[0])
w0 = float(w0[0])
# Draw the scatter plot of data
fig, ax = plt.subplots()
x = testInput
y = testTarget
ax.scatter(x, y, edgecolors=(0, 0, 0))
ax.set_xlabel('RM')
ax.set_ylabel('MEDV')
# Plot the line
y_hat = w1*x + w0
plt.plot(x, y_hat, '-r')
plt.savefig(os.path.join('/content/drive/linearregression', str(checkpoint)+'.png'))