Kaggle competition: Rossmann Store Sales
print("Training file : ")
 train_file.head(3 ).iloc[:,:17]<train_model>
def load_raw_df() :
train_df = pd.read_csv(".. /input/train.csv", parse_dates=[2])
 store_df = pd.read_csv(".. /input/store.csv")
 test_df = pd.read_csv(".. /input/test.csv", parse_dates=[3])
 train_df.head()
store_df.head()
test_df.head()
display(train_df.isnull().sum() ,store_df.isnull().sum() ,test_df.isnull().sum())
 mereged_train_df = pd.merge(train_df, store_df, on='Store')
 merged_test_df = pd.merge(test_df, store_df, on='Store')
 return mereged_train_df, merged_test_df
def extract_linear_feature(df):
df.drop("Customers", 1)
 mappings = {'0': 0, 'a': 1, 'b': 2, 'c': 3, 'd': 4}
df["StoreType"] = df["StoreType"].map(mappings)
 df["Assortment"] = df["Assortment"].map(mappings)
 df["StateHoliday"].loc[df["StateHoliday"] == 0] = "0"
df["StateHoliday"] = df["StateHoliday"].map(mappings)
 df["CompetitionDistance"].fillna(df["CompetitionDistance"].median())
 df["year"] = df.Date.dt.year
df["month"] = df.Date.dt.month
df["day"] = df.Date.dt.day
df["year"] = df.Date.dt.year
df["month"] = df.Date.dt.month
df["day"] = df.Date.dt.day
df.drop(["Date", "PromoInterval", "Promo2SinceWeek", "Promo2SinceYear"], axis=1, inplace=True)
 return df
def get_train_test_label() :
train_df, test_df = load_raw_df()
train_df_2 = extract_linear_feature(train_df)
 train_df_2.head()
y_true = train_df_2.loc[:, 'Sales'].as_matrix(columns=None)
 train_df_2.drop("Sales", axis =1)
 test_df["Customers"] = 1
test_df = extract_linear_feature(test_df)
 test_df.head()
feature_list = list(train_df_2.columns)
 print(feature_list)
 return train_df_2,y_true,test_df
x_train, y_train, x_test = get_train_test_label()
<feature_engineering><EOS> 
train_df = pd.read_csv(".. /input/train.csv", parse_dates=[2])
 store_df = pd.read_csv(".. /input/store.csv")
 test_df = pd.read_csv(".. /input/test.csv", parse_dates=[3])
 features = ['Store', 'DayOfWeek', 'Promo', 'StateHoliday', 'SchoolHoliday', 'StoreType', 'Assortment','Promo2', 'WeekOfYear','year', 'month', 'day']
def extract_feature(df):
mappings = {'0': 0, 'a': 1, 'b': 2, 'c': 3, 'd': 4}
df["StoreType"] = df["StoreType"].map(mappings)
 df["Assortment"] = df["Assortment"].map(mappings)
 df["StateHoliday"].loc[df["StateHoliday"] == 0] = "0"
df["StateHoliday"] = df["StateHoliday"].map(mappings)
 df["CompetitionDistance"].fillna(df["CompetitionDistance"].median())
 df['WeekOfYear'] = df.Date.dt.weekofyear
df["year"] = df.Date.dt.year
df["month"] = df.Date.dt.month
df["day"] = df.Date.dt.day
return df
train_df = pd.merge(train_df, store_df, on='Store')
 test_df = pd.merge(test_df, store_df, on='Store')
 train_df = extract_feature(train_df)
 test_df = extract_feature(test_df)
 train_df = train_df[train_df["Open"] != 0]
train, valid = train_test_split(train_df, test_size=0.03)
 y_train = np.log1p(train.Sales)
 y_valid = np.log1p(valid.Sales)
 dtrain = xgb.DMatrix(train[features], y_train)
 dvalid = xgb.DMatrix(valid[features], y_valid)
 watchlist = [(dtrain, 'train'),(dvalid, 'eval')]
def rmspe(y, yhat):
return np.sqrt(np.mean(((y - yhat)/y)** 2))
def rmspe_xg(yhat, y):
y = np.expm1(y.get_label())
 yhat = np.expm1(yhat)
 return "rmspe", rmspe(y, yhat)
 params = {"objective": "reg:linear",
"booster" : "gbtree",
"eta": 0.1,
"max_depth": 10,
"subsample": 0.85,
"colsample_bytree": 0.4,
"min_child_weight": 6,
"thread": 1,
"seed": 10
}
num_boost_round = 1000
gbm = xgb.train(params, dtrain, num_boost_round, evals=watchlist, early_stopping_rounds=1000, \
feval=rmspe_xg, verbose_eval=True)
 print("Validating")
 yhat = gbm.predict(xgb.DMatrix(valid[features]))
error = rmspe(valid.Sales.values, np.expm1(yhat))
print('RMSPE: {:.6f}'.format(error))
print("Make predictions on the test set")
 dtest = xgb.DMatrix(test_df[features])
 test_probs = gbm.predict(dtest)
 print(test_probs[:5])
 result = pd.DataFrame({"Id": test_df["Id"], 'Sales': np.expm1(test_probs)})
 result.to_csv("xgb_v2.csv", index=False)