Kaggle competition: [Open] Shopee Code League - Logistics
filename = ".. /input/submission/submission.csv"
df = pd.read_csv(filename)
<load_from_csv>
df_order = pd.read_csv('.. /input/open-shopee-code-league-logistic/delivery_orders_march.csv' )
series = read_csv('.. /input/submission/submission.csv', header=0, index_col=0, parse_dates=True, squeeze=True)
 series.plot()
pyplot.show() 
<load_from_csv>
df_SLA = pd.read_excel('.. /input/open-shopee-code-league-logistic/SLA_matrix.xlsx' )
series = read_csv('.. /input/submission/submission.csv', header=0, index_col=0, parse_dates=True, squeeze=True)
 lag_plot(series )<save_to_csv>
SLA_Matrix = [[3, 5, 7, 7], [5, 5, 7, 7], [7, 7, 7, 7], [7, 7, 7, 7]]
Submission.to_csv('submission.csv',index=False )<compute_test_metric>
time_needed = []
for(o, d)in zip(origin, destination):
time_needed.append(SLA_Matrix[o-1][d-1])
 print(time_needed.count(3))
print(time_needed.count(5))
print(time_needed.count(7))
def RMSLE(pred,actual):
return np.sqrt(np.mean(np.power(( np.log(pred+1)-np.log(actual+1)) ,2)) )<load_from_csv>
def converttodate(epoch_time):
return datetime.datetime.fromtimestamp(epoch_time ).strftime("%Y-%m-%d" )
pd.set_option('mode.chained_assignment', None)
 test = pd.read_csv(".. /input/covid19-global-forecasting-week-2/test.csv")
 train = pd.read_csv(".. /input/covid19-global-forecasting-week-2/train.csv")
 train['Province_State'].fillna('', inplace=True)
 test['Province_State'].fillna('', inplace=True)
 train['Date'] = pd.to_datetime(train['Date'])
 test['Date'] = pd.to_datetime(test['Date'])
 train = train.sort_values(['Country_Region','Province_State','Date'])
 test = test.sort_values(['Country_Region','Province_State','Date'] )<feature_engineering>
pick_date = df_order.pick.tolist()
first_attempt = df_order['1st_deliver_attempt'].tolist()
second_attempt = df_order['2nd_deliver_attempt'].tolist()
pick = []
first = []
second = []
for(i, j, k)in zip(pick_date, first_attempt, second_attempt):
pick.append(converttodate(i))
first.append(converttodate(j))
if np.isnan(k):
second.append(np.nan)
 else :
second.append(converttodate(k))
feature_day = [1,20,50,100,200,500,1000]
def CreateInput(data):
feature = []
for day in feature_day:
data.loc[:,'Number day from ' + str(day)+ ' case'] = 0
if(train[(train['Country_Region'] == country)&(train['Province_State'] == province)&(train['ConfirmedCases'] < day)]['Date'].count() > 0):
fromday = train[(train['Country_Region'] == country)&(train['Province_State'] == province)&(train['ConfirmedCases'] < day)]['Date'].max()
else:
fromday = train[(train['Country_Region'] == country)&(train['Province_State'] == province)]['Date'].min()
for i in range(0, len(data)) :
if(data['Date'].iloc[i] > fromday):
day_denta = data['Date'].iloc[i] - fromday
data['Number day from ' + str(day)+ ' case'].iloc[i] = day_denta.days
feature = feature + ['Number day from ' + str(day)+ ' case']
return data[feature]
pred_data_all = pd.DataFrame()
for country in train['Country_Region'].unique() :
for province in train[(train['Country_Region'] == country)]['Province_State'].unique() :
df_train = train[(train['Country_Region'] == country)&(train['Province_State'] == province)]
df_test = test[(test['Country_Region'] == country)&(test['Province_State'] == province)]
X_train = CreateInput(df_train)
 y_train_confirmed = df_train['ConfirmedCases'].ravel()
y_train_fatalities = df_train['Fatalities'].ravel()
X_pred = CreateInput(df_test)
 for day in sorted(feature_day,reverse = True):
feature_use = 'Number day from ' + str(day)+ ' case'
idx = X_train[X_train[feature_use] == 0].shape[0]
if(X_train[X_train[feature_use] > 0].shape[0] >= 20):
break
adjusted_X_train = X_train[idx:][feature_use].values.reshape(-1, 1)
 adjusted_y_train_confirmed = y_train_confirmed[idx:]
adjusted_y_train_fatalities = y_train_fatalities[idx:]
idx = X_pred[X_pred[feature_use] == 0].shape[0]
adjusted_X_pred = X_pred[idx:][feature_use].values.reshape(-1, 1)
 model = make_pipeline(PolynomialFeatures(2), BayesianRidge())
 model.fit(adjusted_X_train,adjusted_y_train_confirmed)
 y_hat_confirmed = model.predict(adjusted_X_pred)
 model.fit(adjusted_X_train,adjusted_y_train_fatalities)
 y_hat_fatalities = model.predict(adjusted_X_pred)
 pred_data = test[(test['Country_Region'] == country)&(test['Province_State'] == province)]
pred_data['ConfirmedCases_hat'] = np.concatenate(( np.repeat(0, len(pred_data)- len(y_hat_confirmed)) , y_hat_confirmed), axis = 0)
 pred_data['Fatalities_hat'] = np.concatenate(( np.repeat(float(0), len(pred_data)- len(y_hat_fatalities)) , y_hat_fatalities), axis = 0)
 pred_data_all = pred_data_all.append(pred_data)
 df_val = pd.merge(pred_data_all,train[['Date','Country_Region','Province_State','ConfirmedCases','Fatalities']],on=['Date','Country_Region','Province_State'], how='left')
 df_val.loc[df_val['Fatalities_hat'] < 0,'Fatalities_hat'] = 0
df_val.loc[df_val['ConfirmedCases_hat'] < 0,'ConfirmedCases_hat'] = 0
df_val_1 = df_val.copy()<compute_test_metric>
df_solution = pd.DataFrame() ;
df_solution['orderid'] = df_order.orderid
df_solution['origin'] = origin
df_solution['destination'] = destination
df_solution['time_needed'] = time_needed
df_solution['pick'] = pick
df_solution['first'] = first
df_solution['second'] = second
RMSLE(df_val[(df_val['ConfirmedCases'].isnull() == False)]['ConfirmedCases'].values,df_val[(df_val['ConfirmedCases'].isnull() == False)]['ConfirmedCases_hat'].values )<compute_test_metric>
is_late = []
weekmask = [1, 1, 1, 1, 1, 1, 0]
for(i, j, k, l)in zip(df_solution['pick'].tolist() , df_solution['first'].tolist() , df_solution['second'].tolist() ,df_solution['time_needed']):
if(abs(np.busday_count(j,i)) - 1 > l):
is_late.append(1)
 else :
is_late.append(0 )
RMSLE(df_val[(df_val['Fatalities'].isnull() == False)]['Fatalities'].values,df_val[(df_val['Fatalities'].isnull() == False)]['Fatalities_hat'].values )<groupby>
df_submit = pd.DataFrame() ;
df_submit['orderid'] = df_solution['orderid']
df_submit['is_late'] = is_late
<feature_engineering><EOS> 
df_submit.to_csv('solution_noholiday.csv',index=False )