Kaggle competition: Titanic - Machine Learning from Disaster
holiday = ['NewYear', 'OrthodoxChristmas', 'MartinLutherKingDay', 'SuperBowl', 'PresidentsDay', 'StPatricksDay', 'Easter', 'Cinco De Mayo', 'IndependenceDay', 'EidAlAdha', 'Thanksgiving', 'Christmas']
weekend = ['Saturday', 'Sunday']
def is_holiday(x):
if x in holiday:
return 1
else:
return 0
def is_weekend(x):
if x in weekend:
return 1
else:
return 0<feature_engineering>
train.isnull().sum()
df_cal['is_holiday_1'] = df_cal['event_name_1'].apply(is_holiday)
 df_cal['is_holiday_2'] = df_cal['event_name_2'].apply(is_holiday)
 df_cal['is_holiday'] = df_cal[['is_holiday_1','is_holiday_2']].max(axis=1)
 df_cal['is_weekend'] = df_cal['weekday'].apply(is_weekend )<drop_column>
train.isnull().sum()
df_cal = df_cal.drop(['weekday', 'wday', 'month', 'year', 'event_name_1', 'event_type_1', 'event_name_2', 'event_type_2'], axis='columns' )<drop_column>
test.isnull().sum()
del_col = []
for x in range(1851):
del_col.append('d_' + str(x+1))<drop_column>
test.isnull().sum()
df_eval = df_eval.drop(del_col, axis='columns' )<merge>
%matplotlib inline
sns.set()
df_eval = pd.merge(df_eval, df_cal, how='left', on='d')
 df_eval.head()<merge>
train_test_data = [train, test]
for dataset in train_test_data:
dataset['Title'] = dataset['Name'].str.extract('([A-Za-z]+)\.', expand=False )
df_eval = pd.merge(df_eval, df_price, how='left', on=['item_id', 'wm_yr_wk', 'store_id'])
 df_eval.head()<filter>
train['Title'].value_counts()
df_eval_test = df_eval.query('d == "d_1852"' )<drop_column>
test['Title'].value_counts()
df_eval_test = df_eval_test[['id', 'store_id', 'item_id', 'dept_id', 'cat_id', 'state_id', 'd', 'qty', 'sell_price']]<feature_engineering>
title_mapping = {"Mr": 0, "Miss": 1, "Mrs": 2,
"Master": 3, "Dr": 3, "Rev": 3, "Col": 3, "Major": 3, "Mlle": 3,"Countess": 3,
"Ms": 3, "Lady": 3, "Jonkheer": 3, "Don": 3, "Dona" : 3, "Mme": 3,"Capt": 3,"Sir": 3 }
for dataset in train_test_data:
dataset['Title'] = dataset['Title'].map(title_mapping )
df_eval_test['qty'] = df_eval_test['d'].apply(lambda x: int(x.replace(x, '0')) )<define_variables>
train.drop('Name', axis=1, inplace=True)
 test.drop('Name', axis=1, inplace=True )
tmp_df = df_eval_test<concatenate>
sex_mapping = {"male": 0, "female": 1}
for dataset in train_test_data:
dataset['Sex'] = dataset['Sex'].map(sex_mapping )
for x in range(28):
df_eval_test = df_eval_test.append(tmp_df )<drop_column>
train["Age"].fillna(train.groupby("Title")["Age"].transform("median"), inplace=True)
 test["Age"].fillna(test.groupby("Title")["Age"].transform("median"), inplace=True )
df_eval_test = df_eval_test.reset_index(drop=True )<define_variables>
for dataset in train_test_data:
dataset.loc[ dataset['Age'] <= 16, 'Age'] = 0,
dataset.loc[(dataset['Age'] > 16)&(dataset['Age'] <= 26), 'Age'] = 1,
dataset.loc[(dataset['Age'] > 26)&(dataset['Age'] <= 36), 'Age'] = 2,
dataset.loc[(dataset['Age'] > 36)&(dataset['Age'] <= 62), 'Age'] = 3,
dataset.loc[ dataset['Age'] > 62, 'Age'] = 4
lst_d = []
i = 0
lst_index = df_eval_test.index
for x in lst_index:
lst_d.append('d_' + str(((lst_index[i])// 30490)+ 1942))
i = i + 1
lst_d<feature_engineering>
Pclass1 = train[train['Pclass']==1]['Embarked'].value_counts()
Pclass2 = train[train['Pclass']==2]['Embarked'].value_counts()
Pclass3 = train[train['Pclass']==3]['Embarked'].value_counts()
df = pd.DataFrame([Pclass1, Pclass2, Pclass3])
 df.index = ['1st class','2nd class', '3rd class']
df.plot(kind='bar',stacked=True, figsize=(10,5))
df_eval_test['d'] = lst_d<merge>
for dataset in train_test_data:
dataset['Embarked'] = dataset['Embarked'].fillna('S' )
df_eval_test = pd.merge(df_eval_test, df_cal, how='left', on='d' )<merge>
embarked_mapping = {"S": 0, "C": 1, "Q": 2}
for dataset in train_test_data:
dataset['Embarked'] = dataset['Embarked'].map(embarked_mapping )
df_eval_test = pd.merge(df_eval_test, df_price, how='left', on=['item_id', 'wm_yr_wk', 'store_id'] )<set_options>
train["Fare"].fillna(train.groupby("Pclass")["Fare"].transform("median"), inplace=True)
 test["Fare"].fillna(test.groupby("Pclass")["Fare"].transform("median"), inplace=True)
 train.head(50 )
del tmp_df
gc.collect()<categorify>
for dataset in train_test_data:
dataset.loc[ dataset['Fare'] <= 17, 'Fare'] = 0,
dataset.loc[(dataset['Fare'] > 17)&(dataset['Fare'] <= 30), 'Fare'] = 1,
dataset.loc[(dataset['Fare'] > 30)&(dataset['Fare'] <= 100), 'Fare'] = 2,
dataset.loc[ dataset['Fare'] > 100, 'Fare'] = 3
df_eval = pd.get_dummies(data=df_eval, columns=['dept_id', 'cat_id', 'store_id', 'state_id'])
 df_eval_test = pd.get_dummies(data=df_eval_test, columns=['dept_id', 'cat_id', 'store_id', 'state_id'] )<drop_column>
train.Cabin.value_counts()
df_eval_test = df_eval_test.drop(['sell_price_x', 'snap_CA', 'snap_TX', 'snap_WI'], axis='columns')
 df_eval_test = df_eval_test.rename(columns={'sell_price_y': 'sell_price'})
 df_eval = df_eval.drop(['snap_CA', 'snap_TX', 'snap_WI'], axis='columns' )<split>
for dataset in train_test_data:
dataset['Cabin'] = dataset['Cabin'].str[:1]
target_col = 'qty'
exclude_cols = ['id', 'item_id', 'd', 'date', 'wm_yr_wk']
feature_cols = [col for col in df_eval.columns if col not in exclude_cols]
y = np.array(df_eval[target_col])
 X = np.array(df_eval[feature_cols])
 X_train, X_test, y_train, y_test = \
train_test_split(X, y, test_size=0.3, random_state=1234)
<train_model>
cabin_mapping = {"A": 0, "B": 0.4, "C": 0.8, "D": 1.2, "E": 1.6, "F": 2, "G": 2.4, "T": 2.8}
for dataset in train_test_data:
dataset['Cabin'] = dataset['Cabin'].map(cabin_mapping )
lgb_train = lgb.Dataset(X_train, y_train)
 lgb_eval = lgb.Dataset(X_test, y_test)
 params = {
'boosting_type': 'gbdt',
'metric': 'rmse',
'objective': 'regression',
'n_jobs': -1,
'seed': 236,
'learning_rate': 0.01,
'bagging_fraction': 0.75,
'bagging_freq': 10,
'colsample_bytree': 0.75}
model = lgb.train(params, lgb_train, num_boost_round=2500, early_stopping_rounds=50, valid_sets = [lgb_train, lgb_eval], verbose_eval=100 )<predict_on_test>
train["Cabin"].fillna(train.groupby("Pclass")["Cabin"].transform("median"), inplace=True)
 test["Cabin"].fillna(test.groupby("Pclass")["Cabin"].transform("median"), inplace=True )
pred = model.predict(df_eval_test[feature_cols] )<prepare_output>
train["FamilySize"] = train["SibSp"] + train["Parch"] + 1
test["FamilySize"] = test["SibSp"] + test["Parch"] + 1
df_eval_test['pred_qty'] = pred<prepare_output>
family_mapping = {1: 0, 2: 0.4, 3: 0.8, 4: 1.2, 5: 1.6, 6: 2, 7: 2.4, 8: 2.8, 9: 3.2, 10: 3.6, 11: 4}
for dataset in train_test_data:
dataset['FamilySize'] = dataset['FamilySize'].map(family_mapping )
predictions = df_eval_test[['id', 'date', 'pred_qty']]
predictions = pd.pivot(predictions, index = 'id', columns = 'date', values = 'pred_qty' ).reset_index()
predictions<drop_column>
features_drop = ['Ticket', 'SibSp', 'Parch']
train = train.drop(features_drop, axis=1)
 test = test.drop(features_drop, axis=1)
 train = train.drop(['PassengerId'], axis=1 )
predictions = predictions.drop(predictions.columns[1], axis=1)
 predictions<rename_columns>
from sklearn.neighbors import KNeighborsClassifier
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.naive_bayes import GaussianNB
from sklearn.svm import SVC
import numpy as np
predictions.columns = ['id'] + ['F' + str(i + 1)for i in range(28)]
predictions<prepare_x_and_y>
k_fold = KFold(n_splits=10, shuffle=True, random_state=0 )
x = 2744099 + 1 - 853720
df_val = df_eval[x:]<prepare_output>
clf = KNeighborsClassifier(n_neighbors = 13)
 scoring = 'accuracy'
score = cross_val_score(clf, train_data, target, cv=k_fold, n_jobs=1, scoring=scoring)
 print(score )
predictions_v = df_val[['id', 'date', 'qty']]
predictions_v = pd.pivot(predictions_v, index = 'id', columns = 'date', values = 'qty' ).reset_index()
predictions_v<feature_engineering>
round(np.mean(score)*100, 2 )
predictions_v['id'] = predictions['id'].apply(lambda x: x.replace('evaluation', 'validation'))
predictions_v.head()<rename_columns>
clf = DecisionTreeClassifier()
scoring = 'accuracy'
score = cross_val_score(clf, train_data, target, cv=k_fold, n_jobs=1, scoring=scoring)
 print(score )
predictions_v.columns = ['id'] + ['F' + str(i + 1)for i in range(28)]
predictions_v.head()<concatenate>
round(np.mean(score)*100, 2 )
predictions_concat = pd.concat([predictions, predictions_v], axis=0 )<save_to_csv>
clf = RandomForestClassifier(n_estimators=13)
 scoring = 'accuracy'
score = cross_val_score(clf, train_data, target, cv=k_fold, n_jobs=1, scoring=scoring)
 print(score )
predictions_concat.to_csv('submission.csv', index=False )<load_from_csv>
round(np.mean(score)*100, 2 )
submission0 = pd.read_csv('/kaggle/input/m5-final-models/submission_LSTM.csv')
 submission1 = pd.read_csv('/kaggle/input/m5-final-models/submission_XGBoost.csv')
 submission2 = pd.read_csv('/kaggle/input/m5-final-models/submission_LGBM.csv')
 submission3 = pd.read_csv('/kaggle/input/m5-final-models/submission_prophet.csv')
 submission4 = pd.read_csv('/kaggle/input/m5-final-models/submission_SARIMAX.csv' )<load_from_csv>
clf = GaussianNB()
scoring = 'accuracy'
score = cross_val_score(clf, train_data, target, cv=k_fold, n_jobs=1, scoring=scoring)
 print(score )
<load_from_csv>
round(np.mean(score)*100, 2 )
calendar = pd.read_csv('/kaggle/input/m5-forecasting-accuracy/calendar.csv')
 prices = pd.read_csv('/kaggle/input/m5-forecasting-accuracy/sell_prices.csv')
 validation = pd.read_csv('/kaggle/input/m5-forecasting-accuracy/sales_train_evaluation.csv')
 sample_sub = pd.read_csv('/kaggle/input/m5-forecasting-accuracy/sample_submission.csv' )<merge>
clf = SVC()
scoring = 'accuracy'
score = cross_val_score(clf, train_data, target, cv=k_fold, n_jobs=1, scoring=scoring)
 print(score )
def perfect_sub() :
submission = OperateBaseModels(submission0,submission1, a=0, b=1)
 diference = validation.merge(submission, how='right')
 shift= 56
perfect_submission = diference[diference.columns[-shift:-shift+28]]
col = { 'd_'+str(1914+i):'F'+str(i+1)for i in range(28)}
perfect_submission = perfect_submission.rename(columns=col)
 perfect_submission.insert(loc=0, column='id', value= submission0['id'])
 perfect_submission = perfect_submission.fillna(0)
 return perfect_submission
def OperateBaseModels(ModelA,ModelB, a,b):
submissionMerge = pd.merge(ModelA, ModelB, on='id', how='left', suffixes=('_x', '_y'))
submissionMerge
submission = pd.DataFrame()
submission.insert(loc=0, column='id', value= ModelB['id'])
 for j in range(28):
i =j+1
submission.insert(loc=i, column='F'+str(i), value=(( submissionMerge[submissionMerge.columns[i]]*a)+(submissionMerge[submissionMerge.columns[i+28]])*b)/(a+b))
return submission 
<feature_engineering>
round(np.mean(score)*100,2 )
class WRMSSEEvaluator(object):
def __init__(self, train_df: pd.DataFrame, valid_df: pd.DataFrame, calendar: pd.DataFrame, prices: pd.DataFrame):
train_y = train_df.loc[:, train_df.columns.str.startswith('d_')]
train_target_columns = train_y.columns.tolist()
weight_columns = train_y.iloc[:, -28:].columns.tolist()
train_df['all_id'] = 0
id_columns = train_df.loc[:, ~train_df.columns.str.startswith('d_')].columns.tolist()
valid_target_columns = valid_df.loc[:, valid_df.columns.str.startswith('d_')].columns.tolist()
if not all([c in valid_df.columns for c in id_columns]):
valid_df = pd.concat([train_df[id_columns], valid_df], axis=1, sort=False)
 self.train_df = train_df
self.valid_df = valid_df
self.calendar = calendar
self.prices = prices
self.weight_columns = weight_columns
self.id_columns = id_columns
self.valid_target_columns = valid_target_columns
weight_df = self.get_weight_df()
self.group_ids =(
 'all_id',
'state_id',
'store_id',
'cat_id',
'dept_id',
['state_id', 'cat_id'],
['state_id', 'dept_id'],
['store_id', 'cat_id'],
['store_id', 'dept_id'],
'item_id',
['item_id', 'state_id'],
['item_id', 'store_id'] 
)
 for i, group_id in enumerate(tqdm(self.group_ids)) :
train_y = train_df.groupby(group_id)[train_target_columns].sum()
scale = []
for _, row in train_y.iterrows() :
series = row.values[np.argmax(row.values != 0):]
scale.append(((series[1:] - series[:-1])** 2 ).mean())
 setattr(self, f'lv{i + 1}_scale', np.array(scale))
setattr(self, f'lv{i + 1}_train_df', train_y)
 setattr(self, f'lv{i + 1}_valid_df', valid_df.groupby(group_id)[valid_target_columns].sum())
 lv_weight = weight_df.groupby(group_id)[weight_columns].sum().sum(axis=1)
 setattr(self, f'lv{i + 1}_weight', lv_weight / lv_weight.sum())
 def get_weight_df(self)-> pd.DataFrame:
day_to_week = self.calendar.set_index('d')['wm_yr_wk'].to_dict()
weight_df = self.train_df[['item_id', 'store_id'] + self.weight_columns].set_index(['item_id', 'store_id'])
 weight_df = weight_df.stack().reset_index().rename(columns={'level_2': 'd', 0: 'value'})
 weight_df['wm_yr_wk'] = weight_df['d'].map(day_to_week)
 weight_df = weight_df.merge(self.prices, how='left', on=['item_id', 'store_id', 'wm_yr_wk'])
 weight_df['value'] = weight_df['value'] * weight_df['sell_price']
weight_df = weight_df.set_index(['item_id', 'store_id', 'd'] ).unstack(level=2)['value']
weight_df = weight_df.loc[zip(self.train_df.item_id, self.train_df.store_id), :].reset_index(drop=True)
 weight_df = pd.concat([self.train_df[self.id_columns], weight_df], axis=1, sort=False)
 return weight_df
def rmsse(self, valid_preds: pd.DataFrame, lv: int)-> pd.Series:
valid_y = getattr(self, f'lv{lv}_valid_df')
 score =(( valid_y - valid_preds)** 2 ).mean(axis=1)
 scale = getattr(self, f'lv{lv}_scale')
 return(score / scale ).map(np.sqrt)
 def score(self, valid_preds: Union[pd.DataFrame, np.ndarray])-> float:
assert self.valid_df[self.valid_target_columns].shape == valid_preds.shape
if isinstance(valid_preds, np.ndarray):
valid_preds = pd.DataFrame(valid_preds, columns=self.valid_target_columns)
 valid_preds = pd.concat([self.valid_df[self.id_columns], valid_preds], axis=1, sort=False)
 all_scores = []
for i, group_id in enumerate(self.group_ids):
lv_scores = self.rmsse(valid_preds.groupby(group_id)[self.valid_target_columns].sum() , i + 1)
 weight = getattr(self, f'lv{i + 1}_weight')
 lv_scores = pd.concat([weight, lv_scores], axis=1, sort=False ).prod(axis=1)
 all_scores.append(lv_scores.sum())
 return np.mean(all_scores)
 df_train_full = pd.read_csv(".. /input/m5-forecasting-accuracy/sales_train_evaluation.csv")
 df_calendar = pd.read_csv(".. /input/m5-forecasting-accuracy/calendar.csv")
 df_prices = pd.read_csv(".. /input/m5-forecasting-accuracy/sell_prices.csv")
 df_train = df_train_full.iloc[:, :-28]
df_valid = df_train_full.iloc[:, -28:]
evaluator = WRMSSEEvaluator(df_train, df_valid, df_calendar, df_prices)
 def WRMSSEE(sub):
val_preds = sample_sub[['id']].merge(sub, on = 'id')
 val_preds.columns = ['id'] + list(df_valid.columns)
 valid_preds = val_preds.iloc[:30490, -28:]
return evaluator.score(valid_preds )<categorify>
clf = SVC()
clf.fit(train_data, target)
 test_data = test.drop("PassengerId", axis=1 ).copy()
prediction = clf.predict(test_data )
<merge><EOS> 
submission = pd.DataFrame({
"PassengerId": test["PassengerId"],
"Survived": prediction
})
 submission.to_csv('submission.csv', index=False )