-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathforecast_helper.py
266 lines (234 loc) Β· 8.98 KB
/
forecast_helper.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
import numpy as np
import pandas as pd
import seaborn as sns
from datetime import datetime
from sklearn.preprocessing import MinMaxScaler
from sklearn.tree import DecisionTreeRegressor
import random
from commodity import Commodity
#commodity_list = []
annual_rainfall = [29, 21, 37.5, 30.7, 52.6, 150, 299, 251.7, 179.2, 70.5, 39.8, 10.9]
base = {
"Paddy": 1245.5,
"Arhar": 3200,
"Bajra": 1175,
"Barley": 980,
"Copra": 5100,
"Cotton": 3600,
"Sesamum": 4200,
"Gram": 2800,
"Groundnut": 3700,
"Jowar": 1520,
"Maize": 1175,
"Masoor": 2800,
"Moong": 3500,
"Niger": 3500,
"Ragi": 1500,
"Rape": 2500,
"Jute": 1675,
"Safflower": 2500,
"Soyabean": 2200,
"Sugarcane": 2250,
"Sunflower": 3700,
"Urad": 4300,
"Wheat": 1350
}
commodity_dict = {
"arhar": "crop_data/Arhar.csv",
"bajra": "crop_data/Bajra.csv",
"barley": "crop_data/Barley.csv",
"copra": "crop_data/Copra.csv",
"cotton": "crop_data/Cotton.csv",
"sesamum": "crop_data/Sesamum.csv",
"gram": "crop_data/Gram.csv",
"groundnut": "crop_data/Groundnut.csv",
"jowar": "crop_data/Jowar.csv",
"maize": "crop_data/Maize.csv",
"masoor": "crop_data/Masoor.csv",
"moong": "crop_data/Moong.csv",
"niger": "crop_data/Niger.csv",
"paddy": "crop_data/Paddy.csv",
"ragi": "crop_data/Ragi.csv",
"rape": "crop_data/Rape.csv",
"jute": "crop_data/Jute.csv",
"safflower": "crop_data/Safflower.csv",
"soyabean": "crop_data/Soyabean.csv",
"sugarcane": "crop_data/Sugarcane.csv",
"sunflower": "crop_data/Sunflower.csv",
"urad": "crop_data/Urad.csv",
"wheat": "crop_data/Wheat.csv"
}
from update_commodities import update_list
commodity_list=update_list()
def SixMonthsForecast():
month1=[]
month2=[]
month3=[]
month4=[]
month5=[]
month6=[]
for i in commodity_list:
crop=SixMonthsForecastHelper(i.getCropName())
k=0
for j in crop:
time = j[0]
price = j[1]
change = j[2]
if k==0:
month1.append((price,change,i.getCropName().split("/")[1],time))
elif k==1:
month2.append((price,change,i.getCropName().split("/")[1],time))
elif k==2:
month3.append((price,change,i.getCropName().split("/")[1],time))
elif k==3:
month4.append((price,change,i.getCropName().split("/")[1],time))
elif k==4:
month5.append((price,change,i.getCropName().split("/")[1],time))
elif k==5:
month6.append((price,change,i.getCropName().split("/")[1],time))
k+=1
month1.sort()
month2.sort()
month3.sort()
month4.sort()
month5.sort()
month6.sort()
crop_month_wise=[]
crop_month_wise.append([month1[0][3],month1[len(month1)-1][2],month1[len(month1)-1][0],month1[len(month1)-1][1],month1[0][2],month1[0][0],month1[0][1]])
crop_month_wise.append([month2[0][3],month2[len(month2)-1][2],month2[len(month2)-1][0],month2[len(month2)-1][1],month2[0][2],month2[0][0],month2[0][1]])
crop_month_wise.append([month3[0][3],month3[len(month3)-1][2],month3[len(month3)-1][0],month3[len(month3)-1][1],month3[0][2],month3[0][0],month3[0][1]])
crop_month_wise.append([month4[0][3],month4[len(month4)-1][2],month4[len(month4)-1][0],month4[len(month4)-1][1],month4[0][2],month4[0][0],month4[0][1]])
crop_month_wise.append([month5[0][3],month5[len(month5)-1][2],month5[len(month5)-1][0],month5[len(month5)-1][1],month5[0][2],month5[0][0],month5[0][1]])
crop_month_wise.append([month6[0][3],month6[len(month6)-1][2],month6[len(month6)-1][0],month6[len(month6)-1][1],month6[0][2],month6[0][0],month6[0][1]])
return crop_month_wise
#supporting function for six months forecast
def SixMonthsForecastHelper(name):
current_month = datetime.now().month
current_year = datetime.now().year
current_rainfall = annual_rainfall[current_month - 1]
name = name.split("/")[1]
name = name.lower()
commodity = commodity_list[0]
for i in commodity_list:
if name == str(i):
commodity = i
break
month_with_year = []
for i in range(1, 7):
if current_month + i <= 12:
month_with_year.append((current_month + i, current_year, annual_rainfall[current_month + i - 1]))
else:
month_with_year.append((current_month + i - 12, current_year + 1, annual_rainfall[current_month + i - 13]))
wpis = []
current_wpi = commodity.getPredictedValue([float(current_month), current_year, current_rainfall])
change = []
for m, y, r in month_with_year:
current_predict = commodity.getPredictedValue([float(m), y, r])
wpis.append(current_predict)
change.append(((current_predict - current_wpi) * 100) / current_wpi)
crop_price = []
for i in range(0, len(wpis)):
m, y, r = month_with_year[i]
x = datetime(y, m, 1)
x = x.strftime("%b %y")
crop_price.append([x, round((wpis[i]* base[name.capitalize()]) / 100, 2) , round(change[i], 2)])
#
return crop_price
#get current month forecast
def CurrentMonth(name):
current_month = datetime.now().month
current_year = datetime.now().year
current_rainfall = annual_rainfall[current_month - 1]
name = name.lower()
commodity = commodity_list[0]
for i in commodity_list:
if name == str(i):
commodity = i
break
current_wpi = commodity.getPredictedValue([float(current_month), current_year, current_rainfall])
current_price = (base[name.capitalize()]*current_wpi)/100
return current_price
#twelve months forecast
def TwelveMonthsForecast(name):
current_month = datetime.now().month
current_year = datetime.now().year
current_rainfall = annual_rainfall[current_month - 1]
name = name.lower()
commodity = commodity_list[0]
for i in commodity_list:
if name == str(i):
commodity = i
break
month_with_year = []
for i in range(1, 13):
if current_month + i <= 12:
month_with_year.append((current_month + i, current_year, annual_rainfall[current_month + i - 1]))
else:
month_with_year.append((current_month + i - 12, current_year + 1, annual_rainfall[current_month + i - 13]))
max_index = 0
min_index = 0
max_value = 0
min_value = 9999
wpis = []
current_wpi = commodity.getPredictedValue([float(current_month), current_year, current_rainfall])
change = []
for m, y, r in month_with_year:
current_predict = commodity.getPredictedValue([float(m), y, r])
if current_predict > max_value:
max_value = current_predict
max_index = month_with_year.index((m, y, r))
if current_predict < min_value:
min_value = current_predict
min_index = month_with_year.index((m, y, r))
wpis.append(current_predict)
change.append(((current_predict - current_wpi) * 100) / current_wpi)
max_month, max_year, r1 = month_with_year[max_index]
min_month, min_year, r2 = month_with_year[min_index]
min_value = min_value * base[name.capitalize()] / 100
max_value = max_value * base[name.capitalize()] / 100
crop_price = []
for i in range(0, len(wpis)):
m, y, r = month_with_year[i]
x = datetime(y, m, 1)
x = x.strftime("%b %y")
crop_price.append([x, round((wpis[i]* base[name.capitalize()]) / 100, 2) , round(change[i], 2)])
x = datetime(max_year,max_month,1)
x = x.strftime("%b %y")
max_crop = [x, round(max_value,2)]
x = datetime(min_year, min_month, 1)
x = x.strftime("%b %y")
min_crop = [x, round(min_value,2)]
return max_crop, min_crop, crop_price
#twelve months previous for creating windows
def TwelveMonthPrevious(name):
name = name.lower()
current_month = datetime.now().month
current_year = datetime.now().year
current_rainfall = annual_rainfall[current_month - 1]
commodity = commodity_list[0]
wpis = []
crop_price = []
#Define Forecast via commondity
for i in commodity_list:
if name == str(i):
commodity = i
break
month_with_year = []
#for 12 months range
for i in range(1, 13):
if current_month - i >= 1:
month_with_year.append((current_month - i, current_year, annual_rainfall[current_month - i - 1]))
else:
month_with_year.append((current_month - i + 12, current_year - 1, annual_rainfall[current_month - i + 11]))
for m, y, r in month_with_year:
current_predict = commodity.getPredictedValue([float(m), 2013, r])
wpis.append(current_predict)
for i in range(0, len(wpis)):
m, y, r = month_with_year[i]
x = datetime(y,m,1)
x = x.strftime("%b %y")
crop_price.append([x, round((wpis[i]* base[name.capitalize()]) / 100, 2)])
new_crop_price =[]
for i in range(len(crop_price)-1,-1,-1):
new_crop_price.append(crop_price[i])
return new_crop_price