Kaggle competition: Titanic - Machine Learning from Disaster
<SOS> metric: categorizationaccuracy Kaggle data source: titanic-machine-learning-from-disaster<load_from_csv>
%matplotlib inline
sub=pd.read_csv('/kaggle/input/ml-challenge-week6/sampleSubmission.csv')
 sub["income class"]=d_test["y_new"]<save_to_csv>
train_df = pd.read_csv(".. /input/titanic/train.csv")
 test_df = pd.read_csv(".. /input/titanic/test.csv")
 combine = [train_df, test_df]
filename = 'submission.csv'
sub.to_csv(filename,index=False )<import_modules>
train_df.isnull().sum()
import os 
<import_modules>
print('_'*40)
 test_df.isnull().sum()
import pandas as pd
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import roc_auc_score 
<load_from_csv>
train_df[['Pclass', 'Survived']].groupby(['Pclass'], as_index=False ).mean().sort_values(by='Survived',
ascending=False )
df_train = pd.read_csv('.. /input/svnit-ml-1/train.csv')
 df_test = pd.read_csv('.. /input/testacm1/test.csv')
<prepare_x_and_y>
train_df[["Sex", "Survived"]].groupby(['Sex'], as_index=False ).mean().sort_values(by='Survived',
ascending=False )
train_X = df_train.loc[:, 'X1':'X23']
train_y = df_train.loc[:, 'Y'] 
<load_pretrained>
train_df[["SibSp", "Survived"]].groupby(['SibSp'], as_index=False ).mean().sort_values(by='Survived',
ascending=False )
rf = RandomForestClassifier(n_estimators=50, random_state=123)
<train_model>
train_df[["Parch", "Survived"]].groupby(['Parch'], as_index=False ).mean().sort_values(by='Survived',
ascending=False )
rf.fit(train_X, train_y)
<predict_on_test>
for dataset in combine:
dataset['Title'] = dataset.Name.str.extract('([A-Za-z]+)\.', expand=False)
 pd.crosstab(train_df['Title'], train_df['Sex'] )
df_test = df_test.loc[:, 'X1':'X23']
pred = rf.predict_proba(df_test)
<save_to_csv>
for dataset in combine:
dataset['Title'] = dataset['Title'].replace(['Lady', 'Countess','Capt', 'Col','Don', 'Dr', 'Major', 'Rev', 'Sir', 'Jonkheer', 'Dona'], 'Rare')
 dataset['Title'] = dataset['Title'].replace('Mlle', 'Miss')
 dataset['Title'] = dataset['Title'].replace('Ms', 'Miss')
 dataset['Title'] = dataset['Title'].replace('Mme', 'Mrs')
 train_df[['Title', 'Survived']].groupby(['Title'], as_index=False ).mean()
result = pd.DataFrame(pred[:,1])
 result.columns = ['Y']
result.index.name = 'ID'
result.index += 1
result.to_csv('output.csv')
<define_variables>
title_mapping = {"Mr": 1, "Miss": 2, "Mrs": 3, "Master": 4, "Rare": 5}
for dataset in combine:
dataset['Title'] = dataset['Title'].map(title_mapping)
 dataset['Title'] = dataset['Title'].fillna(0)
 train_df.head()
seed = 300<import_modules>
train_df = train_df.drop(['Name', 'PassengerId'], axis=1)
 test_df = test_df.drop(['Name', 'PassengerId'], axis=1)
 combine = [train_df, test_df]
train_df.shape, test_df.shape
from math import *
import math
import numpy as np
import pandas as pd<load_from_csv>
for dataset in combine:
dataset['Sex'] = dataset['Sex'].map({'female': 1, 'male': 0} ).astype(int)
 train_df.head()
Train = pd.read_csv(".. /input/hta-tagging/train.csv")
 Train.head(5 )<load_from_csv>
guess_ages = np.zeros(( 2,3))
guess_ages
Test = pd.read_csv(".. /input/hta-tagging/test.csv")
 Test.head(5 )<feature_engineering>
for dataset in combine:
for i in range(0, 2):
for j in range(0, 3):
guess_df = dataset[(dataset['Sex'] == i)&(dataset['Pclass'] == j+1)]['Age'].dropna()
age_guess = guess_df.median()
guess_ages[i,j] = int(age_guess/0.5 + 0.5)* 0.5
for i in range(0, 2):
for j in range(0, 3):
dataset.loc[(dataset.Age.isnull())&(dataset.Sex == i)&(dataset.Pclass == j+1),'Age'] = guess_ages[i,j]
dataset['Age'] = dataset['Age'].astype(int)
 train_df.head()
def ReadFileToDataFrame(df,path):
for i in range(len(df.Filename)) :
Filename = df.Filename[i];
CurrentFile = ".. /input/hta-tagging/{2}/{2}/{0}/{1}".format(Filename.split("-")[0],Filename,path);
File = open(CurrentFile, "r");
contents = File.read() ;
contents = contents.replace("\r
","
")
 contents = contents.replace("\r","
")
 df.Filename[i] = contents
continue;
df = df.rename(columns={"Filename":"Text"},inplace=True);
ReadFileToDataFrame(Train,"train-data");
Train.head(5 )<feature_engineering>
train_df['AgeBand'] = pd.cut(train_df['Age'], 5)
 train_df[['AgeBand', 'Survived']].groupby(['AgeBand'], as_index=False ).mean().sort_values(by='AgeBand', ascending=True )
def ReadTestFileToDataFrame(df,path):
df["Text"] = [""]*len(df.Id);
for i in range(len(df.Id)) :
Filename = df.Id[i];
CurrentFile = ".. /input/hta-tagging/{2}/{2}/{0}/{1}".format(Filename.split("-")[0],Filename,path);
def Do(File):
contents = File.read() ;
contents = contents.replace("\r
","
")
 contents = contents.replace("\r","
")
 df.Text[i] = contents
try:
with codecs.open(CurrentFile, encoding='cp1252')as File:
Do(File);
except:
with codecs.open(CurrentFile, encoding='utf-8')as File:
Do(File);
continue;
df = df.rename(columns={"Filename":"Text"},inplace=True);
ReadTestFileToDataFrame(Test,"test-data");
Test.head(5 )<categorify>
for dataset in combine:
dataset.loc[ dataset['Age'] <= 16, 'Age'] = 0
dataset.loc[(dataset['Age'] > 16)&(dataset['Age'] <= 32), 'Age'] = 1
dataset.loc[(dataset['Age'] > 32)&(dataset['Age'] <= 48), 'Age'] = 2
dataset.loc[(dataset['Age'] > 48)&(dataset['Age'] <= 64), 'Age'] = 3
dataset.loc[ dataset['Age'] > 64, 'Age'] = 4
train_df.head()
def AnsEncoder(df, col_name):
for i in range(len(df[col_name])) :
val = 0;
txt = df.loc[i,col_name];
if(txt == "P"): val = 2;
if(txt == "Q"): val = 1;
if(txt == "N"): val = 0;
df.loc[i,col_name] = val;
continue;
df[col_name] = pd.to_numeric(df[col_name]);<categorify>
train_df = train_df.drop(['AgeBand'], axis=1)
 combine = [train_df, test_df]
train_df.head()
AnsEncoder(Train, "Blinding of intervention");
AnsEncoder(Train, "Blinding of Outcome assessment");
Train.head(5 )<feature_engineering>
for dataset in combine:
dataset['FamilySize'] = dataset['SibSp'] + dataset['Parch'] + 1
train_df[['FamilySize', 'Survived']].groupby(['FamilySize'], as_index=False ).mean().sort_values(by='Survived', ascending=False )
def remove_ref(text):
newtext = "";
for line in text.split("
"):
if(line.lower() =="references" or line.lower() =="reference"): break;
else: newtext += line + "
";
return text;
Train.Text = Train.Text.apply(lambda x : remove_ref(x))
Test.Text = Test.Text.apply(lambda x : remove_ref(x))<string_transform>
for dataset in combine:
dataset['IsAlone'] = 0
dataset.loc[dataset['FamilySize'] == 1, 'IsAlone'] = 1
train_df[['IsAlone', 'Survived']].groupby(['IsAlone'], as_index=False ).mean()
def FindBeforeColon(text):
def kwreplace(cutted):
cutted = cutted.lower() ;
if("key" in cutted): cutted = "key";
elif("objective" in cutted): cutted = "objective";
elif("reference" in cutted): cutted = "reference";
elif("method" in cutted): cutted = "method";
elif(cutted.endswith("es")) : cutted = cutted[:-2];
elif(cutted.endswith("s")) : cutted = cutted[:-1];
return cutted;
tmptxt = "";
curtopic = "";
topic = [];
for line in text.split("
"):
if(":" in line):
cutted = line.split(":")[0];
if(len(cutted)>0):
if(len(cutted.split(" ")) < 6 and cutted[0] in "ABCDEFGHIJKLMNOPQRSTUVWXYZ" and(not "Author" in cutted)) :
cutted = kwreplace(cutted);
if(curtopic != ""):
topic.append(( curtopic, tmptxt)) ;
tmptxt = "";
curtopic = cutted;
tmptxt = "".join(line.split(":")[1:]);
continue;
if(len(line)>0):
if(not " " in line and line[0] in "ABCDEFGHIJKLMNOPQRSTUVWXYZ"):
if(curtopic != ""):
topic.append(( curtopic, tmptxt)) ;
curtopic = kwreplace(line);
tmptxt = "";
continue;
tmptxt += "
" + line;
return topic;<categorify>
train_df = train_df.drop(['Parch', 'SibSp'], axis=1)
 test_df = test_df.drop(['Parch', 'SibSp'], axis=1)
 combine = [train_df, test_df]
train_df.head()
TO = [];
i = 0;
for txt in Train.Text:
TO.append(( i,FindBeforeColon(txt)));
i += 1
TestO = [];
i = 0;
for txt in Test.Text:
TestO.append(( i,FindBeforeColon(txt)));
i += 1;<define_variables>
freq_port = train_df.Embarked.dropna().mode() [0]
print(freq_port )
SelectedCols = ["Text","method","key"]<save_to_csv>
for dataset in combine:
dataset['Embarked'] = dataset['Embarked'].fillna(freq_port)
 train_df[['Embarked', 'Survived']].groupby(['Embarked'], as_index=False ).mean().sort_values(by='Survived', ascending=False )
def WriteToPD(df,arr):
arrlength = len(arr);
PlaceholderList = [None]*df.shape[0];
for idx, optionlist in arr:
if(idx % 100 == 0):
print("{0}/{1} Completed".format(idx,arrlength)) ;
for option, text in optionlist:
if option in SelectedCols:
if not option in df:
df[option] = PlaceholderList;
df.loc[idx,option] = text;<save_to_csv>
for dataset in combine:
dataset['Embarked'] = dataset['Embarked'].map({'S': 0, 'C': 1, 'Q': 2} ).astype(int)
 train_df.head()
WriteToPD(Train,TO)
 Train.head()<save_to_csv>
test_df['Fare'].fillna(test_df['Fare'].dropna().median() , inplace=True )
WriteToPD(Test,TestO)
 Train.head()<choose_model_class>
train_df['FareBand'] = pd.qcut(train_df['Fare'], 4)
 train_df[['FareBand', 'Survived']].groupby(['FareBand'], as_index=False ).mean().sort_values(by='FareBand', ascending=True )
class TFIDFVector:
def __init__(this,train,val):
print("- TF-IDF Vector: Initializing")
 this.tfv = TfidfVectorizer(min_df=3, max_features=None,
strip_accents='unicode', analyzer='word',token_pattern=r'\w{1,}',
ngram_range=(1, 3), use_idf=1,smooth_idf=1,sublinear_tf=1,
stop_words = 'english');
print("- TF-IDF Vector: Preparing");
if type(train[0])!= str:
tl2 = [];
for l in train:
tl2 += [txt for txt in l if txt != None];
for l in val:
tl2 += [txt for txt in l if txt != None];
print("- TF-IDF Vector: Fitting, This might take a while");
this.tfv.fit(tl2);
print("- TF-IDF Vector: Fitted");
del(tl2);
else:
print("- TF-IDF Vector: Fitting, This might take a while");
this.tfv.fit(list(train)+list(val)) ;
print("- TF-IDF Vector: Fitted");
print("- TF-IDF Vector: Initialized")
 def CreateVector(this, val, reduceshape = True):
print("- TF-IDF Vector: Creating Vector")
 if type(val[0])!= str:
ValLength = len(val);
tmplist = []; i = 0;
for ll in val:
if(i % max(ValLength//10,1)== 0): print("- TF-IDF Vector: Creating Vector {0}/{1} Completed".format(i,ValLength)) ;
tmplist.append(( this.tfv.transform(['' if txt is None else txt for txt in ll])));
i += 1;
v = np.stack(np.array([x.toarray() for x in tmplist]))
del(tmplist)
 if(reduceshape):
nsamples = v.shape[0];
value2 = np.prod(v.shape[1:])
 v = v.reshape(( nsamples,value2))
to_return = v;
else:
to_return = this.tfv.transform(val)
 print("- TF-IDF Vector: Vector Created")
 return to_return; 
<train_model>
for dataset in combine:
dataset.loc[ dataset['Fare'] <= 7.91, 'Fare'] = 0
dataset.loc[(dataset['Fare'] > 7.91)&(dataset['Fare'] <= 14.454), 'Fare'] = 1
dataset.loc[(dataset['Fare'] > 14.454)&(dataset['Fare'] <= 31), 'Fare'] = 2
dataset.loc[ dataset['Fare'] > 31, 'Fare'] = 3
dataset['Fare'] = dataset['Fare'].astype(int)
 train_df = train_df.drop(['FareBand'], axis=1)
 combine = [train_df, test_df]
train_df.head(10 )
class SVMModel:
def __init__(this):
return;
def Train(this,X, Y):
print("SVMModel: Training Step will be combined with Predict State")
 this.X = X; this.Y = Y;
def SetVector(this,Vector):
this.VC = Vector;
def Predict(this,XVal):
X = this.VC.CreateVector(this.X); Y = this.Y;
del(this.X);
this.svc = SVC(random_state=seed)
 print("SVMModel: Training")
 this.svc.fit(X, Y); del(X); del(Y);
print("SVMModel: Finished Training")
 print("SVMModel: Predicting");
valv = this.VC.CreateVector(XVal);
del(XVal);
tR = this.svc.predict(valv);
del(valv);
print("SVMModel: Predicted");
return tR;<predict_on_test>
X_train = train_df.drop("Survived", axis=1)
 y_train = train_df["Survived"]
print("X_train.shape" ,X_train.shape)
 print("y_train.shape" ,y_train.shape)
 X_train, X_test, y_train, y_test = train_test_split(X_train, y_train, test_size=0.2, random_state=111 )
def PredictModel(TX,TY,VX,Model):
print("Initializing Model")
 model = Model;
print("Training Model")
 model.Train(( TrainX),(TrainY)) ;
print("Predicting")
 global PredictedY;
PredictedY = model.Predict(VX);
return PredictedY.astype(int);<prepare_x_and_y>
logreg = LogisticRegression()
logreg.fit(X_train, y_train)
 Y_pred_lr = logreg.predict(X_test)
 Score_lr = accuracy_score(y_test,Y_pred_lr)
 print(Score_lr )
TrainX, ValX, TrainY, ValY = TTS(Train[SelectedCols], Train['Blinding of intervention'], test_size=0.2, random_state = seed);
_1, _2, _3, ValY2 = TTS(Train[SelectedCols], Train['Blinding of Outcome assessment'], test_size=0.2, random_state = seed);
TrainX = np.array(TrainX);
TrainY = np.array(TrainY);
ValX = np.array(ValX);
ValY = np.array(ValY);
ValY2 = np.array(ValY2);
TrainY2 = Train['Blinding of Outcome assessment']; TrainY2 = np.array(TrainY2);
TestX = Test[SelectedCols]; TestX = np.array(TestX);
Vector = TFIDFVector(np.array(Train[SelectedCols]), TestX);
ValX = Vector.CreateVector(ValX)
 SVM = SVMModel() ;
SVM.SetVector(Vector);
PredA = PredictModel(TrainX, TrainY , TestX, SVM);
VPredA = SVM.svc.predict(ValX);
AccA = ScoreAcc(ValY,VPredA)
 SVM = SVMModel() ;
SVM.SetVector(Vector);
PredB = PredictModel(TrainX, TrainY2, TestX, SVM);
VPredB = SVM.svc.predict(ValX)
 AccB = ScoreAcc(ValY2,VPredB);
def Convert(i):
if i == 0: return "N";
if i == 1: return "Q";
if i == 2: return "P";
FinalPred = [Convert(PredA[i])+Convert(PredB[i])for i in range(len(PredA)) ];<save_to_csv>
svc = SVC()
svc.fit(X_train, y_train)
 Y_pred_svc = svc.predict(X_test)
 Score_svc = accuracy_score(y_test,Y_pred_svc)
 print(Score_svc )
Test.Prediction = FinalPred;
SubmissionTest = Test[["Id","Prediction"]];
SubmissionTest.to_csv("submission.csv",index=False)
 SubmissionTest.head(5);<compute_test_metric>
knn = KNeighborsClassifier(n_neighbors = 3)
 knn.fit(X_train, y_train)
 Y_pred_knn = knn.predict(X_test)
 Score_knn = accuracy_score(y_test,Y_pred_knn)
 print(Score_knn )
def Convert(i):
if i == 0: return "N";
if i == 1: return "Q";
if i == 2: return "P";
FinalPred = [Convert(VPredA[i])+Convert(VPredB[i])for i in range(len(VPredA)) ];
CorrectAns = [Convert(ValY[i])+Convert(ValY2[i])for i in range(len(ValY)) ];
FinAcc = ScoreAcc(CorrectAns,FinalPred)*100;
print("A set = {0:.2f}%".format(AccA))
print("B set = {0:.2f}%".format(AccB))
print("คะแนนที่คาดว่าจะได้ = {0:.2f}%".format(FinAcc))<set_options>
gaussian = GaussianNB()
gaussian.fit(X_train, y_train)
 Y_pred_gnb = gaussian.predict(X_test)
 Score_gnb = accuracy_score(y_test,Y_pred_gnb)
 print(Score_gnb )
print(os.listdir(".. /input"))
warnings.filterwarnings('ignore')
 plt.rcParams['figure.figsize'] =(16,9)
 sns.set_palette('gist_earth' )<load_from_csv>
perceptron = Perceptron()
perceptron.fit(X_train, y_train)
 Y_pred_per = perceptron.predict(X_test)
 Score_per = accuracy_score(y_test,Y_pred_per)
 print(Score_per )
df_train = pd.read_csv('.. /input/train.csv')
 df_test = pd.read_csv('.. /input/test.csv')
 full = pd.concat([df_train, df_test], axis = 0, sort=True)
 full.set_index('PassengerId', drop = False, inplace=True)
 train = full[:891]
display(full.head() )<categorify>
linear_svc = LinearSVC()
linear_svc.fit(X_train, y_train)
 Y_pred_lsvc = linear_svc.predict(X_test)
 Score_lsvc = accuracy_score(y_test,Y_pred_lsvc)
 print(Score_lsvc )
def parse_Cabin(cabin):
if type(cabin)== str:
m = re.search(r'([A-Z])+', cabin)
 return m.group(1)
 else:
return 'X'
full['Cabin_short'] = full['Cabin'].map(parse_Cabin )<categorify>
sgd = SGDClassifier()
sgd.fit(X_train, y_train)
 Y_pred_sgd = sgd.predict(X_test)
 Score_sgd = accuracy_score(y_test,Y_pred_sgd)
 print(Score_sgd )
dict_fare_by_Pclass = dict(full.groupby('Pclass' ).Fare.mean())
 print(dict_fare_by_Pclass)
 missing_fare = full.loc[full.Fare == 0,'Pclass'].map(dict_fare_by_Pclass)
 full.loc[full.Fare == 0,'Fare'] = missing_fare
missing_fare = full.loc[full.Fare.isnull() ,'Pclass'].map(dict_fare_by_Pclass)
 full.loc[full.Fare.isnull() ,'Fare'] = missing_fare
display(full.head(180)) 
<create_dataframe>
decision_tree = DecisionTreeClassifier()
decision_tree.fit(X_train, y_train)
 Y_pred_dtr = decision_tree.predict(X_test)
 Score_dtr = accuracy_score(y_test,Y_pred_dtr)
 print(Score_dtr )
features = pd.DataFrame()
features['Pclass'] = full['Pclass']
features['Fare'] = full['Fare']
features['Sex'] = full['Sex']<data_type_conversions>
random_forest = RandomForestClassifier(n_estimators=100)
 random_forest.fit(X_train, y_train)
 Y_pred_rf = random_forest.predict(X_test)
 Score_rf = accuracy_score(y_test,Y_pred_rf)
 print(Score_rf )
features['A5'] =(full['Ticket_short'] == 'A5' ).astype(int)
 features['PC'] =(full['Ticket_short'] == 'PC' ).astype(int)
 print(features )<define_variables>
models = pd.DataFrame({
'Model': ['Support Vector Machines', 'KNN', 'Logistic Regression',
'Random Forest', 'Naive Bayes', 'Perceptron',
'Stochastic Gradient Decent', 'Linear SVC',
'Decision Tree'],
'Score': [Score_svc, Score_knn, Score_lr,
Score_rf, Score_gnb, Score_per,
Score_sgd, Score_lsvc, Score_dtr]})
 models.sort_values(by='Score', ascending=False )
dict_Title = {"Capt": "Officer",
"Col": "Officer",
"Major": "Officer",
"Jonkheer": "Royalty",
"Don": "Royalty",
"Sir" : "Royalty",
"Dr": "Officer",
"Rev": "Officer",
"the Countess":"Royalty",
"Dona": "Royalty",
"Mme": "Mrs",
"Mlle": "Miss",
"Ms": "Mrs",
"Mr" : "Mr",
"Mrs" : "Mrs",
"Miss" : "Miss",
"Master" : "Master",
"Lady" : "Royalty"
}
title = title.map(dict_Title)
 plt.figure(figsize =(14,6))
sns.violinplot(x = title, y = full['Age']);<categorify>
Y_pred = linear_svc.predict(test_df )
df_title = pd.DataFrame(title ).join(full[['Age','Survived']])
 dict_age = df_title.groupby('Name' ).Age.mean()
idx = full.Age.isnull()
full.loc[idx,'Age'] = df_title.loc[idx, 'Name'].map(dict_age)
 display(full.head(6))<data_type_conversions>
submission = pd.read_csv('.. /input/titanic/gender_submission.csv')
 submission['Survived'] = Y_pred
submission.to_csv('submission.csv', index=False )