-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathregression_model.py
More file actions
64 lines (49 loc) · 1.96 KB
/
regression_model.py
File metadata and controls
64 lines (49 loc) · 1.96 KB
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
import pandas as pd
import numpy as np
from utils import (
Sequential, layers, optimizers, activations,
losses, metrics, constraints, callbacks)
URL = ('http://archive.ics.uci.edu/ml/machine-learning-databases/'
'auto-mpg/auto-mpg.data')
COLUMN_NAMES = ['MPG', 'Cylinders', 'Displacement', 'Horsepower',
'Weight', 'Acceleration', 'Model Year', 'Origin']
dataset = pd.read_csv(
URL, names=COLUMN_NAMES, na_values='?', comment='\t',
sep=' ', skipinitialspace=True)
# Drop rows with unknown values
dataset = dataset.dropna()
# Convert from numeric to categorical
dataset['Origin'] = dataset['Origin'].map(
{1: 'USA', 2: 'Europe', 3: 'Japan'})
dataset = pd.get_dummies(dataset, columns=['Origin'])
train_dataset = dataset.sample(frac=0.8, random_state=0)
test_dataset = dataset.drop(train_dataset.index)
# Separate label from features
train_features = train_dataset.copy()
test_features = test_dataset.copy()
train_labels = train_features.pop('MPG')
test_labels = test_features.pop('MPG')
normalizer = layers.Normalization(axis=-1)
normalizer.adapt(np.array(train_features))
# Multiply the learning rate by 0.5 whenever the best validation
# loss does not improve for five consecutive epochs.
lr_scheduler = callbacks.ReduceLROnPlateau(factor=0.5, patience=5)
def create_hidden_layer(units: int):
return layers.Dense(
units, activation=activations.relu,
kernel_initializer='he_normal',
kernel_constraint=constraints.max_norm(1.))
model = Sequential([
normalizer, create_hidden_layer(64),
create_hidden_layer(64), layers.Dense(1)])
model.compile(
optimizer=optimizers.AdamW(),
loss=losses.mean_squared_error,
metrics=[metrics.mean_squared_error])
model.fit(
train_features, train_labels, validation_split=0.2,
callbacks=[lr_scheduler], epochs=100)
model.evaluate(test_features, test_labels)
prediction = model.predict(test_features)
print('Prediction: ', prediction[0])
print('Label: ', test_labels.iloc[0])