Kaggle competition: Titanic - Machine Learning from Disaster
<SOS> metric: categorizationaccuracy Kaggle data source: titanic-machine-learning-from-disaster<data_type_conversions>
%matplotlib inline
warnings.filterwarnings('ignore')
 pd.set_option('display.max_columns', None)
<merge>
train_data = pd.read_csv("/kaggle/input/titanic/train.csv")
 test_data = pd.read_csv("/kaggle/input/titanic/test.csv" )
ts = time.time()
group = matrix.groupby(['date_block_num','shop_id' ,'item_id'] ).agg({'item_cnt_month': ['mean']})
 group.columns = [ 'date_shop_item_avg_item_cnt' ]
group.reset_index(inplace=True)
 matrix = pd.merge(matrix, group, on=['date_block_num','shop_id' ,'item_id'], how='left')
 matrix['date_shop_item_avg_item_cnt'] = matrix['date_shop_item_avg_item_cnt'].astype(np.float16)
 matrix = lag_feature(matrix, [1], 'date_shop_item_avg_item_cnt')
 matrix.drop(['date_shop_item_avg_item_cnt'], axis=1, inplace=True)
 time.time() - ts<data_type_conversions>
train = train_data.copy()
test = test_data.copy()
ts = time.time()
group = train.groupby(['item_id'] ).agg({'item_price': ['mean']})
 group.columns = ['item_avg_item_price']
group.reset_index(inplace=True)
 matrix = pd.merge(matrix, group, on=['item_id'], how='left')
 matrix['item_avg_item_price'] = matrix['item_avg_item_price'].astype(np.float16)
 group = train.groupby(['date_block_num','item_id'] ).agg({'item_price': ['mean']})
 group.columns = ['date_item_avg_item_price']
group.reset_index(inplace=True)
 matrix = pd.merge(matrix, group, on=['date_block_num','item_id'], how='left')
 matrix['date_item_avg_item_price'] = matrix['date_item_avg_item_price'].astype(np.float16)
 lags = [1,2,3]
matrix = lag_feature(matrix, lags, 'date_item_avg_item_price')
 for i in lags:
matrix['delta_price_lag_'+str(i)] = \ 
(matrix['date_item_avg_item_price_lag_'+str(i)] - matrix['item_avg_item_price'])/ matrix['item_avg_item_price']
def select_trend(row):
for i in lags:
if row['delta_price_lag_'+str(i)]:
return row['delta_price_lag_'+str(i)]
return 0
matrix['delta_price_lag'] = matrix.apply(select_trend, axis=1)
 matrix['delta_price_lag'] = matrix['delta_price_lag'].astype(np.float16)
 matrix['delta_price_lag'].fillna(0, inplace=True)
 fetures_to_drop = ['item_avg_item_price', 'date_item_avg_item_price']
for i in lags:
fetures_to_drop += ['date_item_avg_item_price_lag_'+str(i)]
fetures_to_drop += ['delta_price_lag_'+str(i)]
matrix.drop(fetures_to_drop, axis=1, inplace=True)
 time.time() - ts<merge>
100*train.isnull().sum() /len(train )
ts = time.time()
group = train.groupby(['date_block_num','shop_id'] ).agg({'revenue': ['sum']})
 group.columns = ['date_shop_revenue']
group.reset_index(inplace=True)
 matrix = pd.merge(matrix, group, on=['date_block_num','shop_id'], how='left')
 matrix['date_shop_revenue'] = matrix['date_shop_revenue'].astype(np.float32)
 group = group.groupby(['shop_id'] ).agg({'date_shop_revenue': ['mean']})
 group.columns = ['shop_avg_revenue']
group.reset_index(inplace=True)
 matrix = pd.merge(matrix, group, on=['shop_id'], how='left')
 matrix['shop_avg_revenue'] = matrix['shop_avg_revenue'].astype(np.float32)
 matrix['delta_revenue'] =(matrix['date_shop_revenue'] - matrix['shop_avg_revenue'])/ matrix['shop_avg_revenue']
matrix['delta_revenue'] = matrix['delta_revenue'].astype(np.float16)
 matrix = lag_feature(matrix, [1], 'delta_revenue')
 matrix.drop(['date_shop_revenue','shop_avg_revenue','delta_revenue'], axis=1, inplace=True)
 time.time() - ts<categorify>
train['Pclass'].value_counts()
matrix['month'] = matrix['date_block_num'] % 12
days = pd.Series([31,28,31,30,31,30,31,31,30,31,30,31])
 matrix['days'] = matrix['month'].map(days ).astype(np.int8 )<feature_engineering>
train['Sex'].value_counts()
<feature_engineering>
train['SibSp'].value_counts()
<feature_engineering>
train['Parch'].value_counts()
ts = time.time()
matrix['item_shop_first_sale'] = matrix['date_block_num'] - matrix.groupby(['item_id','shop_id'])['date_block_num'].transform('min')
 matrix['item_first_sale'] = matrix['date_block_num'] - matrix.groupby('item_id')['date_block_num'].transform('min')
 time.time() - ts<feature_engineering>
train['Ticket'].value_counts()
ts = time.time()
matrix = matrix[matrix.date_block_num > 3]
time.time() - ts<correct_missing_values>
train['Cabin'].value_counts()
ts = time.time()
def fill_na(df):
for col in df.columns:
if('_lag_' in col)&(df[col].isnull().any()):
if('item_cnt' in col):
df[col].fillna(0, inplace=True)
 return df
matrix = fill_na(matrix)
 time.time() - ts<load_pretrained>
train['Embarked'].value_counts()
matrix.to_pickle('.. /working/data.pkl')
 del matrix
gc.collect() ;<set_options>
train = train.drop(['Cabin'], axis = 1)
 test = test.drop(['Cabin'], axis = 1)
 train.head()
rcParams['figure.figsize'] = 12, 4<load_from_csv>
train.isnull().sum()
data = pd.read_pickle('.. /working/data.pkl')
 test = pd.read_csv('.. /input/competitive-data-science-predict-future-sales/test.csv' ).set_index('ID')
 print(len(data.columns))
data.columns<create_dataframe>
train.isnull().sum()
data = data[[
'date_block_num', 'shop_id', 'item_id', 'item_cnt_month', 'shop_city',
'shop_category', 'item_category_id', 'name_2', 'name_3', 'type_code',
'subtype_code', 'item_cnt_month_lag_1', 'item_cnt_month_lag_2',
'item_cnt_month_lag_3', 'date_avg_item_cnt_lag_1',
'date_item_avg_item_cnt_lag_1', 'date_item_avg_item_cnt_lag_2',
'date_item_avg_item_cnt_lag_3', 'date_shop_avg_item_cnt_lag_1',
'date_shop_avg_item_cnt_lag_2', 'date_shop_avg_item_cnt_lag_3',
'date_cat_avg_item_cnt_lag_1', 'date_shop_cat_avg_item_cnt_lag_1',
'date_shop_subtype_avg_item_cnt_lag_1', 'date_city_avg_item_cnt_lag_1',
'date_item_city_avg_item_cnt_lag_1',
'date_shop_item_avg_item_cnt_lag_1',
'delta_price_lag',
'delta_revenue_lag_1', 'month', 'days',
'item_shop_first_sale',
'item_first_sale'
]]
len(data.columns )<prepare_x_and_y>
test.isnull().sum()
X_train = data[data.date_block_num < 33].drop(['item_cnt_month'], axis=1)
 Y_train = data[data.date_block_num < 33]['item_cnt_month']
X_valid = data[data.date_block_num == 33].drop(['item_cnt_month'], axis=1)
 Y_valid = data[data.date_block_num == 33]['item_cnt_month']
X_test = data[data.date_block_num == 34].drop(['item_cnt_month'], axis=1 )<set_options>
test.isnull().sum()
del data
gc.collect() ;<train_model>
train = train.drop(['Ticket'], axis = 1)
 test = test.drop(['Ticket'], axis = 1)
 train.head()
ts = time.time()
model = XGBRegressor(
 max_depth=10,
n_estimators=1000,
min_child_weight=0.5,
colsample_bytree=0.8,
subsample=0.8,
eta=0.1,
tree_method='gpu_hist',
seed=42)
 model.fit(
 X_train,
Y_train,
eval_metric="rmse",
eval_set=[(X_train, Y_train),(X_valid, Y_valid)],
verbose=True,
early_stopping_rounds = 20)
 time.time() - ts<save_to_csv>
Q1 = train['Fare'].quantile(0.05)
 Q3 = train['Fare'].quantile(0.95)
 IQR = Q3 - Q1
lower_limit = Q1- 1.5*IQR
lower_limit
upper_limit = Q3 + 1.5*IQR
upper_limit
Y_pred = model.predict(X_valid ).clip(0, 20)
 Y_test = model.predict(X_test ).clip(0, 20)
 submission = pd.DataFrame({
"ID": test.index,
"item_cnt_month": Y_test
})
 submission.to_csv('xgb_submission.csv', index=False)
 pickle.dump(Y_pred, open('xgb_train.pickle', 'wb'))
pickle.dump(Y_test, open('xgb_test.pickle', 'wb'))<import_modules>
train['Fare'] >(upper_limit )
import numpy as np
import pandas as pd
from xgboost import XGBRegressor
import seaborn as sns
import matplotlib.pyplot as plt
from sklearn.preprocessing import LabelEncoder
from sklearn.model_selection import RandomizedSearchCV,cross_val_score
from sklearn.metrics import mean_squared_error
from sklearn.metrics import make_scorer
from itertools import product
import re
import nltk
from nltk import word_tokenize
from nltk.corpus import stopwords 
<install_modules>
train['Fare'] = train['Fare'].replace(512.3292, 270 )
!pip install pymorphy2
!pip install pymorphy2-dicts
!pip install DAWG-Python<import_modules>
train.sort_values("Fare", ascending=False )
import pymorphy2<load_from_csv>
test.sort_values("Fare", ascending=False )
sample_submissions=pd.read_csv(".. /input/competitive-data-science-predict-future-sales/sample_submission.csv")
 sales_train_df=pd.read_csv(".. /input/competitive-data-science-predict-future-sales/sales_train.csv")
 test_df=pd.read_csv(".. /input/competitive-data-science-predict-future-sales/test.csv")
 items_df=pd.read_csv(".. /input/competitive-data-science-predict-future-sales/items.csv")
 shops_df=pd.read_csv(".. /input/competitive-data-science-predict-future-sales/shops.csv")
 item_categories_df=pd.read_csv(".. /input/competitive-data-science-predict-future-sales/item_categories.csv" )<count_unique_values>
test['Fare'] = test['Fare'].replace(512.3292, 270 )
print(sales_train_df["shop_id"].nunique())
 print(test_df["shop_id"].nunique() )<count_unique_values>
test.sort_values("Fare", ascending=False )
print(sales_train_df["item_id"].nunique())
 print(test_df["item_id"].nunique() )<load_pretrained>
train = train.drop(['Name'], axis = 1)
 test = test.drop(['Name'], axis = 1 )
nltk.download("stopwords" )<string_transform>
train.isnull().sum()
def text_preprocessing(text):
txt=""
morph=pymorphy2.MorphAnalyzer()
stop_words_ru=stopwords.words("russian")
 stop_words_eng=stopwords.words("english")
 stroka=re.sub('[^A-Za-zА-Яа-я]+',' ',text)
 strs=stroka.split(" ")
 filtered_strs=[morph.parse(w.lower())[0].normal_form for w in strs if(w not in stop_words_ru and w not in stop_words_eng)]
txt=" ".join(filtered_strs)
 return txt<feature_engineering>
train.isnull().sum()
<merge>
train["Age"] = train["Age"].fillna(train["Age"].median() )
sales_train_df=sales_train_df.join(shops_df.set_index("shop_id"),on="shop_id",how="left" ).join(
 items_df.set_index("item_id"),on="item_id",how="left" ).join(item_categories_df.set_index("item_category_id")
 ,on="item_category_id",how="left")
 sales_train_df.head().T<merge>
test["Age"] = test["Age"].fillna(test["Age"].median() )
test_df=test_df.join(shops_df.set_index("shop_id"),on="shop_id",how="left" ).join(
 items_df.set_index("item_id"),on="item_id",how="left" ).join(item_categories_df.set_index("item_category_id")
 ,on="item_category_id",how="left")
 test_df.head().T<filter>
train.isnull().sum()
sales_train_df[sales_train_df.item_cnt_day>2000]<filter>
train.isnull().sum()
sales_train_df[sales_train_df.item_category_id==9]["item_cnt_day"].mean()<filter>
test.isnull().sum()
sales_train_df[(sales_train_df.shop_id==12)&(sales_train_df.item_id==11373)]["item_cnt_day"].mean()<filter>
test.isnull().sum()
sales_train_df=sales_train_df[~sales_train_df.isin(sales_train_df[sales_train_df.item_cnt_day>2000])]<filter>
train.isnull().sum()
sales_train_df[sales_train_df.item_price>50000]<filter>
train.isnull().sum()
sales_train_df=sales_train_df[~sales_train_df.isin(sales_train_df[sales_train_df.item_price>50000])]<filter>
test.isnull().sum()
sales_train_df[sales_train_df.item_cnt_day>800]<filter>
test.isnull().sum()
sales_train_df[(sales_train_df.shop_id==12)&(sales_train_df.item_id==20949)]["item_cnt_day"].mean()<filter>
train["Embarked"].value_counts()
sales_train_df=sales_train_df[~sales_train_df.isin(sales_train_df[sales_train_df.item_cnt_day>800])]<filter>
train = train.fillna({"Embarked": "S"} )
test_unique_items=test_df[~test_df["item_id"].isin(item_id_uniq)]
test_unique_items.head()<count_unique_values>
test = test.fillna({"Embarked": "S"} )
test_unique_items["item_id"].nunique()<data_type_conversions>
train.isnull().sum()
%%time
train=[]
col=["date_block_num","shop_id","item_id"]
for i in range(34):
sales=sales_train_df[sales_train_df.date_block_num==i]
train.append(np.array(list(product([i],sales.shop_id.unique() ,sales.item_id.unique())) ,dtype="int16"))
train=pd.DataFrame(np.vstack(train),columns=col)
 train["date_block_num"]=train["date_block_num"].astype(np.int8)
 train["shop_id"]=train["shop_id"].astype(np.int8)
 train["item_id"]=train["item_id"].astype(np.int16)
 print(train.head() )<groupby>
train.isnull().sum()
month_df=sales_train_df[["date_block_num","shop_id","item_id","item_cnt_day"]].groupby(["date_block_num","shop_id","item_id"] ).sum()
month_df.head()<feature_engineering>
test.isnull().sum()
date_block_total["year"]=date_block_total.date_block_num.apply(lambda x:(( x//12)+2013))
date_block_total["month"]=date_block_total.date_block_num.apply(lambda x:(x%12))
date_block_total.head()<data_type_conversions>
test.isnull().sum()
train["year"]=train.date_block_num.apply(lambda x:(( x//12)+2013))
train["month"]=train.date_block_num.apply(lambda x:(x%12))
train["year"]=train["year"].astype(np.int16)
 train["month"]=train["month"].astype(np.int8)
 train.head()<data_type_conversions>
test[test["Fare"].isnull() ]
test_df=pd.read_csv(".. /input/competitive-data-science-predict-future-sales/test.csv")
 test_df["date_block_num"]=34
test_df["year"]=(( 34//12)+2013)
 test_df["month"]=(34%12)
 test_df["date_block_num"]=test_df["date_block_num"].astype(np.int8)
 test_df["shop_id"]=test_df["shop_id"].astype(np.int8)
 test_df["item_id"]=test_df["item_id"].astype(np.int16)
 test_df["year"]=test_df["year"].astype(np.int16)
 test_df["month"]=test_df["month"].astype(np.int8)
 test_df.head()<define_variables>
test[["Pclass","Fare"]].groupby("Pclass" ).mean()
categorical=["year","month"]<merge>
test["Fare"] = test["Fare"].fillna(12 )
train=train.join(month_df,on=["date_block_num","shop_id","item_id"],how="left")
 train["item_cnt_day"]=train["item_cnt_day"].fillna(0 ).clip(0,20 ).astype(np.float16)
 train.head()<concatenate>
test["Fare"].isnull().sum()
train=pd.concat([train,test_df],ignore_index=True,sort=False,keys=col)
 train.fillna(0,inplace=True)
 train.head()<merge>
sex_mapping = {"male": 0, "female": 1}
train['Sex'] = train['Sex'].map(sex_mapping)
 test['Sex'] = test['Sex'].map(sex_mapping)
 train.head()
def create_lag_features(df,feature):
numeric_features=[]
for i in range(1,7):
lagged=df.copy()
lagged=lagged[["date_block_num","shop_id","item_id",feature]]
lagged.columns=["date_block_num","shop_id","item_id",feature+"_lag_"+str(i)]
numeric_features.append(feature+"_lag_"+str(i))
lagged["date_block_num"]+=i
df=df.join(lagged.set_index(["date_block_num","shop_id","item_id"]),on=["date_block_num","shop_id","item_id"],how="left")
 return df,numeric_features<feature_engineering>
lbe = preprocessing.LabelEncoder()
train["Embarked"] = lbe.fit_transform(train["Embarked"])
 test["Embarked"] = lbe.fit_transform(test["Embarked"])
 train.head()
%%time
train,n_col=create_lag_features(train,"item_cnt_day")
 train=train.fillna(0)
 numeric=n_col<groupby>
age_mapping = {'Baby': 1, 'Child': 2, 'Teenager': 3, 'Student': 4, 'Young Adult': 5, 'Adult': 6, 'Senior': 7}
train['AgeGroup'] = train['AgeGroup'].map(age_mapping)
 test['AgeGroup'] = test['AgeGroup'].map(age_mapping )
total_cnt_for_items=sales_train_df[["date_block_num","item_id","item_cnt_day"]].groupby(["date_block_num","item_id"] ).sum()
total_cnt_for_items.columns=["total_cnt_for_items"]
total_cnt_for_items.head()<merge>
train = train.drop(['Age'], axis = 1)
 test = test.drop(['Age'], axis = 1 )
train=train.join(total_cnt_for_items,on=["date_block_num","item_id"],how="left")
 train.fillna(0,inplace=True)
 train["total_cnt_for_items"]=train["total_cnt_for_items"].astype(np.float16)
 train.head().T<drop_column>
train = train.drop(['Fare'], axis = 1)
 test = test.drop(['Fare'], axis = 1 )
%%time
train,n_col=create_lag_features(train,"total_cnt_for_items")
 train=train.fillna(0)
 train=train.drop(["total_cnt_for_items"],axis=1)
 numeric=numeric+n_col<groupby>
train["FamilySize"] = train_data["SibSp"] + train_data["Parch"] + 1
total_cnt_for_shops=sales_train_df[["date_block_num","shop_id","item_cnt_day"]].groupby(["date_block_num","shop_id"] ).sum()
total_cnt_for_shops.columns=["total_cnt_for_shops"]
total_cnt_for_shops.head()<merge>
test["FamilySize"] = test_data["SibSp"] + test_data["Parch"] + 1
sum_cnt_shops=total_cnt_for_shops.reset_index()
cnt_shops=sum_cnt_shops[["shop_id","total_cnt_for_shops"]].groupby("shop_id",as_index=False ).mean()
cnt_shops=cnt_shops.join(shops_df.set_index(["shop_id"]),on="shop_id",how="left")
 cnt_shops.head()<feature_engineering>
train['Single'] = train['FamilySize'].map(lambda s: 1 if s == 1 else 0)
 train['SmallFam'] = train['FamilySize'].map(lambda s: 1 if s == 2 else 0)
 train['MedFam'] = train['FamilySize'].map(lambda s: 1 if 3 <= s <= 4 else 0)
 train['LargeFam'] = train['FamilySize'].map(lambda s: 1 if s >= 5 else 0 )
cnt_shops.loc[cnt_shops.shop_name == 'Сергиев Посад ТЦ "7Я"', 'shop_name'] = 'СергиевПосад ТЦ "7Я"'
cnt_shops['shop_city'] = cnt_shops['shop_name'].str.split(' ' ).map(lambda x: x[0])
 cnt_shops.loc[cnt_shops.shop_city == '!Якутск', 'shop_city'] = 'Якутск'
cnt_shops['shop_type'] = cnt_shops['shop_name'].str.split(' ' ).map(lambda x: x[1])
 cnt_shops.head() 
<merge>
test['Single'] = test['FamilySize'].map(lambda s: 1 if s == 1 else 0)
 test['SmallFam'] = test['FamilySize'].map(lambda s: 1 if s == 2 else 0)
 test['MedFam'] = test['FamilySize'].map(lambda s: 1 if 3 <= s <= 4 else 0)
 test['LargeFam'] = test['FamilySize'].map(lambda s: 1 if s >= 5 else 0 )
train=train.join(cnt_shops[["shop_id","shop_city","shop_type"]].set_index(["shop_id"]),on="shop_id",how="left")
 train.fillna(0,inplace=True)
 train["shop_city"]=train["shop_city"].astype(str)
 train["shop_type"]=train["shop_type"].astype(str)
 train.head().T<define_variables>
train = pd.get_dummies(train, columns = ["Embarked"], prefix="Em" )
categorical=categorical+["shop_city","shop_type"]<merge>
test = pd.get_dummies(test, columns = ["Embarked"], prefix="Em")
train=train.join(total_cnt_for_shops,on=["date_block_num","shop_id"],how="left")
 train.fillna(0,inplace=True)
 train["total_cnt_for_shops"]=train["total_cnt_for_shops"].astype(np.float16)
 train.head().T<drop_column>
predictors = train.drop(['Survived', 'PassengerId'], axis=1)
 target = train["Survived"]
x_train, x_test, y_train, y_test = train_test_split(predictors, target, test_size = 0.20, random_state = 0)
%%time
train,n_col=create_lag_features(train,"total_cnt_for_shops")
 train=train.fillna(0)
 train=train.drop(["total_cnt_for_shops"],axis=1)
 numeric=numeric+n_col<groupby>
logreg = LogisticRegression()
logreg.fit(x_train, y_train)
 y_pred = logreg.predict(x_test)
 acc_logreg = round(accuracy_score(y_pred, y_test)* 100, 2)
 print(acc_logreg)
total_cnt_for_item_categories=sales_train_df[["date_block_num","item_category_id","item_cnt_day"]].groupby(["date_block_num","item_category_id"] ).sum()
total_cnt_for_item_categories.columns=["total_cnt_for_item_categories"]
total_cnt_for_item_categories.head()<feature_engineering>
randomforest = RandomForestClassifier()
randomforest.fit(x_train, y_train)
 y_pred = randomforest.predict(x_test)
 acc_randomforest = round(accuracy_score(y_pred, y_test)* 100, 2)
 print(acc_randomforest )
sum_cnt_for_item_category["category"]=sum_cnt_for_item_category.item_category_name.str.split("-" ).str[0]
sum_cnt_for_item_category["subcategory"]=sum_cnt_for_item_category.item_category_name.str.split("-" ).str[1]
sum_cnt_for_item_category.head()<data_type_conversions>
gbk = GradientBoostingClassifier()
gbk.fit(x_train, y_train)
 y_pred = gbk.predict(x_test)
 acc_gbk = round(accuracy_score(y_pred, y_test)* 100, 2)
 print(acc_gbk)
train=train.join(items_df.set_index(["item_id"]),on=["item_id"],how="left")
 train=train.join(sum_cnt_for_item_category[["item_category_id","category","subcategory"]].set_index(["item_category_id"]),on=["item_category_id"],how="left")
 train.fillna(0,inplace=True)
 train["category"]=train["category"].astype(str)
 train["subcategory"]=train["subcategory"].astype(str)
 train.tail().T<define_variables>
xgb_params = {
'n_estimators': [200, 500],
'subsample': [0.6, 1.0],
'max_depth': [2,5,8],
'learning_rate': [0.1,0.01,0.02],
"min_samples_split": [2,5,10]}
xgb = GradientBoostingClassifier()
xgb_cv_model = GridSearchCV(xgb, xgb_params, cv = 10, n_jobs = -1, verbose = 2)
 xgb_cv_model.fit(x_train, y_train)
 xgb_cv_model.best_params_
xgb = GradientBoostingClassifier(learning_rate = xgb_cv_model.best_params_["learning_rate"],
max_depth = xgb_cv_model.best_params_["max_depth"],
min_samples_split = xgb_cv_model.best_params_["min_samples_split"],
n_estimators = xgb_cv_model.best_params_["n_estimators"],
subsample = xgb_cv_model.best_params_["subsample"])
 xgb_tuned = xgb.fit(x_train,y_train)
 y_pred = xgb_tuned.predict(x_test)
 acc_gbk = round(accuracy_score(y_pred, y_test)* 100, 2)
 print(acc_gbk)
<merge><EOS> 
test
ids = test['PassengerId']
predictions = xgb_tuned.predict(test.drop('PassengerId', axis=1))
output = pd.DataFrame({ 'PassengerId' : ids, 'Survived': predictions })
 output.to_csv('submission.csv', index=False)
 output.head()