Kaggle competition: Titanic - Machine Learning from Disaster
<SOS> metric: categorizationaccuracy Kaggle data source: titanic-machine-learning-from-disaster<create_dataframe>
warnings.filterwarnings("ignore" )
test_id = test_df["Id"].map(str)
 submit_xg = pd.DataFrame({'Id': test_id, "winPlacePerc": test_stack_model} , columns=['Id', 'winPlacePerc'] )<save_to_csv>
train = pd.read_csv(".. /input/titanic/train.csv")
 test = pd.read_csv(".. /input/titanic/test.csv" )
submit_xg.to_csv("submission.csv", index = False )<set_options>
train =train.astype({"Pclass":"category"})
 test =test.astype({"Pclass":"category"} )
%load_ext autoreload
%autoreload 2
%matplotlib inline<import_modules>
y=train["Survived"].values
test_index = len(y)-1
data = pd.concat([train,test],sort=False ).reset_index(drop=True)
 data.info()
from fastai.imports import *
from pandas_summary import DataFrameSummary
from sklearn.ensemble import RandomForestRegressor, RandomForestClassifier
from IPython.display import display
from sklearn import metrics
from sklearn.model_selection import train_test_split
import gc<set_options>
data["Title"]= data["Name"].str.split(", ",expand=True)[1].str.split(".",expand=True)[0]
mapping = {'Mlle': 'Miss', 'Major': 'Mr', 'Col': 'Mr', 'Sir': 'Mr', 'Don': 'Mr', 'Mme': 'Miss',
'Jonkheer': 'Mr', 'Lady': 'Mrs', 'Capt': 'Mr', 'Countess': 'Mrs', 'Ms': 'Miss', 'Dona': 'Mrs'}
data.replace({'Title': mapping}, inplace=True )
def set_plot_sizes(sml, med, big):
plt.rc('font', size=sml)
 plt.rc('axes', titlesize=sml)
 plt.rc('axes', labelsize=med)
 plt.rc('xtick', labelsize=sml)
 plt.rc('ytick', labelsize=sml)
 plt.rc('legend', fontsize=sml)
 plt.rc('figure', titlesize=big)
 def parallel_trees(m, fn, n_jobs=8):
return list(ProcessPoolExecutor(n_jobs ).map(fn, m.estimators_))
def draw_tree(t, df, size=10, ratio=0.6, precision=0):

 s=export_graphviz(t, out_file=None, feature_names=df.columns, filled=True,
special_characters=True, rotate=True, precision=precision)
 IPython.display.display(graphviz.Source(re.sub('Tree {',
f'Tree {{ size={size}; ratio={ratio}', s)))
 def combine_date(years, months=1, days=1, weeks=None, hours=None, minutes=None,
seconds=None, milliseconds=None, microseconds=None, nanoseconds=None):
years = np.asarray(years)- 1970
months = np.asarray(months)- 1
days = np.asarray(days)- 1
types =('<M8[Y]', '<m8[M]', '<m8[D]', '<m8[W]', '<m8[h]',
'<m8[m]', '<m8[s]', '<m8[ms]', '<m8[us]', '<m8[ns]')
 vals =(years, months, days, weeks, hours, minutes, seconds,
milliseconds, microseconds, nanoseconds)
 return sum(np.asarray(v, dtype=t)for t, v in zip(types, vals)
 if v is not None)
 def get_sample(df,n):

 idxs = sorted(np.random.permutation(len(df)) [:n])
 return df.iloc[idxs].copy()
def add_datepart(df, fldname, drop=True, time=False):

 fld = df[fldname]
fld_dtype = fld.dtype
if isinstance(fld_dtype, pd.core.dtypes.dtypes.DatetimeTZDtype):
fld_dtype = np.datetime64
if not np.issubdtype(fld_dtype, np.datetime64):
df[fldname] = fld = pd.to_datetime(fld, infer_datetime_format=True)
 targ_pre = re.sub('[Dd]ate$', '', fldname)
 attr = ['Year', 'Month', 'Week', 'Day', 'Dayofweek', 'Dayofyear',
'Is_month_end', 'Is_month_start', 'Is_quarter_end', 'Is_quarter_start', 'Is_year_end', 'Is_year_start']
if time: attr = attr + ['Hour', 'Minute', 'Second']
for n in attr: df[targ_pre + n] = getattr(fld.dt, n.lower())
 df[targ_pre + 'Elapsed'] = fld.astype(np.int64)// 10 ** 9
if drop: df.drop(fldname, axis=1, inplace=True)
 def is_date(x): return np.issubdtype(x.dtype, np.datetime64)
 def train_cats(df):

 for n,c in df.items() :
if is_string_dtype(c): df[n] = c.astype('category' ).cat.as_ordered()
def apply_cats(df, trn):

 for n,c in df.items() :
if(n in trn.columns)and(trn[n].dtype.name=='category'):
df[n] = pd.Categorical(c, categories=trn[n].cat.categories, ordered=True)
 def fix_missing(df, col, name, na_dict):

 if is_numeric_dtype(col):
if pd.isnull(col ).sum() or(name in na_dict):
df[name+'_na'] = pd.isnull(col)
 filler = na_dict[name] if name in na_dict else col.median()
df[name] = col.fillna(filler)
 na_dict[name] = filler
return na_dict
def numericalize(df, col, name, max_n_cat):

 if not is_numeric_dtype(col)and(max_n_cat is None or len(col.cat.categories)>max_n_cat):
df[name] = col.cat.codes+1
def scale_vars(df, mapper):
warnings.filterwarnings('ignore', category=sklearn.exceptions.DataConversionWarning)
 if mapper is None:
map_f = [([n],StandardScaler())for n in df.columns if is_numeric_dtype(df[n])]
mapper = DataFrameMapper(map_f ).fit(df)
 df[mapper.transformed_names_] = mapper.transform(df)
 return mapper
def proc_df(df, y_fld=None, skip_flds=None, ignore_flds=None, do_scale=False, na_dict=None,
preproc_fn=None, max_n_cat=None, subset=None, mapper=None):

 if not ignore_flds: ignore_flds=[]
if not skip_flds: skip_flds=[]
if subset: df = get_sample(df,subset)
 else: df = df.copy()
ignored_flds = df.loc[:, ignore_flds]
df.drop(ignore_flds, axis=1, inplace=True)
 if preproc_fn: preproc_fn(df)
 if y_fld is None: y = None
else:
if not is_numeric_dtype(df[y_fld]): df[y_fld] = df[y_fld].cat.codes
y = df[y_fld].values
skip_flds += [y_fld]
df.drop(skip_flds, axis=1, inplace=True)
 if na_dict is None: na_dict = {}
else: na_dict = na_dict.copy()
na_dict_initial = na_dict.copy()
for n,c in df.items() : na_dict = fix_missing(df, c, n, na_dict)
 if len(na_dict_initial.keys())> 0:
df.drop([a + '_na' for a in list(set(na_dict.keys())- set(na_dict_initial.keys())) ], axis=1, inplace=True)
 if do_scale: mapper = scale_vars(df, mapper)
 for n,c in df.items() : numericalize(df, c, n, max_n_cat)
 df = pd.get_dummies(df, dummy_na=True)
 df = pd.concat([ignored_flds, df], axis=1)
 res = [df, y, na_dict]
if do_scale: res = res + [mapper]
return res
def rf_feat_importance(m, df):
return pd.DataFrame({'cols':df.columns, 'imp':m.feature_importances_}
).sort_values('imp', ascending=False)
 def set_rf_samples(n):

 forest._generate_sample_indices =(lambda rs, n_samples:
forest.check_random_state(rs ).randint(0, n_samples, n))
def reset_rf_samples() :

 forest._generate_sample_indices =(lambda rs, n_samples:
forest.check_random_state(rs ).randint(0, n_samples, n_samples))
def get_nn_mappers(df, cat_vars, contin_vars):
for v in contin_vars: df[v] = df[v].fillna(df[v].max() +100,)
 for v in cat_vars: df[v].fillna('
cat_maps = [(o, LabelEncoder())for o in cat_vars]
contin_maps = [([o], StandardScaler())for o in contin_vars]
return DataFrameMapper(cat_maps ).fit(df), DataFrameMapper(contin_maps ).fit(df )<define_variables>
data["Fam_size"] = data["SibSp"]+data["Parch"]+1
def mapping(title,tick_size):
for(ind,val)in zip(age_impute,age_impute.values()):
if(( ind[0]==title)&(ind[1]==tick_size)) :
return val
age_impute=data.groupby(["Title","Fam_size"])["Age"].median().astype("float64" ).to_dict()
PATH_INPUT = "/kaggle/input/"
PATH_WORKING = "/kaggle/working/"
PATH_TMP = "/tmp/"<load_from_csv>
data.loc[data["Age"].isnull() ,"Age"]= data.loc[data["Age"].isnull() ,["Title","Fam_size"]].apply(lambda x: mapping(x[0],x[1]),axis=1 )
df_raw = pd.read_csv(f'{PATH_INPUT}train_V2.csv' )<load_from_csv>
data["Age_bin"] = pd.qcut(data["Age"],10,duplicates="drop")
 label = LabelEncoder()
data['Age_bin'] = label.fit_transform(data['Age_bin'].astype(str))
df_test_raw = pd.read_csv(f'{PATH_INPUT}test_V2.csv' )<categorify>
data["Fare"].fillna(data[~data["Fare"].isnull() ].groupby("Pclass")["Fare"].mean() [3],inplace=True)
 data['Last_Name'] = data['Name'].apply(lambda x: str.split(x, ",")[0])
 data['Family_Survival'] = 0.5
for grp, grp_data in data[['Survived','Name', 'Last_Name', 'Fare', 'Ticket', 'PassengerId',
'SibSp', 'Parch', 'Age', 'Cabin']].groupby(['Last_Name', 'Fare']):
if(len(grp_data)!= 1):
for ind, row in grp_data.iterrows() :
vmax = grp_data.drop(ind)['Survived'].max()
vmin = grp_data.drop(ind)['Survived'].min()
passid = row['PassengerId']
if(vmax == 1.0):
data.loc[data['PassengerId'] == passid, 'Family_Survival'] = 1
elif(vmin==0.0):
data.loc[data['PassengerId'] == passid, 'Family_Survival'] = 0
for grp, grp_data in data.groupby('Ticket'):
if(len(grp_data)!= 1):
for ind, row in grp_data.iterrows() :
if(row['Family_Survival'] == 0)|(row['Family_Survival']== 0.5):
vmax = grp_data.drop(ind)['Survived'].max()
vmin = grp_data.drop(ind)['Survived'].min()
passid = row['PassengerId']
if(vmax == 1.0):
data.loc[data['PassengerId'] == passid, 'Family_Survival'] = 1
elif(vmin==0.0):
data.loc[data['PassengerId'] == passid, 'Family_Survival'] = 0
df_raw.to_feather(f'{PATH_TMP}train_raw')
 df_test_raw.to_feather(f'{PATH_TMP}test_raw' )<feature_engineering>
data["Embarked"].fillna(data["Embarked"].mode() [0],inplace=True)
 data["Fare"]=data["Fare"]/data["Fam_size"]
data["Fare"].fillna(data[~data["Fare"].isnull() ].groupby("Pclass")["Fare"].mean() [3],inplace=True )
df_raw["winPlace"] = round(df_raw["winPlacePerc"] /(1.0/(df_raw["maxPlace"] - 1)) )<drop_column>
data["Fare_bin"] = pd.qcut(data["Fare"],13)
 label = LabelEncoder()
data['Fare_bin'] = label.fit_transform(data['Fare_bin'].astype(str))
df_raw.drop(labels="winPlace", axis=1, inplace=True )<sort_values>
data.drop(["Name","SibSp","Parch","Ticket","Age","Fare","PassengerId","Last_Name","Survived","Title","Cabin"],inplace=True,axis=1 )
df_raw.isnull().sum().sort_values(ascending=False)/len(df_raw )<correct_missing_values>
data.isnull().sum() /data.shape[0]*100
df_raw.dropna(inplace=True)
 df_raw.shape<count_missing_values>
data = pd.get_dummies(data,drop_first=True )
df_test_raw.isnull().sum().any()<concatenate>
train_1 = data.iloc[:test_index+1,:]
test_1 = data.iloc[test_index+1:,:]
train_cats(df_raw )<define_variables>
X_train,X_test,y_train,y_test = train_test_split(train_1,y,test_size=0.2, random_state=42 )
ids = df_test_raw["Id"]<concatenate>
sk_fold = StratifiedKFold(10,shuffle=True, random_state=42)
 sc =StandardScaler()
X_train= sc.fit_transform(X_train)
 X_train_1= sc.transform(train_1.values)
 X_test= sc.transform(X_test)
 X_submit= sc.transform(test_1.values)
 log_reg = LogisticRegression()
ran_for = RandomForestClassifier()
ada_boost = AdaBoostClassifier()
grad_boost = GradientBoostingClassifier(n_estimators=100)
 hist_grad_boost = HistGradientBoostingClassifier()
knn = KNeighborsClassifier()
tree= DecisionTreeClassifier()
svc = SVC()
xgb = XGBClassifier()
clf = [("Logistic Regression",log_reg,{"penalty":['l2'],"C":[100, 10, 1.0, 0.1, 0.01]}),\ 
("Support Vector",svc,{"kernel": ["rbf"],"gamma":[0.1, 1, 10, 100],"C":[0.1, 1, 10, 100, 1000]}),\ 
("Decision Tree", tree, {}),\ 
("Random Forest",ran_for,{"n_estimators":[100],"random_state":[42],"min_samples_leaf":[5,10,20,40,50],"bootstrap":[False]}),\ 
("Adapative Boost",ada_boost,{"n_estimators":[100],"learning_rate":[.6,.8,1]}),\ 
("Gradient Boost",grad_boost,{}),\ 
("Histogram GB",hist_grad_boost,{"loss":["binary_crossentropy"],"min_samples_leaf":[5,10,20,40,50],"l2_regularization":[0,.1,1]}),\ 
("XGBoost",xgb,{"n_estimators":[200],"max_depth":[3,4,5],"learning_rate":[.01,.1,.2],"subsample":[.8],"colsample_bytree":[1],"gamma":[0,1,5],"lambda":[.01,.1,1]}),\ 
("K Nearest",knn,{"n_neighbors":[3,5,8],"leaf_size":[25,30,35]})]
stack_list=[]
train_scores = pd.DataFrame(columns=["Name","Train Score","Test Score"])
 i=0
for name,clf1,param_grid in clf:
clf = GridSearchCV(clf1,param_grid=param_grid,scoring="accuracy",cv=sk_fold,return_train_score=True)
 clf.fit(X_train,y_train.reshape(-1,1))
y_pred = clf.best_estimator_.predict(X_test)
 cm = confusion_matrix(y_test,y_pred)
 train_scores.loc[i]= [name,clf.best_score_,(cm[0,0]+cm[1,1,])/(cm[0,0]+cm[0,1]+cm[1,0]+cm[1,1])]
stack_list.append(clf.best_estimator_)
 i=i+1
est = [("dec_tree",stack_list[2]),("ran_for",stack_list[3]),("ada_boost",stack_list[4]),("grad_boost",stack_list[5]),("hist_grad_boost",stack_list[6]),("svc",stack_list[1]),("lr",stack_list[0]),("knn",stack_list[8])]
sc = StackingClassifier(estimators=est,final_estimator = stack_list[2],cv=sk_fold,passthrough=False)
 sc.fit(X_train,y_train)
 y_pred = sc.predict(X_test)
 cm1 = confusion_matrix(y_test,y_pred)
 y_pred_train = sc.predict(X_train)
 cm2 = confusion_matrix(y_train,y_pred_train)
 train_scores.append(pd.Series(["Stacking",(cm2[0,0]+cm2[1,1,])/(cm2[0,0]+cm2[0,1]+cm2[1,0]+cm2[1,1]),(cm1[0,0]+cm1[1,1,])/(cm1[0,0]+cm1[0,1]+cm1[1,0]+cm1[1,1])],index=train_scores.columns),ignore_index=True )
apply_cats(df_test_raw, df_raw )<prepare_x_and_y>
stack_list[7].fit(X_train_1,y)
 y_submit = stack_list[7].predict(X_submit)
 submit = pd.DataFrame({
"PassengerId": test.PassengerId,
"Survived": y_submit
} )
df, y, _ = proc_df(df_raw, 'winPlacePerc', skip_flds=["Id","groupId","matchId"], subset=100000 )<categorify>
submit.PassengerId = submit.PassengerId.astype(int)
 submit.Survived = submit.Survived.astype(int)
 submit.to_csv("titanic_submit.csv", index=False)