-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweather_prediction_ann.py
148 lines (101 loc) · 3.63 KB
/
weather_prediction_ann.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
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
df=pd.read_csv('seattle-weather.csv')
df.head()
# Model Building
from sklearn import tree
from sklearn.model_selection import train_test_split
wcpy_db = df.copy(deep = True)
wcpy_db.drop(labels="date", axis=1, inplace=True)
y = wcpy_db.weather
X = wcpy_db.drop(labels='weather', axis=1)
# Splitting the Data into training and testing part to provide an environment to the model for cross validation
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25, random_state=39)
"""Artificial Neural Network"""
import tensorflow as tf
from sklearn.preprocessing import StandardScaler
sc = StandardScaler()
X_ann_train = sc.fit_transform(X_train)
X_ann_test = sc.transform(X_test)
# print(X_ann_train)
# label encode the output column
import numpy as np
# print(y_test)
from sklearn.preprocessing import LabelEncoder
le = LabelEncoder()
y_train_le = le.fit_transform(y_train)
y_test_le = le.fit_transform(y_test)
print(y_train_le)
# Label Encoding Scheme
# 0 - drizzle
# 1 - fog
# 2 - rain
# 3 - snow
# 4 - sun
y_train_le = y_train_le.reshape(-1, 1)
y_test_le = y_test_le.reshape(-1, 1)
from sklearn.compose import ColumnTransformer
from sklearn.preprocessing import OneHotEncoder
enc = OneHotEncoder()
enc.fit(y_train_le)
enc.fit(y_test_le)
y_ann_train = enc.transform(y_train_le).toarray()
y_ann_test = enc.transform(y_test_le).toarray()
# print(y_ann_train.shape)
# print(y_ann_test)
from tensorflow import keras
from keras import layers
import keras_tuner
from keras_tuner.tuners import RandomSearch
# tuning parameters
def model_test(hp):
model = keras.Sequential()
# adding base layer of our model number of neurons varry from 10 to 512
model.add(layers.Dense(units = hp.Int('units' , min_value = 10
, max_value = 512 ,
step = 64) ,
activation = 'relu' , input_shape = X_ann_train[0].shape))
# adding second layer of our model number of neurons varry from 10 to 512
model.add(layers.Dense(units = hp.Int('units' , min_value = 10
, max_value = 512 ,
step = 64) ,
activation = 'relu'))
# output layer with shape 5 as categorized into 5 category so no need to tune
model.add(layers.Dense(units=5 , activation = 'softmax'))
# compiling our model with varrying hyperparamters
model.compile(optimizer = keras.optimizers.Adam(hp.Choice('learning_rate',
values = [1e-2 , 1e-4 , 1e-3])),
loss = 'categorical_crossentropy' ,
metrics = ['accuracy'])
return model
tuner = RandomSearch(
model_test ,
objective = 'val_accuracy',
max_trials=5 ,
project_name = 'weatherprediction'
)
tuner.search_space_summary()
tuner.search(X_ann_train, y_ann_train, epochs = 100 ,validation_data = (X_ann_test , y_ann_test))
tuner.results_summary()
"""
out of the 5 trial we got max validation accuracy as 84.7%
Best parameters:
Hyperparameters:
units: 10
learning_rate: 0.0
Score: 0.8469945192337036
"""
best_model = tuner.get_best_models()[0]
best_model.fit(X_ann_train, y_ann_train, batch_size=32, epochs = 100)
predicted = best_model.predict(X_ann_test)
testi = [[5 ,5 , 5 , 5]]
testi = np.array(testi)
predicty = best_model.predict(testi)
index = np.argmax(predicty)
from keras.models import load_model
best_model.save('weather.h5')
modeldone = load_model('weather.h5')
predictyy = modeldone.predict(testi)
indexy = np.argmax(predictyy)
print(indexy)