Kaggle competition: Predict Future Sales
train.isnull().sum()<prepare_x_and_y>
train_path = ".. /input/competitive-data-science-predict-future-sales/sales_train.csv"
test_path = ".. /input/competitive-data-science-predict-future-sales/test.csv"
items_path = ".. /input/competitive-data-science-predict-future-sales/items.csv"
shops_path = ".. /input/competitive-data-science-predict-future-sales/shops.csv"
item_cat_path = ".. /input/competitive-data-science-predict-future-sales/item_categories.csv"
y_train = train["trip_duration"]
X_train = train[["vendor_id","passenger_count","pickup_longitude", "pickup_latitude", "dropoff_longitude","dropoff_latitude","dist","hour"]]
X_datatest = test[["vendor_id","passenger_count","pickup_longitude", "pickup_latitude", "dropoff_longitude","dropoff_latitude","dist","hour"]]<drop_column>
items = pd.read_csv(items_path)
 cats = pd.read_csv(item_cat_path)
 shops = pd.read_csv(shops_path)
 train = pd.read_csv(train_path)
 test = pd.read_csv(test_path)
train.drop(['speed','dist','hour']+col_diff, axis=1, inplace=True )<split>
train = train[train.item_price > 0].reset_index(drop = True)
 train.loc[train.item_cnt_day < 1, "item_cnt_day"] = 0
X_train, X_valid, y_train, y_valid = train_test_split(X_train, y_train, test_size = 0.1, random_state=42 )<init_hyperparams>
train.loc[train.shop_id == 0, "shop_id"] = 57
test.loc[test.shop_id == 0 , "shop_id"] = 57
train.loc[train.shop_id == 1, "shop_id"] = 58
test.loc[test.shop_id == 1 , "shop_id"] = 58
train.loc[train.shop_id == 11, "shop_id"] = 10
test.loc[test.shop_id == 11, "shop_id"] = 10
train.loc[train.shop_id == 40, "shop_id"] = 39
test.loc[test.shop_id == 40, "shop_id"] = 39
params = {
'booster': 'gbtree',
'objective': 'reg:linear',
'learning_rate': 0.1,
'max_depth': 14,
'subsample': 0.8,
'colsample_bytree': 0.7,
'colsample_bylevel': 0.7,
'silent': 1
}<train_model>
shops.loc[ shops.shop_name == 'Сергиев Посад ТЦ "7Я"',"shop_name" ] = 'СергиевПосад ТЦ "7Я"'
shops["city"] = shops.shop_name.str.split(" " ).map(lambda x: x[0])
 shops["category"] = shops.shop_name.str.split(" " ).map(lambda x: x[1])
 shops.loc[shops.city == "!Якутск", "city"] = "Якутск"
nrounds = 1200
dtrain = xgb.DMatrix(X_train, np.log(y_train+1))
gbm = xgb.train(params,
dtrain,
num_boost_round = nrounds )<predict_on_test>
shops.category = shops.category.apply(lambda x: x if(x in category)else "etc" )
train_pred = np.exp(gbm.predict(xgb.DMatrix(X_datatest)))- 1<prepare_output>
for cat in shops.category.unique() :
print(cat, len(shops[shops.category == cat]))
my_submission = pd.DataFrame({"id": test.id, "trip_duration": np.exp(train_pred)})
 my_submission.head() 
<save_to_csv>
shops["shop_category"] = LabelEncoder().fit_transform(shops.category)
 shops["shop_city"] = LabelEncoder().fit_transform(shops.city )
my_submission.to_csv('submission.csv', index=False)
 my_submission.head()<set_options>
shops = shops[["shop_id", "shop_category", "shop_city"]]
%matplotlib inline
snb.set({'figure.figsize':(16,8), 'axes.titlesize':30, 'axes.labelsize':20})
<define_variables>
cats["type_code"] = cats.item_category_name.apply(lambda x: x.split(" ")[0] ).astype(str)
 cats.loc[(cats.type_code == "Игровые")|(cats.type_code == "Аксессуары"), "category" ] = "Игры"
TRAINFILEPATH = os.path.join('.. ', 'input', 'train.csv' )<load_from_csv>
cats.type_code = cats.type_code.apply(lambda x: x if(x in category)else "etc" )
data = pd.read_csv(TRAINFILEPATH, index_col=0 )<count_missing_values>
for cat in cats.type_code.unique() :
print(cat, len(cats[cats.type_code == cat]))
data.isna().sum()<data_type_conversions>
cats.type_code = LabelEncoder().fit_transform(cats.type_code)
 cats["split"] = cats.item_category_name.apply(lambda x: x.split("-"))
cats["subtype"] = cats.split.apply(lambda x: x[1].strip() if len(x)> 1 else x[0].strip())
 cats["subtype_code"] = LabelEncoder().fit_transform(cats["subtype"])
 cats = cats[["item_category_id", "subtype_code", "type_code"]]
data['pickup_datetime'] = pd.to_datetime(data['pickup_datetime'])
 data['dropoff_datetime'] = pd.to_datetime(data['dropoff_datetime'] )<filter>
def name_correction(x):
x = x.lower()
x = x.partition('[')[0]
x = x.partition('(')[0]
x = re.sub('[^A-Za-z0-9А-Яа-я]+', ' ', x)
 x = x.replace(' ', ' ')
 x = x.strip()
return x
data = data[data["trip_duration"]<21600]<drop_column>
items = pd.read_csv(items_path)
 items["name1"], items["name2"] = items.item_name.str.split("[", 1 ).str
items["name1"], items["name3"] = items.item_name.str.split("(", 1 ).str
items["name2"] = items.name2.str.replace('[^A-Za-z0-9А-Яа-я]+', " " ).str.lower()
items["name3"] = items.name3.str.replace('[^A-Za-z0-9А-Яа-я]+', " " ).str.lower()
items = items.fillna('0')
 items["item_name"] = items["item_name"].apply(lambda x: name_correction(x))
items.name2 = items.name2.apply(lambda x: x[:-1] if x !="0" else "0" )
print("{} trips have been removed.It means {:0.2f}% of the total trips in the dataset.".format(1458644-1456583,(1458644-1456583)*100/1458644))<define_variables>
items["type"] = items.name2.apply(lambda x: x[0:8] if x.split(" ")[0] == "xbox" else x.split(" ")[0])
 items.loc[(items.type == "x360")|(items.type == "xbox360")|(items.type == "xbox 360"),"type"] = "xbox 360"
items.loc[ items.type == "", "type"] = "mac"
items.type = items.type.apply(lambda x: x.replace(" ", ""))
items.loc[(items.type == 'pc')|(items.type == 'pс')|(items.type == "pc"), "type" ] = "pc"
items.loc[ items.type == 'рs3' , "type"] = "ps3"
train = data<feature_engineering>
group_sum = items.groupby(["type"] ).agg({"item_id": "count"})
 print(group_sum.reset_index())
 group_sum = group_sum.reset_index()
train['month_pickup']=data['pickup_datetime'].dt.month
train['day_pickup']=data['pickup_datetime'].dt.dayofweek
train['hour_pickup']=data['pickup_datetime'].dt.hour
train['minute_pickup']=data['pickup_datetime'].dt.minute
train['second_pickup']=data['pickup_datetime'].dt.second
train = train.drop(columns=['pickup_datetime'] )<normalization>
items.name2 = items.name2.apply(lambda x: "etc" if(x in drop_cols)else x)
 items = items.drop(["type"], axis = 1 )
def cosrad(n):
"Return the cosine of ``n`` degrees in radians."
return math.cos(math.radians(n))
def distance(row):

 lat1 = row['pickup_latitude']
long1 = row['pickup_longitude']
lat2 = row['dropoff_latitude']
long2 = row['dropoff_longitude']
earth_radius = 6371
dLat = math.radians(lat2 - lat1)
 dLong = math.radians(long2 - long1)
 a =(math.sin(dLat / 2)** 2 +
cosrad(lat1)* cosrad(lat2)* math.sin(dLong / 2)** 2)
 c = 2 * math.atan2(math.sqrt(a), math.sqrt(1 - a))
d = earth_radius * c
return d<feature_engineering>
items.name2 = LabelEncoder().fit_transform(items.name2)
 items.name3 = LabelEncoder().fit_transform(items.name3)
 items.drop(["item_name", "name1"],axis = 1, inplace= True)
 items.head()
train['trip_distance']=data.apply(distance, axis=1 )<feature_engineering>
ts = time.time()
matrix = []
cols = ["date_block_num", "shop_id", "item_id"]
for i in range(34):
sales = train[train.date_block_num == i]
matrix.append(np.array(list(product([i], sales.shop_id.unique() , sales.item_id.unique())) , dtype = np.int16))
matrix = pd.DataFrame(np.vstack(matrix), columns = cols)
 matrix["date_block_num"] = matrix["date_block_num"].astype(np.int8)
 matrix["shop_id"] = matrix["shop_id"].astype(np.int8)
 matrix["item_id"] = matrix["item_id"].astype(np.int16)
 matrix.sort_values(cols, inplace = True)
 time.time() - ts
train['trip_duration_log']=data['trip_duration'].apply(np.log )<define_variables>
train["revenue"] = train["item_cnt_day"] * train["item_price"]
features = ['trip_distance', 'day_pickup', 'hour_pickup', 'minute_pickup', "pickup_longitude", "dropoff_longitude", "pickup_latitude", "dropoff_latitude"]
target = 'trip_duration_log'<prepare_x_and_y>
ts = time.time()
group = train.groupby(["date_block_num", "shop_id", "item_id"] ).agg({"item_cnt_day": ["sum"]})
 group.columns = ["item_cnt_month"]
group.reset_index(inplace = True)
 matrix = pd.merge(matrix, group, on = cols, how = "left")
 matrix["item_cnt_month"] = matrix["item_cnt_month"].fillna(0 ).clip(0,20 ).astype(np.float16)
 time.time() - ts
X = train[features]
y = train[target]
X.shape, y.shape<import_modules>
test["date_block_num"] = 34
test["date_block_num"] = test["date_block_num"].astype(np.int8)
 test["shop_id"] = test.shop_id.astype(np.int8)
 test["item_id"] = test.item_id.astype(np.int16)
from sklearn.metrics import make_scorer
from sklearn.model_selection import cross_val_score<compute_test_metric>
ts = time.time()
matrix = pd.concat([matrix, test.drop(["ID"],axis = 1)], ignore_index=True, sort=False, keys=cols)
 matrix.fillna(0, inplace = True)
 time.time() - ts
def rmsle_func(ypred, ytest):
assert len(ytest)== len(ypred)
 return np.sqrt(np.mean(( np.log1p(ypred)- np.log1p(ytest)) **2))<compute_test_metric>
ts = time.time()
matrix = pd.merge(matrix, shops, on = ["shop_id"], how = "left")
 matrix = pd.merge(matrix, items, on = ["item_id"], how = "left")
 matrix = pd.merge(matrix, cats, on = ["item_category_id"], how = "left")
 matrix["shop_city"] = matrix["shop_city"].astype(np.int8)
 matrix["shop_category"] = matrix["shop_category"].astype(np.int8)
 matrix["item_category_id"] = matrix["item_category_id"].astype(np.int8)
 matrix["subtype_code"] = matrix["subtype_code"].astype(np.int8)
 matrix["name2"] = matrix["name2"].astype(np.int8)
 matrix["name3"] = matrix["name3"].astype(np.int16)
 matrix["type_code"] = matrix["type_code"].astype(np.int8)
 time.time() - ts
rmsle = make_scorer(rmsle_func )<import_modules>
def lag_feature(df,lags, cols):
for col in cols:
print(col)
 tmp = df[["date_block_num", "shop_id","item_id",col ]]
for i in lags:
shifted = tmp.copy()
shifted.columns = ["date_block_num", "shop_id", "item_id", col + "_lag_"+str(i)]
shifted.date_block_num = shifted.date_block_num + i
df = pd.merge(df, shifted, on=['date_block_num','shop_id','item_id'], how='left')
 return df
from sklearn.ensemble import RandomForestRegressor
from sklearn.linear_model import SGDRegressor
from sklearn.linear_model import LinearRegression<define_search_model>
ts = time.time()
group = matrix.groupby(["date_block_num"] ).agg({"item_cnt_month" : ["mean"]})
 group.columns = ["date_avg_item_cnt"]
group.reset_index(inplace = True)
 matrix = pd.merge(matrix, group, on = ["date_block_num"], how = "left")
 matrix.date_avg_item_cnt = matrix["date_avg_item_cnt"].astype(np.float16)
 matrix = lag_feature(matrix, [1], ["date_avg_item_cnt"])
 matrix.drop(["date_avg_item_cnt"], axis = 1, inplace = True)
 time.time() - ts
rfr = RandomForestRegressor(n_estimators=15)
 reg = LinearRegression()
sgdr = SGDRegressor()<compute_train_metric>
ts = time.time()
group = matrix.groupby(['date_block_num', 'item_id'] ).agg({'item_cnt_month': ['mean']})
 group.columns = [ 'date_item_avg_item_cnt' ]
group.reset_index(inplace=True)
 matrix = pd.merge(matrix, group, on=['date_block_num','item_id'], how='left')
 matrix.date_item_avg_item_cnt = matrix['date_item_avg_item_cnt'].astype(np.float16)
 matrix = lag_feature(matrix, [1,2,3], ['date_item_avg_item_cnt'])
 matrix.drop(['date_item_avg_item_cnt'], axis=1, inplace=True)
 time.time() - ts
scores_rfr = cross_val_score(rfr, X, y, cv=5, scoring=rmsle )<compute_test_metric>
ts = time.time()
group = matrix.groupby(["date_block_num","shop_id"] ).agg({"item_cnt_month" : ["mean"]})
 group.columns = ["date_shop_avg_item_cnt"]
group.reset_index(inplace = True)
 matrix = pd.merge(matrix, group, on = ["date_block_num","shop_id"], how = "left")
 matrix.date_avg_item_cnt = matrix["date_shop_avg_item_cnt"].astype(np.float16)
 matrix = lag_feature(matrix, [1,2,3], ["date_shop_avg_item_cnt"])
 matrix.drop(["date_shop_avg_item_cnt"], axis = 1, inplace = True)
 time.time() - ts
print("RMSLE: %0.2f(+/- %0.4f)" %(scores_rfr.mean() , scores_rfr.std() * 2))<compute_train_metric>
ts = time.time()
group = matrix.groupby(["date_block_num","shop_id","item_id"] ).agg({"item_cnt_month" : ["mean"]})
 group.columns = ["date_shop_item_avg_item_cnt"]
group.reset_index(inplace = True)
 matrix = pd.merge(matrix, group, on = ["date_block_num","shop_id","item_id"], how = "left")
 matrix.date_avg_item_cnt = matrix["date_shop_item_avg_item_cnt"].astype(np.float16)
 matrix = lag_feature(matrix, [1,2,3], ["date_shop_item_avg_item_cnt"])
 matrix.drop(["date_shop_item_avg_item_cnt"], axis = 1, inplace = True)
 time.time() - ts
scores_reg = cross_val_score(reg, X, y, cv=5, scoring=rmsle)
 print("RMSLE: %0.2f(+/- %0.4f)" %(scores_reg.mean() , scores_reg.std() * 2))<compute_train_metric>
ts = time.time()
group = matrix.groupby(['date_block_num', 'shop_id', 'subtype_code'] ).agg({'item_cnt_month': ['mean']})
 group.columns = ['date_shop_subtype_avg_item_cnt']
group.reset_index(inplace=True)
 matrix = pd.merge(matrix, group, on=['date_block_num', 'shop_id', 'subtype_code'], how='left')
 matrix.date_shop_subtype_avg_item_cnt = matrix['date_shop_subtype_avg_item_cnt'].astype(np.float16)
 matrix = lag_feature(matrix, [1], ['date_shop_subtype_avg_item_cnt'])
 matrix.drop(['date_shop_subtype_avg_item_cnt'], axis=1, inplace=True)
 time.time() - ts
scores_sgdr= cross_val_score(sgdr, X, y, cv=5, scoring=rmsle )<compute_test_metric>
ts = time.time()
group = matrix.groupby(['date_block_num', 'shop_city'] ).agg({'item_cnt_month': ['mean']})
 group.columns = ['date_city_avg_item_cnt']
group.reset_index(inplace=True)
 matrix = pd.merge(matrix, group, on=['date_block_num', "shop_city"], how='left')
 matrix.date_city_avg_item_cnt = matrix['date_city_avg_item_cnt'].astype(np.float16)
 matrix = lag_feature(matrix, [1], ['date_city_avg_item_cnt'])
 matrix.drop(['date_city_avg_item_cnt'], axis=1, inplace=True)
 time.time() - ts
print("RMSLE: %0.2f(+/- %0.4f)" %(scores_sgdr.mean() , scores_sgdr.std() * 2))<train_model>
ts = time.time()
group = matrix.groupby(['date_block_num', 'item_id', 'shop_city'] ).agg({'item_cnt_month': ['mean']})
 group.columns = [ 'date_item_city_avg_item_cnt' ]
group.reset_index(inplace=True)
 matrix = pd.merge(matrix, group, on=['date_block_num', 'item_id', 'shop_city'], how='left')
 matrix.date_item_city_avg_item_cnt = matrix['date_item_city_avg_item_cnt'].astype(np.float16)
 matrix = lag_feature(matrix, [1], ['date_item_city_avg_item_cnt'])
 matrix.drop(['date_item_city_avg_item_cnt'], axis=1, inplace=True)
 time.time() - ts
rfr.fit(X, y )<load_from_csv>
ts = time.time()
group = train.groupby(["item_id"] ).agg({"item_price": ["mean"]})
 group.columns = ["item_avg_item_price"]
group.reset_index(inplace = True)
 matrix = matrix.merge(group, on = ["item_id"], how = "left")
 matrix["item_avg_item_price"] = matrix.item_avg_item_price.astype(np.float16)
 group = train.groupby(["date_block_num","item_id"] ).agg({"item_price": ["mean"]})
 group.columns = ["date_item_avg_item_price"]
group.reset_index(inplace = True)
 matrix = matrix.merge(group, on = ["date_block_num","item_id"], how = "left")
 matrix["date_item_avg_item_price"] = matrix.date_item_avg_item_price.astype(np.float16)
 lags = [1, 2, 3]
matrix = lag_feature(matrix, lags, ["date_item_avg_item_price"])
 for i in lags:
matrix["delta_price_lag_" + str(i)] =(matrix["date_item_avg_item_price_lag_" + str(i)]- matrix["item_avg_item_price"])/ matrix["item_avg_item_price"]
def select_trends(row):
for i in lags:
if row["delta_price_lag_" + str(i)]:
return row["delta_price_lag_" + str(i)]
return 0
matrix["delta_price_lag"] = matrix.apply(select_trends, axis = 1)
 matrix["delta_price_lag"] = matrix.delta_price_lag.astype(np.float16)
 matrix["delta_price_lag"].fillna(0 ,inplace = True)
 features_to_drop = ["item_avg_item_price", "date_item_avg_item_price"]
for i in lags:
features_to_drop.append("date_item_avg_item_price_lag_" + str(i))
features_to_drop.append("delta_price_lag_" + str(i))
matrix.drop(features_to_drop, axis = 1, inplace = True)
 time.time() - ts
TESTFILEPATH = os.path.join('.. ', 'input', 'test.csv')
 test = pd.read_csv(TESTFILEPATH )<feature_engineering>
ts = time.time()
group = train.groupby(["date_block_num","shop_id"] ).agg({"revenue": ["sum"] })
 group.columns = ["date_shop_revenue"]
group.reset_index(inplace = True)
 matrix = matrix.merge(group , on = ["date_block_num", "shop_id"], how = "left")
 matrix['date_shop_revenue'] = matrix['date_shop_revenue'].astype(np.float32)
 group = group.groupby(["shop_id"] ).agg({ "date_block_num":["mean"] })
 group.columns = ["shop_avg_revenue"]
group.reset_index(inplace = True)
 matrix = matrix.merge(group, on = ["shop_id"], how = "left")
 matrix["shop_avg_revenue"] = matrix.shop_avg_revenue.astype(np.float32)
 matrix["delta_revenue"] =(matrix['date_shop_revenue'] - matrix['shop_avg_revenue'])/ matrix['shop_avg_revenue']
matrix["delta_revenue"] = matrix["delta_revenue"].astype(np.float32)
 matrix = lag_feature(matrix, [1], ["delta_revenue"])
 matrix["delta_revenue_lag_1"] = matrix["delta_revenue_lag_1"].astype(np.float32)
 matrix.drop(["date_shop_revenue", "shop_avg_revenue", "delta_revenue"] ,axis = 1, inplace = True)
 time.time() - ts
test['pickup_datetime'] = pd.to_datetime(test['pickup_datetime'])
 test['month_pickup']=test['pickup_datetime'].dt.month
test['day_pickup']=test['pickup_datetime'].dt.dayofweek
test['hour_pickup']=test['pickup_datetime'].dt.hour
test['minute_pickup']=test['pickup_datetime'].dt.minute
test['second_pickup']=test['pickup_datetime'].dt.second
test = test.drop(columns=['pickup_datetime'])
 test['trip_distance']=test.apply(distance, axis=1 )<prepare_x_and_y>
matrix["days"] = matrix["month"].map(days ).astype(np.int8 )
test_X = test[features]<predict_on_test>
ts = time.time()
matrix["item_shop_first_sale"] = matrix["date_block_num"] - matrix.groupby(["item_id","shop_id"])["date_block_num"].transform('min')
 matrix["item_first_sale"] = matrix["date_block_num"] - matrix.groupby(["item_id"])["date_block_num"].transform('min')
 time.time() - ts
predicted_duration_log = rfr.predict(test_X )<prepare_output>
ts = time.time()
matrix = matrix[matrix["date_block_num"] > 3]
time.time() - ts
predicted_duration = np.exp(predicted_duration_log)
 predicted_duration<save_to_csv>
rcParams['figure.figsize'] = 12, 4
my_submission = pd.DataFrame({'id': test['id'], 'trip_duration': predicted_duration})
 WORKINGFILEPATH = os.path.join('.. ', 'working', 'submission.csv')
 my_submission.to_csv(WORKINGFILEPATH, index=False )<load_from_csv>
data = matrix.copy()
del matrix
gc.collect()
df_train = pd.read_csv('.. /input/train.csv')
 df_test = pd.read_csv('.. /input/test.csv' )<load_from_csv>
X_train = data[data.date_block_num < 33].drop(['item_cnt_month'], axis=1)
 Y_train = data[data.date_block_num < 33]['item_cnt_month']
X_valid = data[data.date_block_num == 33].drop(['item_cnt_month'], axis=1)
 Y_valid = data[data.date_block_num == 33]['item_cnt_month']
X_test = data[data.date_block_num == 34].drop(['item_cnt_month'], axis=1 )
df_sample = pd.read_csv('.. /input/sample_submission.csv')
 df_sample.head()<count_unique_values>
del data
gc.collect() ;
len(df_train.index)== df_train.index.nunique()<count_unique_values>
ts = time.time()
model = XGBRegressor(
 max_depth=10,
n_estimators=1000,
min_child_weight=0.5,
colsample_bytree=0.8,
subsample=0.8,
eta=0.1,
seed=42)
 model.fit(
 X_train,
Y_train,
eval_metric="rmse",
eval_set=[(X_train, Y_train),(X_valid, Y_valid)],
verbose=True,
early_stopping_rounds = 20)
 time.time() - ts
len(df_test.index)== df_test.index.nunique()<count_missing_values>
Y_pred = model.predict(X_valid ).clip(0, 20)
 Y_test = model.predict(X_test ).clip(0, 20)
 submission = pd.DataFrame({
"ID": test.index,
"item_cnt_month": Y_test
})
 submission.to_csv('xgb_submission.csv', index=False)
 pickle.dump(Y_pred, open('xgb_train.pickle', 'wb'))
pickle.dump(Y_test, open('xgb_test.pickle', 'wb'))
df_train.isnull().values.any()<count_missing_values>
train_path = ".. /input/competitive-data-science-predict-future-sales/sales_train.csv"
test_path = ".. /input/competitive-data-science-predict-future-sales/test.csv"
items_path = ".. /input/competitive-data-science-predict-future-sales/items.csv"
shops_path = ".. /input/competitive-data-science-predict-future-sales/shops.csv"
item_cat_path = ".. /input/competitive-data-science-predict-future-sales/item_categories.csv"
df_test.isnull().values.any()<data_type_conversions>
items = pd.read_csv(items_path)
 cats = pd.read_csv(item_cat_path)
 shops = pd.read_csv(shops_path)
 train = pd.read_csv(train_path)
 test = pd.read_csv(test_path)
df_train['pickup_datetime'] = pd.to_datetime(df_train['pickup_datetime'])
 df_train['dropoff_datetime'] = pd.to_datetime(df_train['dropoff_datetime'])
 df_test['pickup_datetime'] = pd.to_datetime(df_test['pickup_datetime'] )<feature_engineering>
train = train[train.item_price > 0].reset_index(drop = True)
 train.loc[train.item_cnt_day < 1, "item_cnt_day"] = 0
df_train.loc[:, 'pickup_month'] = df_train['pickup_datetime'].dt.month
df_train.loc[:, 'pickup_weekday'] = df_train['pickup_datetime'].dt.weekday
df_train.loc[:, 'pickup_day'] = df_train['pickup_datetime'].dt.day
df_train.loc[:, 'pickup_hour'] = df_train['pickup_datetime'].dt.hour
df_train.loc[:, 'pickup_minute'] = df_train['pickup_datetime'].dt.minute
df_train.loc[:, 'dropoff_month'] = df_train['dropoff_datetime'].dt.month
df_train.loc[:, 'dropoff_weekday'] = df_train['dropoff_datetime'].dt.weekday
df_train.loc[:, 'dropoff_day'] = df_train['dropoff_datetime'].dt.day
df_train.loc[:, 'dropoff_hour'] = df_train['dropoff_datetime'].dt.hour
df_train.loc[:, 'dropoff_minute'] = df_train['dropoff_datetime'].dt.minute
df_test.loc[:, 'pickup_month'] = df_test['pickup_datetime'].dt.month
df_test.loc[:, 'pickup_weekday'] = df_test['pickup_datetime'].dt.weekday
df_test.loc[:, 'pickup_day'] = df_test['pickup_datetime'].dt.day
df_test.loc[:, 'pickup_hour'] = df_test['pickup_datetime'].dt.hour
df_test.loc[:, 'pickup_minute'] = df_test['pickup_datetime'].dt.minute 
<compute_test_metric>
train.loc[train.shop_id == 0, "shop_id"] = 57
test.loc[test.shop_id == 0 , "shop_id"] = 57
train.loc[train.shop_id == 1, "shop_id"] = 58
test.loc[test.shop_id == 1 , "shop_id"] = 58
train.loc[train.shop_id == 11, "shop_id"] = 10
test.loc[test.shop_id == 11, "shop_id"] = 10
train.loc[train.shop_id == 40, "shop_id"] = 39
test.loc[test.shop_id == 40, "shop_id"] = 39
def calculate_distance(longitude1, latitude1, longitude2, latitude2):
R = 6373.0
lat1 = radians(latitude1)
 lon1 = radians(longitude1)
 lat2 = radians(latitude2)
 lon2 = radians(longitude2)
 dlon = lon2 - lon1
dlat = lat2 - lat1
a = sin(dlat / 2)**2 + cos(lat1)* cos(lat2)* sin(dlon / 2)**2
c = 2 * atan2(sqrt(a), sqrt(1 - a))
return R * c
def add_distance(df):
return df.apply(lambda row: calculate_distance(
 row.pickup_longitude,
row.pickup_latitude,
row.dropoff_longitude,
row.dropoff_latitude 
), axis=1)
 df_train['distance_km'] = add_distance(df_train)
 df_test['distance_km'] = add_distance(df_test)
 df_train.head()<categorify>
shops.loc[ shops.shop_name == 'Сергиев Посад ТЦ "7Я"',"shop_name" ] = 'СергиевПосад ТЦ "7Я"'
shops["city"] = shops.shop_name.str.split(" " ).map(lambda x: x[0])
 shops["category"] = shops.shop_name.str.split(" " ).map(lambda x: x[1])
 shops.loc[shops.city == "!Якутск", "city"] = "Якутск"
le = LabelEncoder()
le.fit(df_train['store_and_fwd_flag'])
 df_train['store_and_fwd_flag'] = le.transform(df_train['store_and_fwd_flag'])
 df_test['store_and_fwd_flag'] = le.transform(df_test['store_and_fwd_flag'])
 le.fit(df_train['vendor_id'])
 df_train['vendor_id'] = le.transform(df_train['vendor_id'])
 df_test['vendor_id'] = le.transform(df_test['vendor_id'] )<define_variables>
shops.category = shops.category.apply(lambda x: x if(x in category)else "etc" )
train_variables = ["vendor_id","passenger_count","store_and_fwd_flag","pickup_longitude","pickup_latitude","dropoff_longitude","dropoff_latitude","pickup_month","pickup_weekday","pickup_day","pickup_hour",'pickup_minute',"distance_km"]<prepare_x_and_y>
for cat in shops.category.unique() :
print(cat, len(shops[shops.category == cat]))
y = np.log1p(df_train['trip_duration'])
 df_train["trip_duration"]
X = df_train[train_variables]
X.shape, y.shape 
<split>
shops["shop_category"] = LabelEncoder().fit_transform(shops.category)
 shops["shop_city"] = LabelEncoder().fit_transform(shops.city )
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=1337)
 X_train.shape, y_train.shape, X_test.shape, y_test.shape<split>
shops = shops[["shop_id", "shop_category", "shop_city"]]
X_train, X_valid, y_train, y_valid = train_test_split(X_train, y_train, random_state=1337)
 X_train.shape, y_train.shape, X_valid.shape, y_valid.shape<train_model>
cats["type_code"] = cats.item_category_name.apply(lambda x: x.split(" ")[0] ).astype(str)
 cats.loc[(cats.type_code == "Игровые")|(cats.type_code == "Аксессуары"), "category" ] = "Игры"
rf = RandomForestRegressor(random_state=1337,n_jobs=-1,bootstrap=True,n_estimators=20)
 fit = rf.fit(X_train, y_train )<compute_test_metric>
cats.type_code = cats.type_code.apply(lambda x: x if(x in category)else "etc" )
score = rf.score(X_valid, y_valid)
 print(score )<predict_on_test>
for cat in cats.type_code.unique() :
print(cat, len(cats[cats.type_code == cat]))
test_columns = X_train.columns
predictions = rf.predict(df_test[test_columns] )<prepare_output>
cats.type_code = LabelEncoder().fit_transform(cats.type_code)
 cats["split"] = cats.item_category_name.apply(lambda x: x.split("-"))
cats["subtype"] = cats.split.apply(lambda x: x[1].strip() if len(x)> 1 else x[0].strip())
 cats["subtype_code"] = LabelEncoder().fit_transform(cats["subtype"])
 cats = cats[["item_category_id", "subtype_code", "type_code"]]
submission = pd.DataFrame({'id': df_test.id, 'trip_duration': np.expm1(predictions)})
 submission.head()<save_to_csv>
def name_correction(x):
x = x.lower()
x = x.partition('[')[0]
x = x.partition('(')[0]
x = re.sub('[^A-Za-z0-9А-Яа-я]+', ' ', x)
 x = x.replace(' ', ' ')
 x = x.strip()
return x
submission.to_csv("submission_florian_coupechoux_nyc_taxi.csv", index=False)
<set_options>
items = pd.read_csv(items_path)
 items["name1"], items["name2"] = items.item_name.str.split("[", 1 ).str
items["name1"], items["name3"] = items.item_name.str.split("(", 1 ).str
items["name2"] = items.name2.str.replace('[^A-Za-z0-9А-Яа-я]+', " " ).str.lower()
items["name3"] = items.name3.str.replace('[^A-Za-z0-9А-Яа-я]+', " " ).str.lower()
items = items.fillna('0')
 items["item_name"] = items["item_name"].apply(lambda x: name_correction(x))
items.name2 = items.name2.apply(lambda x: x[:-1] if x !="0" else "0" )
%matplotlib inline<load_from_csv>
items["type"] = items.name2.apply(lambda x: x[0:8] if x.split(" ")[0] == "xbox" else x.split(" ")[0])
 items.loc[(items.type == "x360")|(items.type == "xbox360")|(items.type == "xbox 360"),"type"] = "xbox 360"
items.loc[ items.type == "", "type"] = "mac"
items.type = items.type.apply(lambda x: x.replace(" ", ""))
items.loc[(items.type == 'pc')|(items.type == 'pс')|(items.type == "pc"), "type" ] = "pc"
items.loc[ items.type == 'рs3' , "type"] = "ps3"
df_train = pd.read_csv('.. /input/train.csv', index_col='id')
 df_test = pd.read_csv('.. /input/test.csv', index_col='id' )<import_modules>
group_sum = items.groupby(["type"] ).agg({"item_id": "count"})
 print(group_sum.reset_index())
 group_sum = group_sum.reset_index()
from geopy.distance import geodesic
from haversine import haversine<compute_test_metric>
items.name2 = items.name2.apply(lambda x: "etc" if(x in drop_cols)else x)
 items = items.drop(["type"], axis = 1 )
def calculate_distance(coords1, coords2):
return haversine(coords1, coords2 )<define_variables>
items.name2 = LabelEncoder().fit_transform(items.name2)
 items.name3 = LabelEncoder().fit_transform(items.name3)
 items.drop(["item_name", "name1"],axis = 1, inplace= True)
 items.head()
periods = {
'morning': {
'min': 6,
'max': 12
},
'afternoon': {
'min': 12,
'max': 18
},
'evening': {
'min': 18,
'max': 24
},
'night': {
'min': 0,
'max': 6
},
}
rush_hours = [ 
(7, 9), 
(16, 18)
 ]
def get_time_period(hour):
for p in periods:
if hour >= periods[p]['min'] and hour < periods[p]['max']:
return p
def is_during_rush(hour):
for h in rush_hours:
if hour >= h[0] and hour < h[1]:
return True
return False 
<feature_engineering>
ts = time.time()
matrix = []
cols = ["date_block_num", "shop_id", "item_id"]
for i in range(34):
sales = train[train.date_block_num == i]
matrix.append(np.array(list(product([i], sales.shop_id.unique() , sales.item_id.unique())) , dtype = np.int16))
matrix = pd.DataFrame(np.vstack(matrix), columns = cols)
 matrix["date_block_num"] = matrix["date_block_num"].astype(np.int8)
 matrix["shop_id"] = matrix["shop_id"].astype(np.int8)
 matrix["item_id"] = matrix["item_id"].astype(np.int16)
 matrix.sort_values(cols, inplace = True)
 time.time() - ts
def extract_date_features(df, col):

 df[col + '_year'] = df[col].dt.year
df[col + '_month'] = df[col].dt.month
df[col + '_week'] = df[col].dt.week
df[col + '_day'] = df[col].dt.day
df[col + '_dow'] = df[col].dt.dayofweek
df[col + '_hour'] = df[col].dt.hour
df[col + '_minute'] = df[col].dt.minute
df[col + '_weekday'] = df[col].dt.weekday
df[col + '_days_in_month'] = df[col].dt.days_in_month
df[col + '_is_month_start'] = df[col].dt.is_month_start
df[col + '_is_month_end'] = df[col].dt.is_month_end
df[col + '_period'] = df[col].dt.hour.apply(get_time_period)
 df['is_during_rush_hour'] = df[col].dt.hour.apply(is_during_rush)
 return df
df_train['pickup_datetime_dt'] = df_train.pickup_datetime.apply(lambda row: datetime.strptime(row, '%Y-%m-%d %H:%M:%S'))
df_test['pickup_datetime_dt'] = df_test.pickup_datetime.apply(lambda row: datetime.strptime(row, '%Y-%m-%d %H:%M:%S'))
df_train = extract_date_features(df_train, 'pickup_datetime_dt')
 df_test = extract_date_features(df_test, 'pickup_datetime_dt' )<feature_engineering>
train["revenue"] = train["item_cnt_day"] * train["item_price"]
<merge>
ts = time.time()
group = train.groupby(["date_block_num", "shop_id", "item_id"] ).agg({"item_cnt_day": ["sum"]})
 group.columns = ["item_cnt_month"]
group.reset_index(inplace = True)
 matrix = pd.merge(matrix, group, on = cols, how = "left")
 matrix["item_cnt_month"] = matrix["item_cnt_month"].fillna(0 ).clip(0,20 ).astype(np.float16)
 time.time() - ts
train = df_train
test = df_test<define_variables>
test["date_block_num"] = 34
test["date_block_num"] = test["date_block_num"].astype(np.int8)
 test["shop_id"] = test.shop_id.astype(np.int8)
 test["item_id"] = test.item_id.astype(np.int16)
train.store_and_fwd_flag = [1 if flag == 'N' else 2 for flag in train.store_and_fwd_flag]<define_variables>
ts = time.time()
matrix = pd.concat([matrix, test.drop(["ID"],axis = 1)], ignore_index=True, sort=False, keys=cols)
 matrix.fillna(0, inplace = True)
 time.time() - ts
test.store_and_fwd_flag = [1 if flag == 'N' else 2 for flag in test.store_and_fwd_flag]<data_type_conversions>
ts = time.time()
matrix = pd.merge(matrix, shops, on = ["shop_id"], how = "left")
 matrix = pd.merge(matrix, items, on = ["item_id"], how = "left")
 matrix = pd.merge(matrix, cats, on = ["item_category_id"], how = "left")
 matrix["shop_city"] = matrix["shop_city"].astype(np.int8)
 matrix["shop_category"] = matrix["shop_category"].astype(np.int8)
 matrix["item_category_id"] = matrix["item_category_id"].astype(np.int8)
 matrix["subtype_code"] = matrix["subtype_code"].astype(np.int8)
 matrix["name2"] = matrix["name2"].astype(np.int8)
 matrix["name3"] = matrix["name3"].astype(np.int16)
 matrix["type_code"] = matrix["type_code"].astype(np.int8)
 time.time() - ts
def to_category(keys):
for key in keys:
train[key] = train[key].astype('category')
 test[key] = test[key].astype('category')
 CATEGORIES = [
'vendor_id',
'store_and_fwd_flag',
'passenger_count'
]
to_category(CATEGORIES )<import_modules>
def lag_feature(df,lags, cols):
for col in cols:
print(col)
 tmp = df[["date_block_num", "shop_id","item_id",col ]]
for i in lags:
shifted = tmp.copy()
shifted.columns = ["date_block_num", "shop_id", "item_id", col + "_lag_"+str(i)]
shifted.date_block_num = shifted.date_block_num + i
df = pd.merge(df, shifted, on=['date_block_num','shop_id','item_id'], how='left')
 return df
from sklearn.ensemble import RandomForestRegressor
from sklearn.model_selection import train_test_split<prepare_x_and_y>
ts = time.time()
group = matrix.groupby(["date_block_num"] ).agg({"item_cnt_month" : ["mean"]})
 group.columns = ["date_avg_item_cnt"]
group.reset_index(inplace = True)
 matrix = pd.merge(matrix, group, on = ["date_block_num"], how = "left")
 matrix.date_avg_item_cnt = matrix["date_avg_item_cnt"].astype(np.float16)
 matrix = lag_feature(matrix, [1], ["date_avg_item_cnt"])
 matrix.drop(["date_avg_item_cnt"], axis = 1, inplace = True)
 time.time() - ts
rf = RandomForestRegressor(n_estimators=10)
 COLUMNS = [
'pickup_datetime_dt_month',
'pickup_datetime_dt_day',
'pickup_datetime_dt_dow',
'pickup_datetime_dt_hour',
'pickup_longitude',
'pickup_latitude',
'dropoff_longitude',
'dropoff_latitude',
'distance',
'passenger_count',
'vendor_id'
]
X_train = train[COLUMNS]
y_train = train.trip_duration
X_test = test[COLUMNS]<import_modules>
ts = time.time()
group = matrix.groupby(['date_block_num', 'item_id'] ).agg({'item_cnt_month': ['mean']})
 group.columns = [ 'date_item_avg_item_cnt' ]
group.reset_index(inplace=True)
 matrix = pd.merge(matrix, group, on=['date_block_num','item_id'], how='left')
 matrix.date_item_avg_item_cnt = matrix['date_item_avg_item_cnt'].astype(np.float16)
 matrix = lag_feature(matrix, [1,2,3], ['date_item_avg_item_cnt'])
 matrix.drop(['date_item_avg_item_cnt'], axis=1, inplace=True)
 time.time() - ts
from math import log, exp<train_model>
ts = time.time()
group = matrix.groupby(["date_block_num","shop_id"] ).agg({"item_cnt_month" : ["mean"]})
 group.columns = ["date_shop_avg_item_cnt"]
group.reset_index(inplace = True)
 matrix = pd.merge(matrix, group, on = ["date_block_num","shop_id"], how = "left")
 matrix.date_avg_item_cnt = matrix["date_shop_avg_item_cnt"].astype(np.float16)
 matrix = lag_feature(matrix, [1,2,3], ["date_shop_avg_item_cnt"])
 matrix.drop(["date_shop_avg_item_cnt"], axis = 1, inplace = True)
 time.time() - ts
rf_X_train = X_train
rf_y_train = y_train
rf_y_train = [log(y)for y in rf_y_train]
rf.fit(rf_X_train, rf_y_train)
 y_hat = rf.predict(X_test )<define_variables>
ts = time.time()
group = matrix.groupby(["date_block_num","shop_id","item_id"] ).agg({"item_cnt_month" : ["mean"]})
 group.columns = ["date_shop_item_avg_item_cnt"]
group.reset_index(inplace = True)
 matrix = pd.merge(matrix, group, on = ["date_block_num","shop_id","item_id"], how = "left")
 matrix.date_avg_item_cnt = matrix["date_shop_item_avg_item_cnt"].astype(np.float16)
 matrix = lag_feature(matrix, [1,2,3], ["date_shop_item_avg_item_cnt"])
 matrix.drop(["date_shop_item_avg_item_cnt"], axis = 1, inplace = True)
 time.time() - ts
y_hat = [exp(y)for y in y_hat]<prepare_output>
ts = time.time()
group = matrix.groupby(['date_block_num', 'shop_id', 'subtype_code'] ).agg({'item_cnt_month': ['mean']})
 group.columns = ['date_shop_subtype_avg_item_cnt']
group.reset_index(inplace=True)
 matrix = pd.merge(matrix, group, on=['date_block_num', 'shop_id', 'subtype_code'], how='left')
 matrix.date_shop_subtype_avg_item_cnt = matrix['date_shop_subtype_avg_item_cnt'].astype(np.float16)
 matrix = lag_feature(matrix, [1], ['date_shop_subtype_avg_item_cnt'])
 matrix.drop(['date_shop_subtype_avg_item_cnt'], axis=1, inplace=True)
 time.time() - ts
np.mean(y_hat )<load_from_csv>
ts = time.time()
group = matrix.groupby(['date_block_num', 'shop_city'] ).agg({'item_cnt_month': ['mean']})
 group.columns = ['date_city_avg_item_cnt']
group.reset_index(inplace=True)
 matrix = pd.merge(matrix, group, on=['date_block_num', "shop_city"], how='left')
 matrix.date_city_avg_item_cnt = matrix['date_city_avg_item_cnt'].astype(np.float16)
 matrix = lag_feature(matrix, [1], ['date_city_avg_item_cnt'])
 matrix.drop(['date_city_avg_item_cnt'], axis=1, inplace=True)
 time.time() - ts
sample = pd.read_csv('.. /input/sample_submission.csv' )<save_to_csv>
ts = time.time()
group = matrix.groupby(['date_block_num', 'item_id', 'shop_city'] ).agg({'item_cnt_month': ['mean']})
 group.columns = [ 'date_item_city_avg_item_cnt' ]
group.reset_index(inplace=True)
 matrix = pd.merge(matrix, group, on=['date_block_num', 'item_id', 'shop_city'], how='left')
 matrix.date_item_city_avg_item_cnt = matrix['date_item_city_avg_item_cnt'].astype(np.float16)
 matrix = lag_feature(matrix, [1], ['date_item_city_avg_item_cnt'])
 matrix.drop(['date_item_city_avg_item_cnt'], axis=1, inplace=True)
 time.time() - ts
sample.trip_duration = y_hat
sample.to_csv('results.csv', index=False )<import_modules>
ts = time.time()
group = train.groupby(["item_id"] ).agg({"item_price": ["mean"]})
 group.columns = ["item_avg_item_price"]
group.reset_index(inplace = True)
 matrix = matrix.merge(group, on = ["item_id"], how = "left")
 matrix["item_avg_item_price"] = matrix.item_avg_item_price.astype(np.float16)
 group = train.groupby(["date_block_num","item_id"] ).agg({"item_price": ["mean"]})
 group.columns = ["date_item_avg_item_price"]
group.reset_index(inplace = True)
 matrix = matrix.merge(group, on = ["date_block_num","item_id"], how = "left")
 matrix["date_item_avg_item_price"] = matrix.date_item_avg_item_price.astype(np.float16)
 lags = [1, 2, 3]
matrix = lag_feature(matrix, lags, ["date_item_avg_item_price"])
 for i in lags:
matrix["delta_price_lag_" + str(i)] =(matrix["date_item_avg_item_price_lag_" + str(i)]- matrix["item_avg_item_price"])/ matrix["item_avg_item_price"]
def select_trends(row):
for i in lags:
if row["delta_price_lag_" + str(i)]:
return row["delta_price_lag_" + str(i)]
return 0
matrix["delta_price_lag"] = matrix.apply(select_trends, axis = 1)
 matrix["delta_price_lag"] = matrix.delta_price_lag.astype(np.float16)
 matrix["delta_price_lag"].fillna(0 ,inplace = True)
 features_to_drop = ["item_avg_item_price", "date_item_avg_item_price"]
for i in lags:
features_to_drop.append("date_item_avg_item_price_lag_" + str(i))
features_to_drop.append("delta_price_lag_" + str(i))
matrix.drop(features_to_drop, axis = 1, inplace = True)
 time.time() - ts
import matplotlib.pyplot as plt
import numpy as np
import os
import pandas as pd
import seaborn as sns
import xgboost as xgb
from catboost import CatBoostRegressor, Pool
from catboost import cv as catboost_cv
from IPython.display import display
from sklearn.cluster import MiniBatchKMeans
from sklearn.model_selection import train_test_split, RandomizedSearchCV
from sklearn.metrics import mean_squared_error<set_options>
ts = time.time()
group = train.groupby(["date_block_num","shop_id"] ).agg({"revenue": ["sum"] })
 group.columns = ["date_shop_revenue"]
group.reset_index(inplace = True)
 matrix = matrix.merge(group , on = ["date_block_num", "shop_id"], how = "left")
 matrix['date_shop_revenue'] = matrix['date_shop_revenue'].astype(np.float32)
 group = group.groupby(["shop_id"] ).agg({ "date_block_num":["mean"] })
 group.columns = ["shop_avg_revenue"]
group.reset_index(inplace = True)
 matrix = matrix.merge(group, on = ["shop_id"], how = "left")
 matrix["shop_avg_revenue"] = matrix.shop_avg_revenue.astype(np.float32)
 matrix["delta_revenue"] =(matrix['date_shop_revenue'] - matrix['shop_avg_revenue'])/ matrix['shop_avg_revenue']
matrix["delta_revenue"] = matrix["delta_revenue"].astype(np.float32)
 matrix = lag_feature(matrix, [1], ["delta_revenue"])
 matrix["delta_revenue_lag_1"] = matrix["delta_revenue_lag_1"].astype(np.float32)
 matrix.drop(["date_shop_revenue", "shop_avg_revenue", "delta_revenue"] ,axis = 1, inplace = True)
 time.time() - ts
%matplotlib inline<define_variables>
matrix["days"] = matrix["month"].map(days ).astype(np.int8 )
DO_XGBOOST = True
DO_CATBOOST = not DO_XGBOOST<load_from_csv>
ts = time.time()
matrix["item_shop_first_sale"] = matrix["date_block_num"] - matrix.groupby(["item_id","shop_id"])["date_block_num"].transform('min')
 matrix["item_first_sale"] = matrix["date_block_num"] - matrix.groupby(["item_id"])["date_block_num"].transform('min')
 time.time() - ts
train_raw = pd.read_csv('.. /input/train/train.csv', index_col='id',
parse_dates=['pickup_datetime', 'dropoff_datetime'])
 test_raw = pd.read_csv('.. /input/test/test.csv', index_col='id',
parse_dates=['pickup_datetime'] )<count_missing_values>
ts = time.time()
matrix = matrix[matrix["date_block_num"] > 3]
time.time() - ts
print('NaN or empty data(train): {}'.format(train_raw.isnull().values.any()))
print('NaN or empty data(test): {}'.format(test_raw.isnull().values.any()))<create_dataframe>
rcParams['figure.figsize'] = 12, 4
train_set = train_raw.copy(deep=True ).drop('dropoff_datetime', axis=1)
 test_set = test_raw.copy(deep=True )<define_variables>
data = matrix.copy()
del matrix
gc.collect()
checkpoints = {
'pre_engineering': {
'train': train_set.copy(deep=True),
'test': test_set.copy(deep=True)
 }
}<categorify>
X_train = data[data.date_block_num < 33].drop(['item_cnt_month'], axis=1)
 Y_train = data[data.date_block_num < 33]['item_cnt_month']
X_valid = data[data.date_block_num == 33].drop(['item_cnt_month'], axis=1)
 Y_valid = data[data.date_block_num == 33]['item_cnt_month']
X_test = data[data.date_block_num == 34].drop(['item_cnt_month'], axis=1 )
def add_checkpoint(key):
d = { 'train': train_set.copy(deep=True), 'test': test_set.copy(deep=True)}
checkpoints.update({
key: d
} )<drop_column>
del data
gc.collect() ;
def restore_checkpoint(key):
return checkpoints[key]['train'].copy(deep=True), checkpoints[key]['test'].copy(deep=True )<categorify>
ts = time.time()
model = XGBRegressor(
 max_depth=10,
n_estimators=1000,
min_child_weight=0.5,
colsample_bytree=0.8,
subsample=0.8,
eta=0.1,
seed=42)
 model.fit(
 X_train,
Y_train,
eval_metric="rmse",
eval_set=[(X_train, Y_train),(X_valid, Y_valid)],
verbose=True,
early_stopping_rounds = 20)
 time.time() - ts
<concatenate><EOS> 
Y_pred = model.predict(X_valid ).clip(0, 20)
 Y_test = model.predict(X_test ).clip(0, 20)
 submission = pd.DataFrame({
"ID": test.index,
"item_cnt_month": Y_test
})
 submission.to_csv('xgb_submission.csv', index=False)
 pickle.dump(Y_pred, open('xgb_train.pickle', 'wb'))
pickle.dump(Y_test, open('xgb_test.pickle', 'wb'))