-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConocoPhilips.py
More file actions
230 lines (127 loc) · 4.29 KB
/
ConocoPhilips.py
File metadata and controls
230 lines (127 loc) · 4.29 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
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
#!/usr/bin/env python
# coding: utf-8
# ### Importing the dataset
# In[119]:
import pandas as pd
# read the file
train_set = pd.read_csv("equip_failures_training_set.csv")
y = train_set.iloc[:, 1].values
# ### Adding na columns
# In[ ]:
# adding the binary na columns for each sensor value
for key in train_set:
if "sensor" in key:
train_set[key + "_na"] = [1 if train_set[key][i] == "na" else 0 for i in range(len(train_set[key]))]
#
# In[ ]:
X = train_set.iloc[:, 2:].values
import numpy as np
X = np.where(X == "na", 0, X)
# In[ ]:
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 = 0)
# ### Feature Scaling
# In[ ]:
from sklearn.preprocessing import StandardScaler
sc = StandardScaler()
X_train = sc.fit_transform(X_train)
# ### Building the ANN model
# In[ ]:
import keras
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import Dropout
from keras.layers import Conv1D
# Initialising the ANN
classifier = Sequential()
# Adding the input layer and the first hidden layer
classifier.add(Dense(units = 170, kernel_initializer = 'uniform', activation = 'relu', input_dim = 340))
## Adding the convolution layer
#classifier.add(Conv1D(10, (3), activation = 'tanh'))
# Adding the next five hidden layers
for i in range(3):
classifier.add(Dense(units = 170, kernel_initializer = 'uniform', activation = 'relu'))
classifier.add(Dropout(rate=0.2))
# Adding the output layer
classifier.add(Dense(units = 1, kernel_initializer = 'uniform', activation = 'sigmoid'))
adamOp = keras.optimizers.Adam(lr = 0.0005, beta_1 = 0.9, beta_2 = 0.999, epsilon = None, amsgrad = False)
# Compiling the ANN
classifier.compile(optimizer = adamOp, loss = 'binary_crossentropy', metrics = ['accuracy'])
# ### Fitting the ANN normally
# In[ ]:
# Fitting the ANN to the Training set
classifier.fit(X_train, y_train, batch_size = 10, epochs = 50, validation_split = 0.1)
# ### Fitting the ANN with GridSearchCV
# In[ ]:
# In[ ]:
# Predicting the Test set results
y_pred = classifier.predict(X_test)
y_pred = (y_pred > 0.5)
# In[ ]:
# Making the Confusion Matrix
from sklearn.metrics import confusion_matrix
cm = confusion_matrix(y_test, y_pred)
print(cm)
# ### Saving the model
# In[ ]:
classifier_json = classifier.to_json()
with open("classifier.json", "w") as json_file:
json_file.write(classifier_json)
classifier.save_weights("weights.h5")
# ## Building an SOM
# In[ ]:
from sklearn.preprocessing import MinMaxScaler
scale_object = MinMaxScaler(feature_range = (0,1))
X_som = scale_object.fit_transform(X)
# In[ ]:
from minisom import MiniSom
som = MiniSom(x = 10, y = 10, input_len = 340, sigma = 1.0, learning_rate = 0.5)
som.random_weights_init(X_som)
som.train_random(data = X_som, num_iteration = 100)
# In[ ]:
from pylab import bone, pcolor, colorbar, plot, show
bone()
pcolor(som.distance_map().T)
colorbar()
markers = ['o', 's']
colors = ['r', 'g']
for i, x in enumerate(X_som):
w = som.winner(x)
plot(w[0] + 0.5,
w[1] + 0.5,
markers[y[i]],
markeredgecolor = colors[y[i]],
markerfacecolor = 'None',
markersize = 10,
markeredgewidth = 2)
show()
# ## Loading the saved model to predict test results
# In[ ]:
test_set = pd.read_csv("equip_failures_test_set.csv")
for key in test_set:
if "sensor" in key:
test_set[key + "_na"] = [1 if test_set[key][i] == "na" else 0 for i in range(len(test_set[key]))]
# In[ ]:
new_X = test_set.iloc[:, 1:].values
new_X = np.where(new_X == "na", 0, new_X)
new_X = sc.fit_transform(new_X)
# In[ ]:
from keras.models import model_from_json
json_file = open("classifier.json", "r")
loaded_json = json_file.read()
json_file.close()
classifier = model_from_json(loaded_json)
classifier.load_weights("weights.h5")
# In[ ]:
new_predictions = classifier.predict(new_X)
new_predictions = new_predictions > 0.5
np.savetxt("test_results.txt", new_predictions, fmt = "%d")
# In[ ]:
with open("test_results.txt","r") as pred_file:
fout = open("results_1.txt", "w")
fout.write("id,target\n")
for idx, val in enumerate(pred_file):
fout.write(str(str(idx + 1) + "," + str(val)))
# In[ ]:
print(new_X.shape)
print(new_predictions[])