-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprojectketiga_rere.py
More file actions
138 lines (108 loc) · 3.65 KB
/
projectketiga_rere.py
File metadata and controls
138 lines (108 loc) · 3.65 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
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
# -*- coding: utf-8 -*-
"""projectketiga_rere.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1psvRxwNDU01bJaCooJ6Qx-4quWDUhpfP
"""
import pandas as pd
from google.colab import files
filesnya = files.upload()
df = pd.read_csv('weather-data.csv')
df.head(10)
# total data
df.shape
#info datanya
df.info()
# cek null
df.isnull().sum()
df['datetime_utc']=pd.to_datetime(df['datetime_utc'])
df['datetime_utc'].head()
df[' _tempm'].fillna(df[' _tempm'].mean(), inplace=True)
df = df[['datetime_utc',' _tempm' ]]
df.head()
df.info()
delhi=df[['datetime_utc',' _tempm']].copy()
delhi['just_date'] = delhi['datetime_utc'].dt.date
delhifinal=delhi.drop('datetime_utc',axis=1)
delhifinal.set_index('just_date', inplace= True)
delhifinal.head()
#info delhi
delhifinal.info()
import matplotlib.pyplot as plt
plt.figure(figsize=(20,8))
plt.plot(delhifinal)
plt.title('Delhi Weather')
plt.xlabel('Date')
plt.ylabel('temperature')
plt.show()
#mendapatkan data values
date = df['datetime_utc'].values
temp = df[' _tempm'].values
import tensorflow as tf
def windowed_dataset(series, window_size, batch_size, shuffle_buffer):
series = tf.expand_dims(series, axis=-1)
ds = tf.data.Dataset.from_tensor_slices(series)
ds = ds.window(window_size + 1, shift=1, drop_remainder=True)
ds = ds.flat_map(lambda w: w.batch(window_size + 1))
ds = ds.shuffle(shuffle_buffer)
ds = ds.map(lambda w: (w[:-1], w[-1:]))
return ds.batch(batch_size).prefetch(1)
from sklearn.model_selection import train_test_split
x_train, x_test, y_train, y_test = train_test_split(temp, date, test_size = 0.2, random_state = 0 , shuffle=False)
print(len(x_train), len(x_test))
# model
from keras.layers import Dense, LSTM
data_x_train = windowed_dataset(x_train, window_size=60, batch_size=100, shuffle_buffer=5000)
data_x_test = windowed_dataset(x_test, window_size=60, batch_size=100, shuffle_buffer=5000)
model = tf.keras.models.Sequential([
tf.keras.layers.Conv1D(filters=32, kernel_size=5,
strides=1, padding="causal",
activation="relu",
input_shape=[None, 1]),
tf.keras.layers.LSTM(64, return_sequences=True),
tf.keras.layers.LSTM(64, return_sequences=True),
tf.keras.layers.Dense(30, activation="relu"),
tf.keras.layers.Dense(10, activation="relu"),
tf.keras.layers.Dense(1),
tf.keras.layers.Lambda(lambda x: x * 400)
])
lr_schedule = tf.keras.callbacks.LearningRateScheduler(
lambda epoch: 1e-8 * 10**(epoch / 20))
optimizer = tf.keras.optimizers.SGD(lr=1e-8, momentum=0.9)
model.compile(loss=tf.keras.losses.Huber(),
optimizer=optimizer,
metrics=["mae"])
max = df[' _tempm'].max()
print('Max value : ' )
print(max)
min = df[' _tempm'].min()
print('Min Value : ')
print(min)
x = (90.0 - 1.0) * (10 / 100)
print(x)
# metode callback
class myCallback(tf.keras.callbacks.Callback):
def on_epoch_end(self, epoch, logs={}):
if(logs.get('mae')< x):
self.model.stop_training = True
print("\nMAE of the model < 10% of data scale")
callbacks = myCallback()
tf.keras.backend.set_floatx('float64')
history = model.fit(data_x_train ,epochs=100, validation_data=data_x_test, callbacks=[callbacks])
# plot of mae
import matplotlib.pyplot as plt
plt.plot(history.history['mae'])
plt.plot(history.history['val_mae'])
plt.title('MAE')
plt.ylabel('mae')
plt.xlabel('epoch')
plt.legend(['train', 'test'], loc='upper left')
plt.show()
# plot of loss
plt.plot(history.history['loss'])
plt.plot(history.history['val_loss'])
plt.title('Model Loss')
plt.ylabel('loss')
plt.xlabel('epoch')
plt.legend(['train', 'test'], loc='upper left')
plt.show()