Kaggle competition: Titanic - Machine Learning from Disaster
<SOS> metric: categorizationaccuracy Kaggle data source: titanic-machine-learning-from-disaster<compute_train_metric>
import os
import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib as mpl
import matplotlib.pyplot as plt
from scipy import stats
from scipy.stats import norm, skew
from sklearn.preprocessing import LabelEncoder
from datetime import datetime
all = []
for k in range(x.shape[1]):
auc = roc_auc_score(OOF_CSV[0].target,x[:,k])
 all.append(auc)
 print('Model %i has OOF AUC = %.4f'%(k,auc))
m = [np.argmax(all)]; w = []<find_best_params>
train = pd.read_csv('.. /input/titanic/train.csv')
 test = pd.read_csv('.. /input/titanic/test.csv' )
old = np.max(all);
RES = 200;
PATIENCE = 10;
TOL = 0.0003
DUPLICATES = False
print('Ensemble AUC = %.4f by beginning with model %i'%(old,m[0]))
print()
for kk in range(len(OOF)) :
md = x[:,m[0]]
for i,k in enumerate(m[1:]):
md = w[i]*x[:,k] +(1-w[i])*md
mx = 0; mx_k = 0; mx_w = 0
print('Searching for best model to add...')
 for k in range(x.shape[1]):
print(k,', ',end='')
 if not DUPLICATES and(k in m): continue
bst_j = 0; bst = 0; ct = 0
for j in range(RES):
tmp = j/RES*x[:,k] +(1-j/RES)*md
auc = roc_auc_score(TRUE,tmp)
 if auc>bst:
bst = auc
bst_j = j/RES
else: ct += 1
if ct>PATIENCE: break
if bst>mx:
mx = bst
mx_k = k
mx_w = bst_j
inc = mx-old
if inc<=TOL:
print() ; print('No increase.Stopping.')
 break
print() ;
print('Ensemble AUC = %.4f after adding model %i with weight %.3f.Increase of %.4f'%(mx,mx_k,mx_w,inc))
print()
old = mx; m.append(mx_k); w.append(mx_w )<train_model>
categorical = len(train.select_dtypes(include=['object'] ).columns)
 numerical = len(train.select_dtypes(include=['int64','float64'] ).columns)
 print('Total number of variables= ', categorical, 'Categorical', '+',
numerical, 'Numerical', '=>', categorical+numerical, 'variables')
 train.head(10 )
print('We are using models',m)
 print('with weights',w)
 print('and achieve ensemble AUC = %.4f'%old )<save_to_csv>
comb = train.append(test)
 comb.shape
comb.isnull().sum() /comb.isnull().count() *100
df = OOF_CSV[0].copy()
df.pred = md
df.to_csv('ensemble_oof.csv',index=False )<load_from_csv>
TicketType = []
for i in range(len(comb.Ticket)) :
ticket = comb.Ticket.iloc[i]
for c in string.punctuation:
ticket = ticket.replace(c,"")
 splited_ticket = ticket.split(" ")
 if len(splited_ticket)== 1:
TicketType.append('NO')
 else:
TicketType.append(splited_ticket[0])
 comb['TicketType'] = TicketType
comb['TicketType'] = np.where(( comb.TicketType!='NO')&(comb.TicketType!='PC')& 
(comb.TicketType!='CA')&(comb.TicketType!='A5')& 
(comb.TicketType!='SOTONOQ'),'OT',comb.TicketType)
 comb['Crew'] = np.where(comb.Fare==0, 1, 0)
 comb.Crew.value_counts()
comb['LastName'] = comb['Name'].str.extract('([A-Za-z]+),', expand=False)
 comb['Group'] = comb['TicketType'].astype(str)+ comb['Embarked'].astype(str)+ comb['LastName'].astype(str)
 le = LabelEncoder()
comb['GroupID'] = le.fit_transform(comb['Group'] )
SUB = np.sort([f for f in FILES if 'sub' in f])
 SUB_CSV = [pd.read_csv(PATH+k)for k in SUB]
print('We have %i submission files...'%len(SUB))
print() ; print(SUB )<define_variables>
comb['TicketFreq'] = comb.groupby('Ticket')['Ticket'].transform('count' )
a = np.array([ int(x.split('_')[1].split('.')[0])for x in SUB ])
 b = np.array([ int(x.split('_')[1].split('.')[0])for x in OOF ])
 if len(a)!=len(b):
print('ERROR submission files dont match oof files')
 else:
for k in range(len(a)) :
if a[k]!=b[k]: print('ERROR submission files dont match oof files' )<prepare_x_and_y>
comb.drop(['Ticket', 'Sex', 'Title', 'Name', 'Fare', 'Age', 'TicketType', 'LastName', 'Parch', 'SibSp', 'Crew',
'Cabin', 'Embarked', 'Group'], axis=1, inplace=True)
 comb.head(5 )
y = np.zeros(( len(SUB_CSV[0]),len(SUB)))
 for k in range(len(SUB)) :
y[:,k] = SUB_CSV[k].target.values<save_to_csv>
n=len(train)
 train_df = comb.iloc[:n]
test_df = comb.iloc[n:]
y_train = train_df['Survived'].astype(int)
 X_train = train_df.drop(['Survived', 'PassengerId'], axis=1)
 X_test = test_df.drop(['Survived', 'PassengerId'], axis=1 )
df = SUB_CSV[0].copy()
df.target = md2
df.to_csv('ensemble_sub.csv',index=False )<install_modules>
def stats(models, X_train, y_train):
stats = {}
for name, inst in models.items() :
mscores = []
model_pipe = make_pipeline(StandardScaler() , inst)
 model_pipe.fit(X_train, y_train)
 acc=round(model_pipe.score(X_train, y_train)* 100, 2)
 mscores.append(acc)
 scores = cross_val_score(model_pipe, X_train, y_train, cv=10, scoring='accuracy')
 acccv=round(scores.mean() * 100, 2)
 mscores.append(acccv)
 mscores.append(scores.std())
 y_train_cv_pred = cross_val_predict(model_pipe, X_train, y_train, cv=10)
 p = round(precision_score(y_train, y_train_cv_pred)* 100, 2)
 s = round(recall_score(y_train, y_train_cv_pred)* 100, 2)
 mscores.append(p)
 mscores.append(s)
 f1 = 2*(( p*s)/(p+s))
mscores.append(f1)
 roc_auc_cvs = round(cross_val_score(model_pipe, X_train, y_train, cv=10, scoring='roc_auc' ).mean() * 100, 2)
 mscores.append(roc_auc_cvs)
 stats[name] = mscores
colnames = ['Accuracy','AccuracyCv','StandardDeviation', 'Precision','Sensitivity','F1Score', 'RocAucCv']
df_ms = pd.DataFrame.from_dict(stats, orient='index', columns=colnames)
 df_msr = df_ms.sort_values(by='RocAucCv', ascending=False)
 return df_msr
models = {'AdaBoostClassifier': AdaBoostClassifier() ,
'GradientBoostingClassifier': GradientBoostingClassifier() ,
'LogisticRegression': LogisticRegression() ,
'SupportVectorMachines': SVC(random_state=0),
'RandomForestClassifier': RandomForestClassifier() ,
'KNeighborsClassifier': KNeighborsClassifier() ,
'DecisionTreeClassifier': DecisionTreeClassifier() }
df_all_models = stats(models, X_train,y_train)
 df_all_models
!pip install xgboost 
<load_from_csv>
vote = VotingClassifier(estimators=[ 
('AdaBoostClassifier', AdaBoostClassifier()), 
('GradientBoostingClassifier', GradientBoostingClassifier()), 
('LogisticRegression', LogisticRegression()), 
('SupportVectorMachines', SVC()), 
('RandomForest', RandomForestClassifier()), 
('KNN', KNeighborsClassifier())
 ],
voting='hard', n_jobs=15)
 vote.fit(X_train,y_train)
 prediction_vote=vote.predict(X_test)
 print('Voting Classifier Accuracy Score = {:.2f}%'.format(round(vote.score(X_train,y_train, sample_weight=None)* 100, 2)) , 'on', datetime.now() )
<data_type_conversions><EOS> 
output= pd.DataFrame(pd.DataFrame({
"PassengerId": test_df["PassengerId"],
"Survived": prediction_vote}))
output.head()
output.to_csv('FinalSubmission.csv', index=False )