-
Notifications
You must be signed in to change notification settings - Fork 0
/
waterTemperaturePredictor.py
182 lines (140 loc) · 4.98 KB
/
waterTemperaturePredictor.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
from __future__ import absolute_import, division, print_function
import pathlib
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
from google.cloud import bigquery
print(tf.__version__)
def get_dataset(client, dataset_id):
dataset = client.get_dataset(dataset_id)
full_dataset_id = "{}.{}".format(dataset.project, dataset.dataset_id)
friendly_name = dataset.friendly_name
print(
"Got dataset '{}' with friendly_name '{}'.".format(
full_dataset_id, friendly_name
)
)
# View dataset properties
print("Description: {}".format(dataset.description))
print("Labels:")
labels = dataset.labels
if labels:
for label, value in labels.items():
print("\t{}: {}".format(label, value))
else:
print("\tDataset has no labels defined.")
# View tables in dataset
print("Tables:")
tables = list(client.list_tables(dataset)) # API request(s)
if tables:
for table in tables:
print("\t{}".format(table.table_id))
else:
print("\tThis dataset does not contain any tables.")
def get_rows_as_dataframe(client, dataset_id, table_id):
QUERY = (
f'SELECT * FROM `{dataset_id}.{table_id}` '
'WHERE waterTemperature IS NOT NULL AND ambientTemperature != 0 and ambientHumidity != 0')
print("{}".format(QUERY))
df = client.query(QUERY).to_dataframe() # API request
return df
dataset_id = 'selfhydro-197504.selfhydro'
table_id = 'selfhydro_state'
client = bigquery.Client()
get_dataset(client, dataset_id)
df = get_rows_as_dataframe(client, dataset_id, table_id)
sorted_df = df.sort_values(by='time')
def clean_data(dataframe):
dataframe.pop('waterLevel')
dataframe.pop('deviceId')
dataframe = dataframe.dropna()
return dataframe
cleaned_dataframe = clean_data(sorted_df)
print(cleaned_dataframe)
stats = cleaned_dataframe.describe()
print(stats)
cleaned_dataframe.pop('time')
sns.pairplot(cleaned_dataframe[["ambientTemperature", "ambientHumidity", "waterTemperature", "waterElectricalConductivity"]], diag_kind="kde", kind="reg", palette="husl")
train_dataset = cleaned_dataframe.sample(frac=0.8, random_state=0)
test_dataset = cleaned_dataframe.drop(train_dataset.index)
train_stats = train_dataset.describe()
train_stats.pop("waterTemperature")
train_stats = train_stats.transpose()
print(train_stats)
train_labels = train_dataset.pop('waterTemperature')
test_labels = test_dataset.pop('waterTemperature')
def norm(x):
return (x - train_stats['mean']) / train_stats['std']
normed_train_data = norm(train_dataset)
normed_test_data = norm(test_dataset)
def build_model():
model = keras.Sequential([
layers.Dense(64, activation=tf.nn.relu, input_shape=[len(train_dataset.keys())]),
layers.Dense(64, activation=tf.nn.relu),
layers.Dense(1)
])
optimizer = tf.keras.optimizers.RMSprop(0.001)
model.compile(loss='mean_squared_error',
optimizer=optimizer,
metrics=['mean_absolute_error', 'mean_squared_error'])
return model
model = build_model()
model.summary()
example_batch = normed_train_data[:10]
example_result = model.predict(example_batch)
example_result
class PrintDot(keras.callbacks.Callback):
def on_epoch_end(self, epoch, logs):
if epoch % 100 == 0:
print('')
print('.', end='')
EPOCHS = 1000
early_stop = keras.callbacks.EarlyStopping(monitor='val_loss', patience=10)
history = model.fit(normed_train_data, train_labels, epochs=EPOCHS,
validation_split = 0.2, verbose=0, callbacks=[early_stop, PrintDot()])
hist = pd.DataFrame(history.history)
hist['epoch'] = history.epoch
hist.tail()
def plot_history(history):
hist = pd.DataFrame(history.history)
hist['epoch'] = history.epoch
plt.figure()
plt.xlabel('Epoch')
plt.ylabel('Mean Abs Error [MPG]')
plt.plot(hist['epoch'], hist['mean_absolute_error'],
label='Train Error')
plt.plot(hist['epoch'], hist['val_mean_absolute_error'],
label = 'Val Error')
plt.ylim([0,5])
plt.legend()
plt.figure()
plt.xlabel('Epoch')
plt.ylabel('Mean Square Error [$MPG^2$]')
plt.plot(hist['epoch'], hist['mean_squared_error'],
label='Train Error')
plt.plot(hist['epoch'], hist['val_mean_squared_error'],
label = 'Val Error')
plt.ylim([0,20])
plt.legend()
plot_history(history)
test_predictions = model.predict(normed_test_data).flatten()
print("\n")
print(test_dataset)
print(test_labels)
print(test_predictions)
plt.scatter(test_labels, test_predictions)
plt.xlabel('True Values [Water Temperature]')
plt.ylabel('Predictions [Water Temperature]')
plt.axis('equal')
plt.axis('square')
plt.xlim([0,plt.xlim()[1]])
plt.ylim([0,plt.ylim()[1]])
_ = plt.plot([-100, 100], [-100, 100])
error = test_predictions - test_labels
plt.hist(error, bins = 25)
plt.xlabel("Prediction Error [Water Temperature]")
_ = plt.ylabel("Count")
plt.show()