-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.py
52 lines (39 loc) · 1.57 KB
/
helpers.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
import pandas as pd
import xgboost as xgb
import pickle
meal = pd.read_csv("preprocessing/meal_info.csv")
center = pd.read_csv("preprocessing/fulfilment_center_info.csv")
mapping = pickle.load(open("preprocessing/mapping","rb"))
def apply_cats(df, mapping):
for column, map in mapping.items():
for cats, vals in map.items():
df.loc[df[column]==cats,column]=vals
df[column]= df[column].astype('int16')
def preprocessing(df):
df['discount'] = (df['base_price'].astype('int32') - df['checkout_price'].astype('int32'))*100/df['base_price'].astype('int32')
df['meal_id']=df['meal_id'].astype('int64')
df['center_id']=df['center_id'].astype('int64')
testdf = df.merge(meal,on='meal_id')
testdf = testdf.merge(center,on='center_id')
apply_cats(testdf,mapping)
testdf.astype('int32')
return testdf
def get_predictions(df,bst):
dfpd = preprocessing(df)
df = xgb.DMatrix(dfpd.drop(columns=['id','week','base_price']).values)
predictions = bst.predict(df)
predictions = pd.DataFrame(predictions.round())
print(predictions)
return predictions
def predict_csv(file,bst):
dfpd = pd.read_csv(file)
dfpd=dfpd.astype('int64')
predictions=get_predictions(dfpd,bst)
predictions = predictions.rename(columns={0 : 'num_orders'})
#predictions = predictions.rename({'unnamed : 0' : 'pred'})
predictions = pd.concat([dfpd, predictions],axis=1)
return predictions
def predict_individual(data,bst):
df=pd.DataFrame(data,index=[0])
preds=get_predictions(df,bst)
return preds