-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreturnCrop.py
100 lines (87 loc) · 3.34 KB
/
returnCrop.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
# %matplotlib inline
from matplotlib import pyplot as plt
import pandas as pd
import numpy as np
from glob import glob
import numpy as np
from sklearn import datasets, linear_model
from sklearn.svm import SVR
def returnCrop(state,area,capital):
xls_data = sorted(glob('Dataset/*.xls'))
# state = "Maharashtra"
apple = []
year = []
sheetnames = []
required_elements = [5,6,7,8,9,11,12,13,14,15,19]
for xls in xls_data:
xl = pd.ExcelFile(xls)
sheetnames = sheetnames + xl.sheet_names
y = set(sheetnames)
y = list(y)
items = np.zeros((len(y),11,11))
possible_crops = []
for xls in xls_data:
xl = pd.ExcelFile(xls)
for sheet in sorted(xl.sheet_names):
if sheet.startswith("Coco"):
continue
df = xl.parse(sheet, skiprows=3)
df.iloc[6,1] = df.iloc[0,1]
df.iloc[0,1] = np.nan
df.iloc[14,1] = df.iloc[7,1]
df.iloc[7,1] = np.nan
df = df[df.iloc[:,1].notnull()]
df.drop(df.columns[[0,2]],axis=1,inplace=True)
df = df.iloc[:21,:]
# df[state].shape
if state in df.columns:
possible_crops.append(sheet)
try:
for i,element in enumerate(required_elements):
items[y.index(sheet),i,int(xls[8:12])-2004] = df[state].iloc[element]
except:
pass
year.append([int(xls[8:12])])
for i in range(0,len(items)):
for j in range(0,len(items[0])):
for k in range(0,len(items[0][0])):
if np.isnan(items[i,j,k]):
items[i,j,k] = 0
for i in range(0,len(items)):
for j in range(0,len(items[0])):
for k in range(0,len(items[0][0])):
if np.isnan(items[i,j,k]):
items[i,j,k] = np.mean(items[i][j])
models = [[0 for y in range(0,len(items[0]))] for x in range(0,len(items))]
for i in range(0,len(items)):
for j in range(0,len(items[0])):
models[i][j] = linear_model.LinearRegression()
models[i][j].fit(year, items[i,j])
# svr_rbf = SVR(kernel='rbf', C=1e3, gamma=0.1)
# models[i][j] = svr_rbf.fit(year, items[i,j])
# svr_rbf = SVR(kernel='rbf', C=1e3, gamma=0.1)
# svm = svr_rbf.fit(year, apple)
# y_rbf = svm.predict(y_trend)
# plt.plot(y_rbf)
all_crops=[]
ans = np.zeros((len(y),11))
cost = np.zeros((len(y)))
gain = np.zeros((len(y)))
supp = 1500
for i in range(0,len(items)):
for j in range(0,len(items[0])):
ans[i][j] = models[i][j].predict(2017)
max_num=0
max_index=0
for i in range(0,len(items)):
for j in range(0,int(len(ans[0])/2)):
cost[i]+=ans[i][j]*ans[i][j+5]
# print((ans[i][len(items[0])-1]*supp-cost[i])*2)
all_crops.append((int(ans[i][len(items[0])-1])*int(supp)-int(cost[i]))*int(area))
if ((int(ans[i][len(items[0])-1])*int(supp)-int(cost[i]))*int(area)) > max_num:
max_num = ((int(ans[i][len(items[0])-1])*int(supp)-int(cost[i]))*int(area))
max_index = i
# return y[max_index], ans[max_index]
# print(max(all_crops),y[max_index])
crops_possible = list(set(possible_crops))
return (y[max_index], ans[max_index], crops_possible)