Kaggle competition: Predict Future Sales
learn.lr_find()<train_model>
sales_train = pd.read_csv(".. /input/competitive-data-science-predict-future-sales/sales_train.csv")
 test = pd.read_csv(".. /input/competitive-data-science-predict-future-sales/test.csv")
 test.drop('ID',axis=1,inplace=True )
learn.fit_one_cycle(8 )<save_model>
test_shop_item_pair = test[(test.shop_id==10)]
test_shop_item_pair.loc[test_shop_item_pair.shop_id == 10, 'shop_id']= 11
test.loc[test.shop_id == 10, 'shop_id']= 11
learn.save('stage-1-50-224' )<train_model>
def reduce_size(df):
float_cols = [c for c in df if df[c].dtype == "float64"]
int_cols = [c for c in df if df[c].dtype in ["int64", "int32"]]
df[float_cols] = df[float_cols].astype(np.float32)
 df[int_cols] = df[int_cols].astype(np.int16)
 return df
learn.fit_one_cycle(4, max_lr=slice(1e-6,1e-4))<load_pretrained>
all_data1 = pickle.load(open(".. /input/predict-future-sales-part-1/all_data1","rb"))
learn.load('stage-1-50-224');<predict_on_test>
feat_to_keep = ['date_block_num', 'shop_id', 'item_id', 'item_cnt_month',
'item_category_id', 'subtype', 'db_avg_items_sold_lag_1',
'db_shop_cat_avg_items_sold_lag_1', 'db_item_id_items_sold_lag_1',
'db_item_id_items_sold_lag_2', 'db_item_id_items_sold_lag_3',
'db_item_id_items_sold_lag_6','city_target_enc',
'item_id_target_enc', 'month_target_enc','db_shop_city_avg_items_sold_lag_1',
'db_shop_city_avg_items_sold_lag_2', 'db_city_avg_items_sold_lag_1',
'month', 'item_months_since_first_sale','item_shop_last_sale',
'db_item_avg_price_lag_1','delta_price_lag', 'delta_price_lag_1',
'delta_price_lag_3','max_cnt_lag_1', 'max_cnt_lag_3','max_cnt_lag_6', 'revenue_shop_lag_2']
preds, _ = learn.get_preds(DatasetType.Test )<split>
all_data1 = all_data1.loc[:,feat_to_keep]
def get_numeric_part(file_path, path_test):
low = len(str(path_test)) + 1
return str(file_path)[low:-4]<string_transform>
X_train = all_data1[all_data1.date_block_num < 33].drop(['item_cnt_month'], axis=1)
 Y_train = all_data1[all_data1.date_block_num < 33]['item_cnt_month']
X_valid = all_data1[all_data1.date_block_num == 33].drop(['item_cnt_month'], axis=1)
 Y_valid = all_data1[all_data1.date_block_num == 33]['item_cnt_month']
X_test = all_data1[all_data1.date_block_num == 34].drop(['item_cnt_month'], axis=1)
 del all_data1
gc.collect() ;
get_numeric_part(data.test_ds.items[0], path_test )<prepare_output>
def rmse(y_pred, y_test):
rmse = sqrt(mean_squared_error(y_test,y_pred))
return print(rmse )
df = pd.DataFrame(preds.numpy() , columns=data.classes)
 filenames = [get_numeric_part(fp, path_test)for fp in data.test_ds.items]
df.insert(0, "id", filenames)
 df_sorted = df.sort_values(by='id' )<save_to_csv>
def predict(model,name):
Y_test = model.predict(X_test ).clip(0., 20.)
results = X_test.loc[:,['shop_id', 'item_id']]
results['prediction'] = Y_test
if len(test.columns)==3:
test.drop('ID',axis=1,inplace=True)
 sub = pd.merge(test, results, on = ['shop_id', 'item_id'], how='left')
 submission = pd.DataFrame({
"ID": test.index,
"item_cnt_month": sub['prediction']
})
 file_name = str(name)+ '_submission.csv'
submission.to_csv(file_name, index=False )
<import_modules><EOS>