Kaggle competition: Predict Future Sales
df_train=pd.read_csv('/kaggle/input/titanic/train.csv', dtype=data_type )<prepare_x_and_y>
sales = pd.read_csv(os.path.join(dirname, 'sales_train.csv'))
items = pd.read_csv(os.path.join(dirname, 'items.csv'))
item_cat = pd.read_csv(os.path.join(dirname, 'item_categories.csv'))
shops = pd.read_csv(os.path.join(dirname, 'shops.csv'))
test = pd.read_csv(os.path.join(dirname, 'test.csv'))
submission = pd.read_csv(os.path.join(dirname, 'sample_submission.csv'))
X = df_train.drop(columns=['Survived','Name', 'PassengerId'], axis=1)
 di = {False: 0, True: 1}
y = df_train.replace({"Survived": di} ).Survived 
<data_type_conversions>
%matplotlib inline
categorical_cols = X.select_dtypes(include=['category', object, 'string'] ).columns.to_list()
for col in categorical_cols:
X[col] = X[col].astype('category')
 X[col] = X[col].cat.add_categories('Unknown')
 X[col] = X[col].fillna('Unknown' )<split>
for p in [np, pd, scipy,sklearn, seaborn, lgb]:
print(p.__name__, p.__version__ )
X_train, X_validation, y_train, y_validation = train_test_split(X, y, test_size = 0.3, stratify=y, random_state = 123 )<define_variables>
def downcast_dtypes(df):

 float_cols = [c for c in df if df[c].dtype == "float64"]
int_cols = [c for c in df if df[c].dtype == "int64"]
df[float_cols] = df[float_cols].astype(np.float32)
 df[int_cols] = df[int_cols].astype(np.int32)
 return df
train_pool = Pool(data=X_train,
label = y_train,
cat_features = categorical_cols)
 eval_pool = Pool(data=X_validation,
label = y_validation,
cat_features = categorical_cols )<train_model>
def get_rmse(actual, predicted):

 mse = mean_squared_error(actual, predicted)
 rmse = sqrt(mse)
 return rmse
model = CatBoostClassifier(custom_loss=['Accuracy'],iterations = 30000,od_type = 'Iter', od_wait= 1000,task_type = 'GPU', random_seed=42,)
 model.fit(train_pool,
eval_set = eval_pool,
use_best_model=True,
verbose = False )<save_to_csv>
sales_test = sales.copy()
test_grid = test[["shop_id", "item_id"]]
test_grid["date_block_num"] = 34
test_grid["month"] = 11
test_grid["date"] = test_grid["item_price"] = test_grid["item_cnt_day"] = 0
test_grid = test_grid.reindex(columns=sales.columns.values)
 sales_test = sales_test.append(test_grid )
df_test = pd.read_csv('/kaggle/input/titanic/test.csv', dtype = data_type)
 df_submission = pd.read_csv('/kaggle/input/titanic/gender_submission.csv')
 df_test.drop(columns=['Name', 'PassengerId'], axis=1, inplace=True)
 for col in categorical_cols:
df_test[col] = df_test[col].astype('category')
 df_test[col] = df_test[col].cat.add_categories('Unknown')
 df_test[col] = df_test[col].fillna('Unknown')
 test_pool = Pool(data=df_test, cat_features=categorical_cols)
 predictions = model.predict(test_pool)
 df_submission['Survived'] = predictions
df_submission.to_csv('catboost_submission_no_cv.csv', index=False )<train_on_grid>
cols_to_rename = list(all_data.columns.difference(index_cols))
shift_range = [1, 2, 3, 4, 5, 12]
for month_shift in notebook.tqdm(shift_range):
train_shift = all_data[index_cols + cols_to_rename].copy()
train_shift['date_block_num'] = train_shift['date_block_num'] + month_shift
foo = lambda x: '{}_lag_{}'.format(x, month_shift)if x in cols_to_rename else x
train_shift = train_shift.rename(columns=foo)
 all_data = pd.merge(all_data, train_shift, on=index_cols, how='left' ).fillna(0)
 del train_shift
fit_cols = [col for col in all_data.columns if col[-1] in [str(item)for item in shift_range]]
to_drop_cols = list(set(list(all_data.columns)) -(set(fit_cols)|set(index_cols)))+ ['date_block_num']
item_category_mapping = items[['item_id','item_category_id']].drop_duplicates()
all_data = pd.merge(all_data, item_category_mapping, how='left', on='item_id')
 all_data = downcast_dtypes(all_data)
 gc.collect() ;
grid = {'learning_rate': [0.03, 0.1, 0.01],
'iterations' : [500, 1000, 3000, 5000, 10000, 20000],
'depth': [4, 6, 10],
'l2_leaf_reg': [1, 3, 5, 7, 9]}
model = CatBoostClassifier(task_type = 'GPU', eval_metric = 'Accuracy')
 model_randomized_search_result = model.randomized_search(grid,
Pool(X, y, cat_features=categorical_cols),
cv = 3,
plot=True )<find_best_params>
shop_first_block = sales_test.groupby("shop_id" ).date_block_num.min()
all_data["shop_1st"] = all_data["shop_id"].map(shop_first_block)
 all_data["shop_1st"] = all_data["date_block_num"] - all_data["shop_1st"]
item_first_block = sales_test.groupby("item_id" ).date_block_num.min()
all_data["item_1st"] = all_data["item_id"].map(item_first_block)
 all_data["item_1st"] = all_data["date_block_num"] - all_data["item_1st"]
all_data.shop_1st.value_counts()
best_params = model_randomized_search_result['params']
print('Best parms found', best_params )<train_on_grid>
all_data["month"]=(all_data["date_block_num"]+1)%12
best_model_params = best_params.copy()
best_model_params.update({
'task_type': 'GPU'
})
 model = CatBoostClassifier(**best_model_params)
 model.fit(train_pool,
eval_set = eval_pool,
use_best_model=True,
verbose = False )<save_to_csv>
dates = all_data['date_block_num']
last_block = dates.max()
print('Test `date_block_num` is %d' % last_block )
predictions = model.predict(test_pool)
 df_submission['Survived'] = predictions
df_submission.to_csv('cb_submission_with_cv_param.csv', index=False )<prepare_x_and_y>
dates_train = dates[dates < last_block]
dates_test = dates[dates == last_block]
X_train = all_data.loc[dates < last_block].drop(to_drop_cols, axis=1)
 X_test = all_data.loc[dates == last_block].drop(to_drop_cols, axis=1)
 y_train = all_data.loc[dates < last_block, 'target'].values.clip(0,20)
 y_test = all_data.loc[dates == last_block, 'target'].values.clip(0,20)
 print(X_train.head() )
X = df_train.drop(columns=['Survived','PassengerId'], axis=1)
 for col in categorical_cols:
X[col] = X[col].astype('category')
 X[col] = X[col].cat.add_categories('Unknown')
 X[col] = X[col].fillna('Unknown')
 di = {False: 0, True: 1}
y = df_train.replace({"Survived": di} ).Survived
text_feature = ['Name']<feature_engineering>
X_test_compare = pd.DataFrame(data=X_test, columns=["ID", "shop_id", "item_id"])
 X_test_compare.reset_index(inplace=True, drop=True)
 X_test_compare["ID"]=X_test_compare.index
print(test.columns, test.shape, test.head())
 print(X_test_compare.columns, X_test_compare.shape, X_test_compare.head())
 print("Are the same",(test == X_test_compare ).value_counts() )
X.Name = X.Name.apply(lambda x: x.lower())
 X.Name = X.Name.apply(lambda x: x.translate(string.punctuation))
X.Name = X.Name.apply(lambda x: x.translate(string.digits))<split>
reg = make_pipeline(StandardScaler() , SGDRegressor(loss="epsilon_insensitive"))
reg.fit(X_train.values, y_train)
 pred_reg = reg.predict(X_test.values ).clip(0,20 )
X_train, X_validation, y_train, y_validation = train_test_split(X, y, test_size = 0.3, stratify=y, random_state = 123)
 train_text_pool = Pool(data=X_train,
label = y_train,
cat_features = categorical_cols,
text_features = text_feature,
feature_names=list(X_train), 
)
 eval_text_pool = Pool(data=X_validation,
label = y_validation,
cat_features = categorical_cols,
text_features = text_feature,
feature_names=list(X_validation), 
)
 model_with_text = CatBoostClassifier(
 custom_loss=['Accuracy'],
iterations = 30000,
od_type = 'Iter',
od_wait= 2000,
task_type = 'GPU',
random_seed=42,)
 model_with_text.fit(train_text_pool,
eval_set = eval_text_pool,
use_best_model=True,
metric_period=1000,
verbose = True )<load_from_csv>
print("r2 train: ", r2_score(y_train, reg.predict(X_train.values ).clip(0,20)) )
df_test = pd.read_csv('/kaggle/input/titanic/test.csv', dtype = data_type)
 df_test.Name = df_test.Name.apply(lambda x: x.lower())
 df_test.Name = df_test.Name.apply(lambda x: x.translate(string.punctuation))
df_test.Name = df_test.Name.apply(lambda x: x.translate(string.digits))
df_submission = pd.read_csv('/kaggle/input/titanic/gender_submission.csv')
 df_test.drop(columns=['PassengerId'], axis=1, inplace=True)
 for col in categorical_cols:
df_test[col] = df_test[col].astype('category')
 df_test[col] = df_test[col].cat.add_categories('Unknown')
 df_test[col] = df_test[col].fillna('Unknown')
 test_pool = Pool(data=df_test, cat_features=categorical_cols, text_features = text_feature)
 predictions = model_with_text.predict(test_pool)
 df_submission['Survived'] = predictions
df_submission.to_csv('cb_with_text.csv', index=False )<set_options>
filename = 'pred_reg_model_l1.sav'
joblib.dump(reg, filename )
warnings.filterwarnings('ignore')
 %matplotlib inline 
<load_from_csv>
lgb_params = {
'feature_fraction': 0.75,
'metric': 'rmse',
'nthread':1,
'min_data_in_leaf': 2**7,
'bagging_fraction': 0.75,
'learning_rate': 0.03,
'objective': 'mse',
'bagging_seed': 2**7,
'num_leaves': 2**7,
'bagging_freq':1,
'verbose':0
}
lgb1 = lgb.train(lgb_params, lgb.Dataset(X_train, label=y_train), 100)
 pred_lgb = lgb1.predict(X_test ).clip(0,20 )
train_df = pd.read_csv('/kaggle/input/titanic/train.csv')
 test_df = pd.read_csv('/kaggle/input/titanic/test.csv')
 combine = [train_df, test_df] 
<groupby>
print("r2 train: ", r2_score(y_train, lgb1.predict(X_train ).clip(0,20)) )
train_df[['Pclass', 'Survived']].groupby(['Pclass'] ).mean()<count_values>
lr = LinearRegression()
lr.fit(X_train_level2, y_train_level2)
 print('Coefficient: {}'.format(lr.coef_))
print('Normalized Coefficient: {}'.format(lr.coef_ / lr.coef_.sum()))
target_count = train_df['Survived'].value_counts()
target_count<count_missing_values>
test_preds_stacking_lr = lr.predict(np.vstack(( pred_reg, pred_lgb)).T ).clip(0,20)
 test_preds_stacking_lr.shape
print(train_df.isnull().sum())
 print()
print(test_df.isnull().sum())
<feature_engineering>
print("r2 train: ", r2_score(y_train_level2, lr.predict(X_train_level2 ).clip(0,20)) )
for dataset in combine:
dataset['Title'] = dataset['Name'].str.extract('([A-Za-z]+)\.',\
expand=False )<feature_engineering>
filename = 'pred_stack_model_l2.sav'
joblib.dump(lr, filename )
for dataset in combine:
dataset['Title'] = dataset['Title'].\
replace(['Lady', 'Countess','Capt', 'Col', 'Don', 'Dr',\
'Major', 'Rev', 'Sir', 'Jonkheer', 'Dona'], 'Rare')
 dataset['Title'] = dataset['Title'].replace('Mlle', 'Miss')
 dataset['Title'] = dataset['Title'].replace('Ms', 'Miss')
 dataset['Title'] = dataset['Title'].replace('Mme', 'Mrs' )<groupby>
submissionstackinglr = submission.copy()
submissionstackinglr["item_cnt_month"]=test_preds_stacking_lr
print(submissionstackinglr.item_cnt_month.min() , submissionstackinglr.item_cnt_month.max())
 submissionstackinglr["item_cnt_month"]
print(submissionstackinglr.item_cnt_month.min() , submissionstackinglr.item_cnt_month.max())
 print(submissionstackinglr.head() )
<categorify><EOS> 
submissionstackinglr.to_csv('cu_full_1_5_12_reg_lgb_stacked_lr_v4.csv', index=False )