Kaggle competition: Titanic - Machine Learning from Disaster
before = len(sales_train_evaluation)
 sales_train_evaluation.dropna(inplace = True)
 after = len(sales_train_evaluation)
 print(f"Reduced {(before-after)/before}%" )<train_model>
train_data = pd.read_csv('/kaggle/input/titanic/train.csv')
 test_data = pd.read_csv('/kaggle/input/titanic/test.csv' )
%%time
print("Splitting data into train, validation, evaluation set")
 np.random.seed(777)
 SIZE = 2_000_000
validation_idx = np.random.choice(sales_train_evaluation.index.values, SIZE, replace = False)
 train_idx = np.setdiff1d(sales_train_evaluation.index.values, validation_idx)
 train = sales_train_evaluation.loc[train_idx]
validation = sales_train_evaluation.loc[validation_idx]
del sales_train_evaluation; gc.collect()
print("Train:", train.shape)
 print("Validation:", validation.shape )<define_variables>
train_data = train_data.drop(columns=['Name','Cabin'])
 train_data['family_member'] = train_data['SibSp'] + train_data['Parch']
train_data = train_data.drop(columns=['SibSp', 'Parch'] )
categorical_features = [
'item_id', 'dept_id', 'store_id', 'cat_id', 'state_id',
'event_name_1', 'event_type_1', 'event_name_2', 'event_type_2'
]<create_dataframe>
test_data = test_data.drop(columns=['Name','Cabin'])
 test_data['family_member'] = test_data['SibSp'] + test_data['Parch']
test_data = test_data.drop(columns=['SibSp', 'Parch'] )
train_pool = lgb.Dataset(
 data=train[train_columns],
label=train["sales"],
categorical_feature=categorical_features,
free_raw_data=False)
 del train; gc.collect()<create_dataframe>
train_data.fillna(0,inplace=True)
 test_data.fillna(0,inplace=True )
val_pool = lgb.Dataset(
 data=validation[train_columns],
label=validation["sales"],
categorical_feature=categorical_features,
free_raw_data=False 
)
 del validation; gc.collect()<init_hyperparams>
X = train_data.drop(columns=['Survived'])
 Y = train_data['Survived']
cate_features_index = np.where(X.dtypes != float)[0]
if TEST_RUN:
ITERATIONS=1
else:
ITERATIONS = 1200
params = {
"objective" : "poisson",
"metric" : "rmse",
"force_row_wise" : True,
"learning_rate" : 0.075,
"sub_row" : 0.75,
"bagging_freq" : 1,
"lambda_l2" : 0.1,
'verbosity': 1,
'num_iterations': ITERATIONS,
'num_leaves': 128,
"min_data_in_leaf": 100,
"early_stopping": 10
}<train_model>
from catboost import CatBoostClassifier, cv, Pool
%%time
model = lgb.train(
 params,
train_pool,
valid_sets = val_pool,
verbose_eval=20 )<drop_column>
from sklearn.model_selection import train_test_split
model.save_model("model_{}.lgb".format(MODEL_VERSION))
del train_pool, val_pool; gc.collect()<data_type_conversions>
from sklearn.model_selection import train_test_split
df = load_data(train=False)
 df["sale"] =(( df['sell_price'] * 100 % 10)< 6 ).astype('int8')
 make_date_features(df )<feature_engineering>
xtrain,xtest,ytrain,ytest = train_test_split(X,Y,train_size=0.82,random_state=42 )
def lag_features_for_day(dt, day):
print(type(dt))
lags = [7, 28]
lag_cols = [f"lag_{lag}" for lag in lags]
for lag, lag_col in zip(lags, lag_cols):
dt.loc[dt['date'] == str(day), lag_col] = \
dt.loc[dt['date'] == str(day-timedelta(days=lag)) , 'sales'].values
windows = [7, 28]
for window in windows:
for lag, lag_col in zip(lags, lag_cols):
df_window = dt[(dt['date'] <= str(day-timedelta(days=lag)))&(dt['date'] > str(day-timedelta(days=lag+window)))]
df_window_grouped = df_window.groupby("item_id" ).agg({'sales':'mean'} ).reindex(dt.loc[dt['date']==str(day),'item_id'])
 dt.loc[dt['date'] == str(day),f'item_id_rmean_{lag_col}_{window}'] = df_window_grouped.sales.values
lag = 28
lag_col = 'lag_28'
window = 28
df_window = dt[(dt['date'] <= str(day-timedelta(days=lag)))&(dt['date'] > str(day-timedelta(days=lag+window)))]
df_window_grouped = df_window.groupby("store_id" ).agg({'sales':'mean'} ).reindex(dt.loc[dt['date']==str(day),'store_id'])
 dt.loc[dt['date'] == str(day),f'store_id_rmean_{lag_col}_{window}'] = df_window_grouped.sales.values
print(f"Features done for day {str(day)}")
<create_dataframe>
clf =CatBoostClassifier(eval_metric='Accuracy',use_best_model=True,random_seed=42 )
%%time
END_DATE = EVAL_SPLIT
PREDICT_DAYS = 28
for f_day in tqdm(range(1,PREDICT_DAYS+1)) :
pred_date =(datetime.strptime(END_DATE, '%Y-%m-%d')+ timedelta(days=f_day)).date()
print(f"Forecasting day {END_DAY+f_day}, date: {str(pred_date)}")
 pred_begin_date = pred_date - timedelta(days=BACKWARD_LAG+1)
 prediction_data = df[(df['date'] >= str(pred_begin_date)) &(df['date'] <= str(pred_date)) ].copy()
lag_features_for_day(prediction_data, pred_date)
 prediction_data = prediction_data.loc[prediction_data['date'] == str(pred_date), train_columns]
prediction = model.predict(prediction_data)
 print("Prediction", prediction.size, prediction)
 df.loc[df['date'] == str(pred_date), 'sales'] = prediction<set_options>
clf.fit(xtrain,ytrain,cat_features=cate_features_index,eval_set=(xtest,ytest), early_stopping_rounds=50 )
del prediction_data
gc.collect()<load_from_csv>
test_id = test_data.PassengerId
sales_ = pd.read_csv(os.path.join(KAGGLE_DATA_FOLDER, 'sales_train_evaluation.csv'))
submission = pd.read_csv(os.path.join(SUBMISSION_PATH))<feature_engineering>
test_data.isnull().sum()
submission_eval = df.loc[df['date'] > END_DATE, ['id', 'day', 'sales']].copy()
submission_eval.loc[submission_eval['sales'] < 0, 'sales'] = 0
submission_eval.sort_values('id', inplace=True)
 submission_eval['day'] = submission_eval['day'].apply(lambda x: 'F{}'.format(x - END_DAY))
print(submission_eval.columns)
 print(submission_eval.head() , submission_eval.tail() , sep="
" )<data_type_conversions>
prediction = clf.predict(test_data )
<save_to_csv><EOS> 
df_sub = pd.DataFrame()
df_sub['PassengerId'] = test_id
df_sub['Survived'] = prediction.astype(np.int)
 df_sub.to_csv('gender_submission.csv', index=False )