-
Notifications
You must be signed in to change notification settings - Fork 0
/
nn3.py
62 lines (50 loc) · 1.53 KB
/
nn3.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
from keras.models import Sequential
from keras.layers import Dense
import matplotlib.pyplot as plt
import numpy as np
import csv, math
import pandas as pd
np.random.seed(10)
def split_data(data, period=1):
X, Y = [], []
for i in range(len(data)-period-1):
tmp = data[i:(i+period), 0]
X.append(tmp)
Y.append(data[i + period, 0])
return np.array(X), np.array(Y)
#city = input("enter the city name (for ex: nyc or chicago) : ")
citty = ['atlanta','chicago','nyc','portland']
for city in citty:
filename = "./temperatures/"+city+".csv"
dataframe = pd.read_csv(filename,usecols=[3])
data = dataframe.values
for i in range(data.shape[0]):
if pd.isna(data[i][0]):
data[i][0]=data[i-1][0]
data = data.astype('float32')
train_size = int(len(data)*0.995)
test_size = len(data)-train_size
train = data[0:train_size,:]
test = data[train_size:len(data),:]
period = 1
X_train, Y_train = split_data(train, period)
X_test, Y_test = split_data(test, period)
model = Sequential()
#hidden layer
model.add(Dense(8, input_dim=period, activation='relu'))
#output layer
model.add(Dense(1))
#create the architecture
model.compile(loss='mean_squared_error', optimizer='adam')
#train the network
model.fit(X_train, Y_train, epochs=2, batch_size=2, verbose=2)
Predict = model.predict(X_test)
ans = Predict[:30]
print(ans)
plt.plot(Y_test, color='orange')
plt.plot(Predict)
plt.title(city.capitalize())
plt.xlabel('No. of Days')
plt.ylabel('Temperature in degree Celsius')
plt.legend(["Actual Test Data", "Predicted Temperature"])
plt.show()