-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathElectrical_Grid_Stability.py
232 lines (108 loc) · 3.44 KB
/
Electrical_Grid_Stability.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
#!/usr/bin/env python
# coding: utf-8
# # Import Libraries
# In[29]:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
get_ipython().run_line_magic('matplotlib', 'inline')
# # Load the Dataset
# In[2]:
df=pd.read_csv("grid_data.csv")
df.head(10)
# # Exploratory Data Analysis
# In[3]:
# summary of a DataFrame
df.info()
# In[4]:
# checking the presence of missing values in each column
df.isnull().sum()
# In[5]:
# descriptive statistics
df.describe()
# In[6]:
# Show the counts of observations in each category.
print(df['stabf'].value_counts())
sns.set_style('whitegrid')
sns.countplot(x='stabf',data=df, palette='YlGnBu_r')
# In[7]:
# distribution of observations in column 'stab'.
plt.figure(figsize=(8,4))
sns.distplot(df['stab'], color='r')
# In[8]:
# correlation heatmap
plt.figure(figsize=(14,10))
sns.heatmap(df.corr(), annot=True)
# # Train Test Split
# In[9]:
X = df.drop(['stab', 'stabf'],axis=1)
y = df['stab']
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X,y,test_size=0.2,random_state=42)
# # Scaling
# In[10]:
from sklearn.preprocessing import StandardScaler
scaler = StandardScaler()
X_train= scaler.fit_transform(X_train)
X_test = scaler.transform(X_test)
# # Build the Model
# In[11]:
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.layers import Dropout
# In[12]:
X_train.shape
# In[13]:
model = Sequential()
model.add(Dense(12, activation='elu'))
model.add(Dropout(0.5))
model.add(Dense(3,activation='elu'))
model.add(Dense(1))
model.compile(optimizer='adam', loss='mse')
# In[14]:
from tensorflow.keras.callbacks import EarlyStopping
early_stop = EarlyStopping(monitor='val_loss', mode='min', verbose=1, patience=10)
# In[15]:
model.fit(x=X_train,y=y_train.values,
validation_split=0.1,
batch_size=32,epochs=100, callbacks=[early_stop])
# In[16]:
# save the model
model.save('Electrical_Grid_Stability.h5')
# In[17]:
model.summary()
# In[18]:
losses = pd.DataFrame(model.history.history)
losses.plot()
# # Model Evaluation
# In[19]:
from sklearn.metrics import mean_squared_error,mean_absolute_error
predictions = model.predict(X_test)
# In[20]:
predictions
# In[21]:
mean_absolute_error(y_test,predictions)
# In[22]:
mean_squared_error(y_test,predictions)
# In[23]:
np.sqrt(mean_squared_error(y_test,predictions))
# In[24]:
def foo(t1, t2, t3, t4, p1, p2, p3, p4, g1, g2, g3, g4):
X=pd.DataFrame(data=np.array([[t1, t2, t3, t4, p1, p2, p3, p4, g1, g2, g3, g4]]))
X_test = scaler.transform(X)
prediction = model.predict(X_test)
print(prediction)
if prediction>=0:
return "Oops! the system is linearly unstable."
else:
return "Great! the system is stable."
# In[25]:
foo(4.689852, 4.007747, 1.478573, 3.733787, 4.041300, -1.410344, -1.238204, -1.392751, 0.269708, 0.250364, 0.164941, 0.482439)
# In[26]:
foo(2.042954, 8.514335, 8.173809, 5.466635, 3.783797, -1.639912,-0.662469, -1.481417, 0.154129, 0.944486, 0.053225, 0.499109)
# In[27]:
foo(6.530527, 6.781790, 4.349695, 8.673138, 3.492807, -1.390285, -1.532193, -0.570329, 0.073056, 0.505441, 0.378761, 0.942631)
# In[28]:
foo(3.392299, 1.274827, 2.954947, 6.894759, 4.349512, -1.663661, -0.952437, -1.733414, 0.502079, 0.567242, 0.285880, 0.366120)