Kaggle competition: Titanic - Machine Learning from Disaster
<SOS> metric: categorizationaccuracy Kaggle data source: titanic-machine-learning-from-disaster<drop_column>
init_notebook_mode(connected=True )
del x_train['matchType']
del x_test['matchType']<set_options>
%matplotlib inline
del df_train; del df_test
gc.collect()<split>
df_train = pd.read_csv('.. /input/titanic/train.csv')
 df_test = pd.read_csv('.. /input/titanic/test.csv')
 df_sub = pd.read_csv('.. /input/titanic/gender_submission.csv' )
folds = KFold(n_splits=3,random_state=6)
 oof_preds = np.zeros(x_train.shape[0])
 sub_preds = np.zeros(x_test.shape[0])
 start = time.time()
valid_score = 0
importances = pd.DataFrame()
for n_fold,(trn_idx, val_idx)in enumerate(folds.split(x_train, y_train)) :
trn_x, trn_y = x_train.iloc[trn_idx], y_train[trn_idx]
val_x, val_y = x_train.iloc[val_idx], y_train[val_idx]
train_data = lgb.Dataset(data=trn_x, label=trn_y)
 valid_data = lgb.Dataset(data=val_x, label=val_y)
 params = {"objective" : "regression", "metric" : "mae", 'n_estimators':10000, 'early_stopping_rounds':100,
"num_leaves" : 30, "learning_rate" : 0.3, "bagging_fraction" : 0.9,
"bagging_seed" : 0}
lgb_model = lgb.train(params, train_data, valid_sets=[train_data, valid_data], verbose_eval=1000)
 oof_preds[val_idx] = lgb_model.predict(val_x, num_iteration=lgb_model.best_iteration)
 oof_preds[oof_preds>1] = 1
oof_preds[oof_preds<0] = 0
sub_pred = lgb_model.predict(x_test, num_iteration=lgb_model.best_iteration)/ folds.n_splits
sub_pred[sub_pred>1] = 1
sub_pred[sub_pred<0] = 0
sub_preds += sub_pred
print('Fold %2d RMSE : %.6f' %(n_fold + 1, mean_absolute_error(val_y, oof_preds[val_idx])))
 valid_score += mean_absolute_error(val_y, oof_preds[val_idx] )<train_model>
df_train.dtypes.value_counts()
print('Done' )<save_to_csv>
df_test.dtypes.value_counts()
test_pred = pd.DataFrame({"Id":test_idx})
 test_pred["winPlacePerc"] = sub_preds
test_pred.columns = ["Id", "winPlacePerc"]
test_pred.to_csv("lgb_base_model.csv", index=False )<train_model>
df_train["Survived"].value_counts()
print('Done' )<save_to_csv>
missing_rate_train =(df_train.isna().sum() /df_train.shape[0] ).sort_values()
nb_missing = df_train.isna().sum().sort_values()
print(f'{"Variable" :-<40} {"missing_rate_train":-<30} {"Number of missing values":-<30}')
 for n in range(len(missing_rate_train)) :
print(f'{missing_rate_train.index[n] :-<30} {missing_rate_train[n]:-<30} {nb_missing[n]:-<30}' )
sub = pd.read_csv('.. /input/submission2/submission.csv')
 sub.to_csv('practice1.csv',index=False )<save_to_csv>
missing_rate_test =(df_test.isna().sum() /df_test.shape[0] ).sort_values()
nb_missing = df_test.isna().sum().sort_values()
print(f'{"Variable" :-<30} {"missing_rate_train":-<30} {"Number of missing values":-<30}')
 for n in range(len(missing_rate_test)) :
print(f'{missing_rate_test.index[n] :-<30} {missing_rate_test[n]:-<30} {nb_missing[n]:-<30}' )
sub = pd.read_csv('.. /input/pleasetry1/sub1.csv')
 sub.to_csv('sub1.csv',index=False )<import_modules>
def TransfromAge(df_aux):
GroupAge = ['inf-10', '10-18', '18-35', '35-65', 'sup-65']
cond1 =(df_aux["Age"].isnull())&(df_aux["title"]=="Master")
 df_aux.loc[cond1, 'Age'] = calcul_median(df_aux,"Master")
 cond2 =(df_aux["Age"].isnull())&(df_aux["title"]=="Miss")
 df_aux.loc[cond2, 'Age'] = calcul_median(df_aux,"Miss")
 cond3 =(df_aux["Age"].isnull())&(df_aux["title"]=="Mrs")
 df_aux.loc[cond3, 'Age'] = calcul_median(df_aux,"Mrs")
 cond4 =(df_aux["Age"].isnull())&(df_aux["title"]=="Mr")
 df_aux.loc[cond4, 'Age'] = calcul_median(df_aux,"Mr")
 cond5 =(df_aux["Age"].isnull())&(df_aux["title"]=="officier/capitaine")
 df_aux.loc[cond5, 'Age'] = calcul_median(df_aux,"officier/capitaine")
 cond6 =(df_aux["Age"].isnull())&(df_aux["title"]=="royale")
 df_aux.loc[cond6, 'Age'] = calcul_median(df_aux,"royale")
 bins = [0, 10, 18, 35, 65, np.inf]
GroupAge = ['inf-10', '10-18', '18-35', '35-65', 'sup-65']
df_aux['GroupAge'] = pd.cut(df_aux['Age'], bins, labels=GroupAge)
 return df_aux
def calcul_median(df_aux,ch):
return df_aux[df_aux["title"]==ch].Age.dropna().median()
import numpy as np
import pandas as pd<save_to_csv>
df_test[df_test["Fare"].isnull() ]
sub = pd.read_csv('.. /input/pleasetry1/sub1.csv')
 sub.to_csv('sub1.csv',index=False )<import_modules>
def TransfromFare(df_aux):
df_aux.loc[(df_aux["Fare"].isnull())&(df_aux["Pclass"]==3), 'Fare'] = df_aux[df_aux["Pclass"]==3].Fare.dropna().median()
bins = [-1,8,14,20,60,100,600]
GroupFare = ['0-8£','8-14£','14-20£','20-60£','60-100£','100-515£']
df_aux['GroupFare'] = pd.cut(df_aux['Fare'], bins, labels=GroupFare)
 return df_aux
import numpy as np
import pandas as pd 
<save_to_csv>
def TransfromCabin(df_aux):
df_aux.loc[(df_aux["Cabin"].isnull()), 'HasOrNotCabinNumber'] = "Has Not Cabin Number"
df_aux.loc[(df_aux["Cabin"].notnull()), 'HasOrNotCabinNumber'] = "Has Cabin Number"
return df_aux.drop(columns=["Cabin","Ticket"] )
sub = pd.read_csv('.. /input/pleasetry2/sub2.csv')
 sub.to_csv('sub2.csv',index=False )<import_modules>
df_train[df_train["Embarked"].isnull() ]
import numpy as np
import pandas as pd<save_to_csv>
def TransfromEmbarked(df_aux):
df_aux.loc[(df_aux["Embarked"].isnull())&(df_aux["Pclass"]==1), 'Embarked'] = "S"
return df_aux
sub = pd.read_csv('.. /input/pleasetry3/sub3.csv')
 sub.to_csv('sub3.csv',index=False )<import_modules>
def TransfromFamiliy(df_aux):
df_aux["familiySize"] = df_aux["SibSp"] + df_aux["Parch"] + 1
AloneTravel =(df_aux['SibSp'] == 0)&(df_aux['Parch'] == 0)
 CoupleTravel =(df_aux['SibSp'] == 0)&(df_aux['Parch'] == 1)
 siblingsTravel =(df_aux['SibSp'] == 1)&(df_aux['Parch'] == 0)
 SmallFamilly =(df_aux['familiySize'] <= 3)&(df_aux['familiySize'] >= 2)
 BigFamilly =(df_aux['familiySize'] >3)
 df_aux.loc[AloneTravel, 'familiy'] = "Alone Travel"
df_aux.loc[CoupleTravel, 'familiy'] = "Couple Travel"
df_aux.loc[siblingsTravel, 'familiy'] = "siblings Travel"
df_aux.loc[SmallFamilly, 'familiy'] = "Small Familly"
df_aux.loc[BigFamilly, 'familiy'] = "Big Familly"
return df_aux
import numpy as np
import pandas as pd<save_to_csv>
dfTrain = TransfromTitle(df_train)
 dfTrain = TransfromAge(dfTrain)
 dfTrain = TransfromFare(dfTrain)
 dfTrain = TransfromCabin(dfTrain)
 dfTrain = TransfromEmbarked(dfTrain)
 dfTrain = TransfromFamiliy(dfTrain )
sub = pd.read_csv('.. /input/pleasetry4/sub3.csv')
 sub.to_csv('sub4.csv',index=False )<import_modules>
dfTrain = dfTrain[['familiySize','SibSp','Parch','title','GroupAge','GroupFare','HasOrNotCabinNumber',
'Sex','Embarked','Pclass','familiy','Survived']]
import numpy as np
import pandas as pd<save_to_csv>
dfTest = TransfromTitle(df_test)
 dfTest = TransfromAge(dfTest)
 dfTest = TransfromFare(dfTest)
 dfTest = TransfromCabin(dfTest)
 dfTest = TransfromEmbarked(dfTest)
 dfTest = TransfromFamiliy(dfTest )
sub = pd.read_csv('.. /input/pleasetry5/sub5.csv')
 sub.to_csv('sub5.csv',index=False )<import_modules>
dfTest = dfTest[['familiySize','SibSp','Parch','title','GroupAge','GroupFare','HasOrNotCabinNumber',
'Sex','Embarked','Pclass','familiy']]
import numpy as np
import pandas as pd<save_to_csv>
from sklearn.preprocessing import LabelEncoder, OneHotEncoder
from sklearn.impute import SimpleImputer
from sklearn.pipeline import make_pipeline
from sklearn.model_selection import GridSearchCV, train_test_split
from sklearn.impute import KNNImputer
from sklearn.preprocessing import MinMaxScaler, StandardScaler, RobustScaler
sub = pd.read_csv('.. /input/pleasetry6/sub5.csv')
 sub.to_csv('sub5.csv',index=False )<import_modules>
X_train = dfTrain.drop(columns = ['Survived'] ).values
y = dfTrain.Survived.values
X_test = dfTest.values
import numpy as np
import pandas as pd<save_to_csv>
for i in range(len(X_train)) :
X_train[i,9] = str(X_train[i,9])
 X_train[:,0:3] = StandardScaler().fit_transform(X_train[:,0:3])
 onehotencoder_1 = OneHotEncoder()
u1 = onehotencoder_1.fit_transform(X_train[:,3:] ).toarray()
X_train2 = np.concatenate(( X_train[:,0:3], u1), axis=1)
 X_train2.shape
sub = pd.read_csv('.. /input/lastTry1/sub6.csv')
 sub.to_csv('sub6.csv',index=False )<save_to_csv>
StandardScaler().fit_transform(X_test[:,0:3] ).shape
X_test[:,0:3].shape
sub = pd.read_csv('.. /input/lasttry1/sub6.csv')
 sub.to_csv('sub6.csv',index=False )<save_to_csv>
for i in range(len(X_test)) :
X_test[i,9] = str(X_test[i,9])
 X_test[:,0:3] = StandardScaler().fit_transform(X_test[:,0:3])
 onehotencoder_2 = OneHotEncoder()
u2 = onehotencoder_2.fit_transform(X_test[:,3:] ).toarray()
X_test2 = np.concatenate(( X_test[:,0:3], u2), axis=1)
 X_test2.shape
sub = pd.read_csv('.. /input/lasttry1/sub6_avg.csv')
 sub.to_csv('sub6_avg.csv',index=False )<load_from_csv>
from sklearn.pipeline import make_pipeline
from sklearn.ensemble import RandomForestClassifier
from sklearn.ensemble.gradient_boosting import GradientBoostingClassifier
from sklearn.feature_selection import SelectKBest
from sklearn.model_selection import StratifiedKFold
from sklearn.model_selection import GridSearchCV
from sklearn.model_selection import cross_val_score
from sklearn.feature_selection import SelectFromModel
from sklearn.linear_model import LogisticRegression, LogisticRegressionCV
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.naive_bayes import GaussianNB
from sklearn.svm import SVC
from sklearn.model_selection import GridSearchCV
import xgboost as xgb
inittime = dt.datetime.now()
print("Capturing train database...")
 df_train = pd.read_csv(".. /input/train_V2.csv")
 print("Train Database captured in ", dt.datetime.now() - inittime, " secs")
 df_train= reduce_mem_usage(df_train)
 inittime = dt.datetime.now()
print("Capturing Test database...")
 df_test = pd.read_csv(".. /input/test_V2.csv")
 print("Test Database captured in ", dt.datetime.now() - inittime, " secs")
 df_test= reduce_mem_usage(df_test)
 print("Train data set :
",df_train.head())
 print("Test data set :
", df_test.head() )<feature_engineering>
logreg = LogisticRegression()
Gauss = GaussianNB()
rf = RandomForestClassifier()
gboost = GradientBoostingClassifier()
DTC = DecisionTreeClassifier()
RF = RandomForestClassifier(n_estimators=200)
 SVectorMachine = SVC()
xgb = xgb.XGBClassifier(max_depth=3, n_estimators=10, learning_rate=0.01)
 models = [logreg,Gauss, gboost,DTC,RF,SVectorMachine,xgb]
df_train.loc[df_train['winPlacePerc'].isna() ==True, 'winPlacePerc']= 0.5<count_values>
def compute_score(clf, X, y, scoring='accuracy'):
xval = cross_val_score(clf, X, y, cv = 10, scoring=scoring)
 return np.mean(xval )
df_train['matchType'].value_counts()<feature_engineering>
for model in models:
print('Cross-validation of : {0}'.format(model.__class__))
score = compute_score(clf=model, X=X_train2, y=y, scoring='accuracy')
 print('CV score = {0}'.format(score))
print('----->>>>>>' )
df_test['winPlacePerc']= 0.0
df_train['Type']= 'Train'
df_test['Type']= 'Test'
print(df_train.shape, df_test.shape )<concatenate>
X = np.asarray(X_train2 ).astype(np.float32)
 Y = np.asarray(y ).astype(np.float32 )
df= pd.concat([df_train, df_test], ignore_index=True)
 del df_train
print(df.shape )<drop_column>
classifier = Sequential()
classifier.add(Dense(units = 35,activation = "relu",kernel_initializer="uniform",input_dim=33))
classifier.add(Dropout(rate=0.1))
classifier.add(Dense(units = 20,activation = "relu",kernel_initializer="uniform"))
classifier.add(Dense(units = 15,activation = "relu",kernel_initializer="uniform"))
classifier.add(Dense(units = 1,activation = "sigmoid",kernel_initializer="uniform"))
classifier.compile(optimizer="rmsprop",loss="binary_crossentropy",metrics=['acc'])
 classifier.fit(X,Y,batch_size=50,epochs=110 )
df= reduce_mem_usage(df )<merge>
result = []
Y_pred = classifier.predict(np.asarray(X_test2 ).astype(np.float32))
Y_pred =(Y_pred>0.55)
 for i in range(len(Y_pred)) :
if Y_pred[i][0] == True :
result.append(1)
 else :
result.append(0)
 PassengerId = df_test["PassengerId"]
SurvivedResult = pd.DataFrame({'Survived': result})
 results = pd.concat([PassengerId,SurvivedResult],axis=1)
 results.to_csv("gender_submission.csv",sep = ',',index=False )