-
Notifications
You must be signed in to change notification settings - Fork 0
/
crop_pred _MAIN_SCRIPT.py
469 lines (301 loc) · 9.61 KB
/
crop_pred _MAIN_SCRIPT.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
#!/usr/bin/env python
# coding: utf-8
# In[20]:
# import tensorflow as tf
# tf.logging.set_verbosity(tf.logging.ERROR)
import numpy as np
import pandas as pd
import sklearn
import pickle
# In[21]:
from sklearn.preprocessing import OneHotEncoder, LabelEncoder
from keras.callbacks import ModelCheckpoint
from keras.models import Sequential
from keras.layers import Dense, Activation, Flatten
from sklearn import preprocessing
from scipy import stats
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import MinMaxScaler
# In[22]:
df = pd.read_csv("crop_data.csv")
# df=np.array(df)
# print(X)
df4= pd.read_csv("crop_data.csv")
# In[23]:
df.Crop=df.Crop.str.strip()
df4.Crop=df4.Crop.str.strip()
# In[24]:
df=df[df.Crop!="Coconut"]
df=df[df.Crop!="Rice"]
df4=df4[df4.Crop!="Coconut"]
df4=df4[df4.Crop!="Rice"]
print(df.shape)
print(df)
print(df4)
# In[25]:
#df4.to_csv('{}.csv'.format('crop_data1'),index=False)
# In[70]:
x=np.array(df4.Area)
y=np.array(df4.Production)
fig, ax = plt.subplots()
ax.scatter(x[1:1000], y[1:1000])
ax.plot(1000, 1000, 'k--', lw=4)
plt.ylim(0, 100000)
plt.xlim(0, 100000)
ax.set_xlabel('Area')
ax.set_ylabel('Production')
plt.show()
# In[75]:
z=np.array(df4.Rainfall)
y=np.array(df4.Production)
fig, ax = plt.subplots()
ax.scatter(z[1:1000], y[1:1000])
ax.plot(1000, 1000, 'k--', lw=4)
plt.ylim(0, 1000)
plt.xlim(0, 1000)
ax.set_xlabel('rainfall')
ax.set_ylabel('Production')
# In[26]:
labelencoder_X = LabelEncoder()
df.Crop = labelencoder_X.fit_transform(df.Crop)
df.District_Name= labelencoder_X.fit_transform(df.District_Name)
df.State_Name= labelencoder_X.fit_transform(df.State_Name)
df.Season= labelencoder_X.fit_transform(df.Season)
print(df)
# In[27]:
df_temp_Crop=df[['Crop']].values
df_temp_State=df[['State_Name']].values
df_temp_Dis=df[['District_Name']].values
df_temp_Season=df[['Season']].values
# inverse transform
# inverse = scaler.inverse_transform(normalized)
# X=df[['Crop','District_Name','State_Name']].values
onehotencoder = OneHotEncoder( )
df_temp_Crop = onehotencoder.fit_transform(df_temp_Crop).toarray()
print(df_temp_Crop.shape)
df_temp_Crop = df_temp_Crop[:,1:]
print(df_temp_Crop.shape)
df_temp_State = onehotencoder.fit_transform(df_temp_State).toarray()
print(df_temp_State.shape)
df_temp_State = df_temp_State[:,1:]
print(df_temp_State.shape)
df_temp_Dis = onehotencoder.fit_transform(df_temp_Dis).toarray()
print(df_temp_Dis.shape)
df_temp_Dis = df_temp_Dis[:,1:]
print(df_temp_Dis.shape)
df_temp_Season = onehotencoder.fit_transform(df_temp_Season).toarray()
print(df_temp_Season.shape)
df_temp_Season = df_temp_Season[:,1:]
print(df_temp_Season.shape)
# In[28]:
df_temp_year=df[['Crop_Year']].values
df_rainfall=df[['Rainfall']].values
scaler = MinMaxScaler()
scaler.fit(df_temp_year)
df_temp_year = scaler.transform(df_temp_year)
scaler.fit(df_rainfall)
df_rainfall = scaler.transform(df_rainfall)
print(df_rainfall)
print(df_temp_year)
print(df)
# In[29]:
df=df.dropna()
# In[30]:
#area=np.array(df.Area).reshape(-1,1)
#rainfall=np.array(df.Rainfall).reshape(-1,1)
#print(area.shape)
X=np.concatenate((df_temp_State,df_temp_Dis,df_temp_year,df_temp_Season,df_temp_Crop,df_rainfall),axis=1)
# X=pd.concat([df_temp_Crop,df_temp_State,df_temp_Dis],axis='columns')
print(X.shape)
# In[31]:
df7=pd.DataFrame(X);
print(df7)
print(df4)
# In[32]:
#df7.to_csv('{}.csv'.format('encoded'),index=False)
# In[33]:
df2=pd.DataFrame(df4.State_Name.unique());
df2.rename(columns={0:'State_Name'},inplace=True);
df3=pd.DataFrame(df4.District_Name.unique());
df3.rename(columns={0:'District_Name'},inplace=True);
df5=pd.DataFrame(df4.Season.unique());
df5.rename(columns={0:'Season'},inplace=True);
df6=pd.DataFrame(df4.Crop.unique());
df6.rename(columns={0:'Crop'},inplace=True);
print(df2);
print(df3);
print(df5);
print(df6);
# In[34]:
# df8=pd.DataFrame(df_temp_Crop);
# df9=pd.DataFrame(df_temp_State);
# df10=pd.DataFrame(df_temp_Dis);
# df11=pd.DataFrame(df_temp_Season);
# print(df8)
# print(df9)
# print(df10)
# print(df11)
# In[35]:
#df2=pd.DataFrame(np.array(X));
#print(df2)
#df2.to_csv('{}.csv'.format('test_data'),index=False)
# In[36]:
#c=df2[df2.State_Name == 'Andaman and Nicobar Islands'].index
#df9.iloc[c,:]
# In[37]:
# g=X[4][:]
# print(g.size)
# In[38]:
# Y=df[['Production']].values/df[['Area']].values
# print(Y)
# print(Y.shape)
Y=np.array(df[['Production']].values)
#imputer=Imputer(missing_values='NaN',strategy='mean')
#imputer=imputer.fit(Y)
#Y=imputer.transform(Y)
Y=Y/(np.array(df[['Area']].values))
area=np.array(df.Area);
Y=np.concatenate((Y,area.reshape(-1,1)),axis=1)
print(Y[:,1])
#scaler = MinMaxScaler()
#scaler.fit(Y)
#Y = scaler.transform(Y)
print(Y)
# Y.describe()
# Y=stats.zscore(Y, axis = 1)
#print(Y)
print(Y.shape)
print(X.shape)
X_train,X_test,Y_train1,Y_test1=train_test_split(X,Y,test_size=0.2,random_state=0)
# In[39]:
col=np.size(Y_train1,1);
Y_train=Y_train1[:,0:col-1];
col=np.size(Y_test1,1);
Y_test=Y_test1[:,0:col-1];
print(Y_train.shape)
print(Y_test.shape)
# In[40]:
print(X.shape)
# In[41]:
NN_model = Sequential()
# In[42]:
# The Input Layer :
NN_model.add(Dense(128, kernel_initializer='normal',input_dim =712 , activation='relu'))
# The Hidden Layers :
NN_model.add(Dense(256, kernel_initializer='normal',activation='relu'))
NN_model.add(Dense(256, kernel_initializer='normal',activation='relu'))
NN_model.add(Dense(128, kernel_initializer='normal',activation='relu'))
NN_model.add(Dense(64, kernel_initializer='normal',activation='relu'))
NN_model.add(Dense(32, kernel_initializer='normal',activation='relu'))
# The Output Layer :
NN_model.add(Dense(1, kernel_initializer='normal',activation='linear'))
# Compile the network :
NN_model.compile(loss='mean_absolute_error', optimizer='adam', metrics=['mean_absolute_error'])
NN_model.summary()
# In[43]:
checkpoint_name = 'Weights-{epoch:03d}--{val_loss:.5f}.hdf5'
checkpoint = ModelCheckpoint(checkpoint_name, monitor='val_loss', verbose = 1, save_best_only = True, mode ='auto')
callbacks_list = [checkpoint]
# In[25]:
NN_model.fit(X_train, Y_train, epochs=100, validation_split = 0.2, callbacks=callbacks_list,batch_size=10000)
# In[44]:
wights_file = 'Weights-100--6.54972.hdf5' # choose the best checkpoint
NN_model.load_weights(wights_file) # load it
NN_model.compile(loss='mean_absolute_error', optimizer='adam', metrics=['mean_absolute_error'])
# In[ ]:
def make_submission(prediction, sub_name,Y_test,X_test,Yield_pred,test_Yield):
my_submission = pd.DataFrame({'Id':1,'Priceperarea':prediction,'Y_test':Y_test,'Yield_pred':Yield_pred,'test_yield':test_Yield,})
my_submission.to_csv('{}.csv'.format(sub_name),index=False)
print('A submission file has been made')
predictions = NN_model.predict(X_test)
# inverse transform
# X_test = scaler.inverse_transform(X_test)
import matplotlib.pyplot as plt
Yield_pred=np.multiply(predictions,Y_test1[:,1].reshape(-1,1))
test_Yield=np.multiply(Y_test[:,0],Y_test1[:,1])
print(Yield_pred.shape)
fig, ax = plt.subplots()
ax.scatter(test_Yield[1:1000], Yield_pred[1:1000])
ax.plot(1000, 1000, 'k--', lw=4)
ax.set_xlabel('Measured');
ax.set_ylabel('Predicted');
plt.savefig('foo.png')
plt.show()
t=np.mean((test_Yield-Yield_pred)**2)
print(t)
# print(Yield_pred.reshape(-1,).shape)
# print(Y_test[:,0].shape)
# print(Y_test1[:,1].shape)
# print(test_Yield.shape)
# make_submission(predictions[:,0],'submission(NN)',Y_test[:,0],X_test[:,0],Yield_pred.reshape(-1,),test_Yield)
# print(predictions[:,0])
# In[65]:
pickle.dump(NN_model,open('model.pkl','wb'))
# In[66]:
from flask import Flask, request, redirect, url_for, flash, jsonify
import numpy as np
import pandas as pd
import pickle as p
import json
# In[67]:
df12 = pd.read_csv("crop_data1.csv")
# df=np.array(df)
# print(X)
df13= pd.read_csv("encoded.csv")
# In[68]:
df2=pd.DataFrame(df12.State_Name.unique());
df2.rename(columns={0:'State_Name'},inplace=True);
df3=pd.DataFrame(df12.District_Name.unique());
df3.rename(columns={0:'District_Name'},inplace=True);
df5=pd.DataFrame(df12.Season.unique());
df5.rename(columns={0:'Season'},inplace=True);
df6=pd.DataFrame(df12.Crop.unique());
df6.rename(columns={0:'Crop'},inplace=True);
print(df2);
print(df3);
print(df5);
print(df6);
# In[69]:
# df9=pd.DataFrame(df13.iloc[:,:30]);
# df10=pd.DataFrame(df13.iloc[:,30:584]);
# df11=pd.DataFrame(df13.iloc[:,585:590]);
# df8=pd.DataFrame(df13.iloc[:,590:711]);
# print(df8)
# print(df9)
# print(df10)
# print(df11)
# In[53]:
# # app = Flask(__name__)
# #@app.route('/makecalc', methods=['POST'])
# #def makecalc():
# # data = request.get_json()
# model1='model.pkl'
# model = p.load(open(model1, 'rb'))
# df31=df9.iloc[df2[df2.State_Name == 'Andaman and Nicobar Islands'].index,:]
# # print(df31)
# df32=df10.iloc[df3[df3.District_Name =='NICOBARS'].index,:]
# df33=df15=pd.DataFrame(np.array(2012).reshape(-1,1))
# df34=df11.iloc[df5[df5.Season == 'kharif'].index,:]
# df35=df8.iloc[df6[df6.Crop == 'Arecanut'].index,:]
# df36=df16=pd.DataFrame(np.array(35).reshape(-1,1))
# df14= pd.concat([df31, df32,df33,df34,df35,df36], axis=1, sort=False)
# df37=np.array(df14)
# # print(df37)
# prediction = model.predict(df37)
# print(prediction)
# # return jsonify(prediction)
# # if __name__ == '__main__':
# # modelfile = 'model.pkl'
# # model = p.load(open(modelfile, 'rb'))
# # app.run(debug=True,host='localhost', port=5000)
# In[81]:
# import requests
# import json
# url ='http://localhost:5000/'
# #or 8888 and the host to socket.gethostname()
# data = g.tolist()
# j_data = json.dumps(data)
# headers = {'content-type': 'application/json', 'Accept-Charset': 'UTF-8'}
# r = requests.post(url, data=j_data, headers=headers)
# print(r,r.text)
# In[ ]: