Kaggle competition: Titanic - Machine Learning from Disaster
new_train = downcast_dtypes(new_train )<set_options>
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.linear_model import LogisticRegression
from sklearn.ensemble import RandomForestClassifier
from sklearn.linear_model import Perceptron
from sklearn.tree import DecisionTreeClassifier
from sklearn.svm import SVC
from sklearn.ensemble import StackingClassifier
from sklearn.preprocessing import StandardScaler
from sklearn.pipeline import make_pipeline
from sklearn.metrics import f1_score,mean_squared_error
gc.collect()<define_search_space>
data = pd.read_csv("/kaggle/input/titanic/train.csv")
 test_data = pd.read_csv("/kaggle/input/titanic/test.csv")
 data.shape
%%time
new_train = generate_lag(new_train, [1,2,3,4,5,6,12], 'item_cnt_month' )<merge>
data.nunique().sort_values(ascending=False ).head(5 )
%%time
group = new_train.groupby(['date_block_num', 'item_id'])['item_cnt_month'].mean().rename('item_month_mean' ).reset_index()
new_train = pd.merge(new_train, group, on=['date_block_num', 'item_id'], how='left')
 new_train = generate_lag(new_train, [1,2,3,6,12], 'item_month_mean')
 new_train.drop(['item_month_mean'], axis=1, inplace=True )<merge>
data.drop(["Name","Ticket","Cabin"],axis=1,inplace=True)
 data.head() ,data.shape
%%time
group = new_train.groupby(['date_block_num', 'shop_id'])['item_cnt_month'].mean().rename('shop_month_mean' ).reset_index()
new_train = pd.merge(new_train, group, on=['date_block_num', 'shop_id'], how='left')
 new_train = generate_lag(new_train, [1,2,3,6,12], 'shop_month_mean')
 new_train.drop(['shop_month_mean'], axis=1, inplace=True )<merge>
imp = SimpleImputer(missing_values=np.nan, strategy='mean')
 data.Age = imp.fit_transform(data[['Age']] ).ravel()
imp2 = SimpleImputer(missing_values=np.nan,strategy='most_frequent')
 data.Embarked = imp2.fit_transform(data[['Embarked']] ).ravel()
data.Age.mean() ,data.Age.std() ,data.Age.isnull().sum()
%%time
group = new_train.groupby(['date_block_num', 'shop_id', 'item_category_id'])['item_cnt_month'].mean().rename('shop_category_month_mean' ).reset_index()
new_train = pd.merge(new_train, group, on=['date_block_num', 'shop_id', 'item_category_id'], how='left')
 new_train = generate_lag(new_train, [1, 2], 'shop_category_month_mean')
 new_train.drop(['shop_category_month_mean'], axis=1, inplace=True )<merge>
data.groupby('Sex' ).Survived.mean()
%%time
group = new_train.groupby(['date_block_num', 'main_category_id'])['item_cnt_month'].mean().rename('main_category_month_mean' ).reset_index()
new_train = pd.merge(new_train, group, on=['date_block_num', 'main_category_id'], how='left')
 new_train = generate_lag(new_train, [1], 'main_category_month_mean')
 new_train.drop(['main_category_month_mean'], axis=1, inplace=True )<merge>
data['relatives'] = data['SibSp'] + data['Parch']
data.loc[data['relatives'] > 0, 'not_alone'] = 0
data.loc[data['relatives'] == 0, 'not_alone'] = 1
data['not_alone'] = data['not_alone'].astype(int )
%%time
group = new_train.groupby(['date_block_num', 'sub_category_id'])['item_cnt_month'].mean().rename('sub_category_month_mean' ).reset_index()
new_train = pd.merge(new_train, group, on=['date_block_num', 'sub_category_id'], how='left')
 new_train = generate_lag(new_train, [1], 'sub_category_month_mean')
 new_train.drop(['sub_category_month_mean'], axis=1, inplace=True )<feature_engineering>
data = data.join(pd.get_dummies(data['Sex']))
data = data.join(pd.get_dummies(data['Embarked']))
data.tail()
new_train['month'] = new_train['date_block_num'] % 12<categorify>
test_data.drop(["Name","Ticket","Cabin"],axis=1,inplace=True)
 test_data['relatives'] = test_data['SibSp'] + test_data['Parch']
test_data.loc[test_data['relatives'] > 0, 'not_alone'] = 0
test_data.loc[test_data['relatives'] == 0, 'not_alone'] = 1
test_data['not_alone'] = test_data['not_alone'].astype(int)
 test_data.Age = imp.transform(test_data[['Age']] ).ravel()
test_data.Embarked = imp2.transform(test_data[['Embarked']] ).ravel()
test_data = test_data.join(pd.get_dummies(test_data['Sex']))
test_data = test_data.join(pd.get_dummies(test_data['Embarked']))
test_data.shape
holiday_dict = {
0: 6,
1: 3,
2: 2,
3: 8,
4: 3,
5: 3,
6: 2,
7: 8,
8: 4,
9: 8,
10: 5,
11: 4,
}
new_train['holidays_in_month'] = new_train['month'].map(holiday_dict )<categorify>

 f = ['Pclass','Age','SibSp','Parch','Fare','relatives','not_alone','female','C','Q']
len(f )
moex = {
12: 659, 13: 640, 14: 1231,
15: 881, 16: 764, 17: 663,
18: 743, 19: 627, 20: 692,
21: 736, 22: 680, 23: 1092,
24: 657, 25: 863, 26: 720,
27: 819, 28: 574, 29: 568,
30: 633, 31: 658, 32: 611,
33: 770, 34: 723,
}
new_train['moex_value'] = new_train.date_block_num.map(moex)
 new_train = downcast_dtypes(new_train )<set_options>
test_data.Fare.fillna(method = 'ffill',inplace=True )
gc.collect()<filter>
X = data[f]
X_test = test_data[f]
y = data['Survived']
new_train = new_train[new_train.date_block_num > 11]<data_type_conversions>
level1 = LogisticRegression()
def fill_na(df):
for col in df.columns:
if('_lag_' in col)&(df[col].isnull().any()):
df[col].fillna(0, inplace=True)
 return df
new_train = fill_na(new_train )<train_model>
model = StackingClassifier(estimators=level0, final_estimator=level1 )
def xgtrain() :
regressor = xgb.XGBRegressor(n_estimators = 5000,
learning_rate = 0.01,
max_depth = 10,
subsample = 0.5,
colsample_bytree = 0.5)
 regressor_ = regressor.fit(new_train[new_train.date_block_num < 33].drop(['item_cnt_month'], axis=1 ).values,
new_train[new_train.date_block_num < 33]['item_cnt_month'].values,
eval_metric = 'rmse',
eval_set = [(new_train[new_train.date_block_num < 33].drop(['item_cnt_month'], axis=1 ).values,
new_train[new_train.date_block_num < 33]['item_cnt_month'].values), 
(new_train[new_train.date_block_num == 33].drop(['item_cnt_month'], axis=1 ).values,
new_train[new_train.date_block_num == 33]['item_cnt_month'].values)
 ],
verbose=True,
early_stopping_rounds = 50, 
)
 return regressor_<train_model>
model.fit(X,y )
%%time
regressor_ = xgtrain()<predict_on_test>
pred_y = model.predict(X_test )
<save_to_csv><EOS> 
output = pd.DataFrame({'PassengerId': test_data.PassengerId, 'Survived': pred_y})
 output.to_csv('my_submission.csv', index=False)
 print("Submitted successfully!" )