Kaggle competition: Walmart Recruiting - Store Sales Forecasting
test_loader = DataLoader(
 test_set,
batch_size=1,
shuffle=False )<choose_model_class>
from sklearn.ensemble import RandomForestRegressor
criterion = nn.BCELoss()<compute_test_metric>
from sklearn.ensemble import RandomForestRegressor
def compute_spearmanr_ignore_nan(trues, preds):
rhos = []
for tcol, pcol in zip(np.transpose(trues), np.transpose(preds)) :
rhos.append(spearmanr(tcol, pcol ).correlation)
 return np.nanmean(rhos )<choose_model_class>
train = pd.read_csv("/kaggle/input/walmart-recruiting-store-sales-forecasting/train.csv.zip")
 test = pd.read_csv("/kaggle/input/walmart-recruiting-store-sales-forecasting/test.csv.zip")
 stores = pd.read_csv("/kaggle/input/walmart-recruiting-store-sales-forecasting/stores.csv" )
<load_pretrained>
train=train.merge(stores,how='left',on='Store')
 train.replace({'A': 1, 'B': 2,'C':3},inplace=True)
 train['Date']=pd.to_datetime(train['Date'])
 train['dia']=pd.to_datetime(train['Date'] ).dt.day
train['mes']=pd.to_datetime(train['Date'] ).dt.month
train['ano']=pd.to_datetime(train['Date'] ).dt.year
train.head()
models = []
for fold in range(5):
model_path = f'.. /input/google-qa-labeling-pretrained-v3/model_{fold}_2.t7'
if os.path.exists(model_path):
print(f'model available for prediction at {model_path}')
 model = Model_v1()
model.load_state_dict(torch.load(model_path))
models.append(model)
 for fold in range(5):
model_path = f'.. /input/google-qa-labeling-pretrained-v4/model_{fold}_2.t7'
if os.path.exists(model_path):
print(f'model available for prediction at {model_path}')
 model = Model_v2()
model.load_state_dict(torch.load(model_path))
models.append(model )<find_best_params>
test=test.merge(stores,how='left',on='Store')
 test.replace({'A': 1, 'B': 2,'C':3},inplace=True)
 test['Date']=pd.to_datetime(test['Date'])
 test['dia']=pd.to_datetime(test['Date'] ).dt.day
test['mes']=pd.to_datetime(test['Date'] ).dt.month
test['ano']=pd.to_datetime(test['Date'] ).dt.year
test.head()
final_predicts = []
for model in models:
model = model.cuda()
model.eval()
test_predicts = []
with torch.no_grad() :
for input_question, input_answer in test_loader:
print(input_answer.shape)
 input_question, input_answer = input_question.cuda() , input_answer.cuda()
input_question, input_answer = Variable(input_question, requires_grad=False), Variable(input_answer, requires_grad=False)
 scores = model(input_question[:,0],
input_question[:,2],
input_question[:,1],
input_answer[:,0],
input_answer[:,2],
input_answer[:,1])
 test_predicts.append(scores.reshape(scores.shape[-1]))
final_predicts.append(test_predicts )<groupby>
X = train.loc[:, train.columns != 'Weekly_Sales'].drop('Date',axis=1)
 y = train.loc[:, train.columns == 'Weekly_Sales']
rf = RandomForestRegressor()
rf.fit(X, y)
 test['Weekly_Sales'] = rf.predict(test.drop('Date',axis=1))
pres = np.average(final_predicts, axis=0)
 test_output = [[p.item() for p in pre] for pre in pres]<define_variables>
test['ID']=test['Store'].astype(str)+'_'+test['Dept'].astype(str)+'_'+test['Date'].astype(str)
 test[['ID','Weekly_Sales']].to_csv('sub.csv',index=False )