Kaggle competition: Predict Future Sales
clean_all_df = clean_all_df[cols_list]<count_missing_values>
data = pd.read_pickle('/kaggle/input/all-datta5/data2.pkl')
 print(data.shape)
 data.head(3 )
clean_all_df.isnull().sum() [clean_all_df.isnull().sum() > 0]<concatenate>
a = []
for i in range(3,34):
b = data[data.date_block_num==i]
c = b.sum() ['item_cnt_month']
d = len(b.shop_id.unique())
 a.append(c/d)
 print(a )
temp_all_df = pd.concat([clean_all_df, pd.Series(np.argmax(clean_all_df.loc[:, "instlevel1" : "instlevel9"].values, axis=1), name="eduindex")], axis=1)
 edu_median_dict = temp_all_df.groupby("eduindex")["meaneduc"].median().to_dict()<correct_missing_values>
weekarr = []
t = 2
count = 0
for w in range(3):
for i in [31,28,31,30,31,30,31,31,30,31,30,31]:
a = [0,0,0,0,0,0,0,count]
count+=1
for j in range(i):
a[t]+=1
if t==6:
t=-1
t+=1
weekarr.append(a)
 weekarr = pd.DataFrame(np.vstack(weekarr), columns=['week0','week1','week2','week3','week4','week5','week6','date_block_num'])
 data = pd.merge(data, weekarr, on=['date_block_num'], how='left')
 del weekarr
gc.collect()
def fill_na(x):
if pd.isnull(x["meaneduc"]):
x.loc["meaneduc"] = edu_median_dict[x.loc["eduindex"]]
return x<feature_engineering>
X_zong = data.drop(['item_cnt_month'], axis=1)
 Y_train = data[data.date_block_num < 33]['item_cnt_month']
Y_valid = data[data.date_block_num == 33]['item_cnt_month']
del data
gc.collect()
temp_all_df = temp_all_df.apply(fill_na, axis=1 )<drop_column>
minMax = MinMaxScaler()
X_zong_std = minMax.fit_transform(X_zong.iloc[:,:-7])
 X_zong.iloc[:,:-7] = pd.DataFrame(np.vstack(X_zong_std),columns=X_zong.columns[:-7])
 X_zong.iloc[:,:-7] = downcast_dtypes(X_zong.iloc[:,:-7])
 X_zong['week0'] = X_zong['week0'].astype(np.int8)
 X_zong['week1'] = X_zong['week1'].astype(np.int8)
 X_zong['week2'] = X_zong['week2'].astype(np.int8)
 X_zong['week3'] = X_zong['week3'].astype(np.int8)
 X_zong['week4'] = X_zong['week4'].astype(np.int8)
 X_zong['week5'] = X_zong['week5'].astype(np.int8)
 X_zong['week6'] = X_zong['week6'].astype(np.int8)
 del X_zong_std
gc.collect()
clean_all_df = temp_all_df.drop("eduindex", axis=1 )<filter>
X_train = X_zong[X_zong.date_block_num < 0.96679688]
X_valid = X_zong[X_zong.date_block_num == 0.96679688]
X_test = X_zong[X_zong.date_block_num == 1]
del X_zong
gc.collect()
heads_all_df = clean_all_df[clean_all_df.parentesco1 == 1]<define_variables>
ts = time.time()
model = XGBRegressor(
 max_depth=10,
n_estimators=1000,
min_child_weight=0.5,
colsample_bytree=0.9,
subsample=0.8,
eta=0.1,
seed=1)
 model.fit(
 X_train,
Y_train,
eval_metric="rmse",
eval_set=[(X_train, Y_train),(X_valid, Y_valid)],
verbose=True,
early_stopping_rounds = 10)
 time.time() - ts
rm_cols = ["tamhog", "r4t3", "hhsize", "hogar_total", "male", "area1"]<drop_column>
Y_test = model.predict(X_test ).clip(0, 20)
 test = pd.read_csv('/kaggle/input/competitive-data-science-predict-future-sales/test.csv' ).set_index('ID')
 submission = pd.DataFrame({
"ID": test.index,
"item_cnt_month": Y_test
})
 submission.to_csv('xgb_submission1.csv', index=False )