Kaggle competition: Titanic - Machine Learning from Disaster
df_base['target'] = df2['target']* 0.3 + df_base['target'] * 0.7
plt.figure(figsize=(8,8))
plt.subplot(1, 2, 1)
 sns.boxplot(df2['target'],orient='v')
 plt.subplot(1, 2, 2)
 sns.boxplot(df_base['target'], orient='v')
 plt.show() 
<import_modules>
train = pd.read_csv("/kaggle/input/titanic/train.csv")
 test = pd.read_csv("/kaggle/input/titanic/test.csv" )
from scipy.stats import truncnorm<save_to_csv>
train = train.drop(columns= ['Name','Ticket','Cabin'])
 test = test.drop(columns= ['Name','Ticket','Cabin'] )
df_base['target'] = truncnorm.mean(df2['target'],df_base['target'])
 df_base[['card_id','target']].to_csv("blend3.csv",index=False )<set_options>
train['Embarked_S'] =(train['Embarked'] == 'S' ).astype(int)
 train['Embarked_C'] =(train['Embarked'] == 'C' ).astype(int)
 train['Embarked_Q'] =(train['Embarked'] == 'Q' ).astype(int)
 train['Gender'] =(train['Sex'] == 'male' ).astype(int )
pd.set_option('display.max_columns', None)
 print('available GPU devices catboost:', get_gpu_device_count() )<define_variables>
test['Embarked_S'] =(test['Embarked'] == 'S' ).astype(int)
 test['Embarked_C'] =(test['Embarked'] == 'C' ).astype(int)
 test['Embarked_Q'] =(test['Embarked'] == 'Q' ).astype(int)
 test['Gender'] =(test['Sex'] == 'male' ).astype(int )
DATA_DIR = '/kaggle/input/m5-forecasting-accuracy'
MODEL_VER = 'v0'
BACKWARD_LAGS = 60
END_D = 1913
CUT_D = END_D - int(365 * 1.2)
 END_DATE = '2016-04-24'
print(datetime.strptime(END_DATE, '%Y-%m-%d'))<define_variables>
train = train.drop(columns = ['Sex'])
 test = test.drop(columns = ['Sex'] )
CALENDAR_DTYPES = {
'date': 'str',
'wm_yr_wk': 'int16',
'weekday': 'object',
'wday': 'int16',
'month': 'int16',
'year': 'int16',
'd': 'object',
'event_name_1': 'object',
'event_type_1': 'object',
'event_name_2': 'object',
'event_type_2': 'object',
'snap_CA': 'int16',
'snap_TX': 'int16',
'snap_WI': 'int16'
}
PARSE_DATES = ['date']
SPRICES_DTYPES = {
'store_id': 'object',
'item_id': 'object',
'wm_yr_wk': 'int16',
'sell_price': 'float32'
}<load_from_csv>
train = train.drop(columns = ['Embarked'])
 test = test.drop(columns = ['Embarked'] )
def get_df(is_train=True, backward_lags=None):
strain = pd.read_csv('{}/sales_train_validation.csv'.format(DATA_DIR))
print('read train:', strain.shape)
 cat_cols = ['id', 'item_id', 'dept_id','store_id', 'cat_id', 'state_id']
last_day = int(strain.columns[-1].replace('d_', ''))
print('first day is:', CUT_D)
 print('last day is:', last_day)
 if not is_train:
for day in range(last_day + 1, last_day + 28 + 28 + 1):
strain['d_{}'.format(day)] = np.nan
value_vars = [col for col in strain.columns
if(col.startswith('d_')and(int(col.replace('d_', '')) >= END_D - backward_lags)) ]
else:
value_vars = [col for col in strain.columns
if(col.startswith('d_')and(int(col.replace('d_', '')) >= CUT_D)) ]
strain = pd.melt(
 strain,
id_vars = cat_cols,
value_vars = value_vars,
var_name = 'd',
value_name = 'sales' 
)
 print('melted train:', strain.shape)
 calendar = pd.read_csv('{}/calendar.csv'.format(DATA_DIR), dtype=CALENDAR_DTYPES, parse_dates=PARSE_DATES)
 print('read calendar:', calendar.shape)
 strain = strain.merge(calendar, on='d', copy=False)
 del calendar
gc.collect()
print('calendar merge done')
 sprices = pd.read_csv('{}/sell_prices.csv'.format(DATA_DIR), dtype=SPRICES_DTYPES)
 print('read prices:', sprices.shape)
 strain = strain.merge(
 sprices,
on=['store_id', 'item_id', 'wm_yr_wk'],
copy=False 
)
 del sprices
gc.collect()
print('prices merge done')
 print('begin train date:', strain['date'].min())
 print('end train date:', strain['date'].max())
 if not is_train:
strain = strain.loc[
strain['date'] >=(datetime.strptime(END_DATE, '%Y-%m-%d')- timedelta(days=backward_lags))
]
print('date cut train:', strain.shape)
 print('cut train date:', strain['date'].min())
 print('end train date:', strain['date'].max())
 return strain<categorify>
train.fillna(0, inplace=True)
 test.fillna(0, inplace=True )
def make_features(strain):
print('in dataframe:', strain.shape)
 lags = [7, 28]
windows= [7, 28]
wnd_feats = ['id', 'item_id']
lag_cols = ['lag_{}'.format(lag)for lag in lags ]
for lag, lag_col in zip(lags, lag_cols):
strain[lag_col] = strain[['id', 'sales']].groupby('id')['sales'].shift(lag)
 print('lag sales done')
 for wnd_feat in wnd_feats:
for wnd in windows:
for lag_col in lag_cols:
wnd_col = '{}_{}_rmean_{}'.format(lag_col, wnd_feat, wnd)
 strain[wnd_col] = strain[[wnd_feat, lag_col]].groupby(wnd_feat)[lag_col].transform(
 lambda x: x.rolling(wnd ).mean() 
)
 print('rolling mean sales for feature done:', wnd_feat)
 date_features = {
'week_num': 'weekofyear',
'quarter': 'quarter',
'mday': 'day'
}
for date_feat_name, date_feat_func in date_features.items() :
strain[date_feat_name] = getattr(strain['date'].dt, date_feat_func ).astype('int16')
 print('date features done')
 strain['d'] = strain['d'].apply(lambda x: int(x.replace('d_', '')))
 print('out dataframe:', strain.shape)
 return strain<feature_engineering>
X = train.drop(columns = ['Survived'])
 y = train['Survived']
%%time
strain = get_df(is_train=True, backward_lags=None)
 strain = make_features(strain )<feature_engineering>
model = XGBClassifier(learning_rate=0.01, n_estimators=1000)
 model.fit(X,y )
drop_cols = ['id', 'sales', 'date', 'wm_yr_wk', 'weekday']
train_cols = strain.columns[~strain.columns.isin(drop_cols)]
cat_cols = [
'item_id', 'dept_id', 'store_id', 'cat_id', 'state_id',
'event_name_1', 'event_type_1', 'event_name_2', 'event_type_2'
]
strain[cat_cols] = strain[cat_cols].fillna(0 )<define_variables>
pred = model.predict(test )
<train_model><EOS> 
result = pd.DataFrame({'PassengerId':test['PassengerId'], 'Survived':pred})
 result.to_csv("submission.csv", index=False )