Kaggle competition: Titanic - Machine Learning from Disaster
<SOS> metric: categorizationaccuracy Kaggle data source: titanic-machine-learning-from-disaster<data_type_conversions>
training_data = pd.read_csv(".. /input/titanic/train.csv")
 test_data = pd.read_csv(".. /input/titanic/test.csv")
 gender_data = pd.read_csv(".. /input/titanic/gender_submission.csv")
train['date_account_created'] = pd.to_datetime(train['date_account_created'])
 train['date_first_booking'] = pd.to_datetime(train['date_first_booking'])
 train['timestamp_first_active'] = pd.to_datetime(train['timestamp_first_active'], format='%Y%m%d%H%M%S')
 train['timestamp_first_active'] = pd.to_datetime(train['timestamp_first_active'] ).dt.date
test['date_account_created'] = pd.to_datetime(test['date_account_created'])
 test['timestamp_first_active'] = pd.to_datetime(test['timestamp_first_active'], format='%Y%m%d%H%M%S')
 test['timestamp_first_active'] = pd.to_datetime(test['timestamp_first_active'] ).dt.date 
<drop_column>
X_train = training_data.drop(['Survived'], axis=1)
 y_train = training_data["Survived"]
X_test = test_data.copy()
y_test = gender_data['Survived']
X_train = X_train.reindex(columns=["Pclass", "Sex", "Age", "SibSp", "Parch", "Fare", "Embarked", "Name", "Ticket", "Cabin", "PassengerId"])
 X_test = X_test.reindex(columns=["Pclass", "Sex", "Age", "SibSp", "Parch", "Fare", "Embarked", "Name", "Ticket", "Cabin", "PassengerId"] )
train.drop(['date_first_booking'], axis=1,inplace=True)
 test.drop(['date_first_booking'], axis=1,inplace=True )<feature_engineering>
def missing_data(X):
total_missing_values = X.isnull().sum().sort_values(ascending=False)
 percent_1 = X.isnull().sum() /X.isnull().count() *100
percent_2 =(round(percent_1, 1)).sort_values(ascending=False)
 missing_data = pd.concat([total_missing_values, percent_2], axis=1, keys=['Total', '%'])
 print(missing_data.head(5))
print("----- Before cleaning -----")
 print(missing_data(X_train))
print(missing_data(X_test))
dac = np.vstack(train.date_account_created.astype(str ).apply(lambda x: list(map(int, x.split('-')))).values)
 train['dac_year'] = dac[:,0]
train['dac_month'] = dac[:,1]
train['dac_day'] = dac[:,2]
train.drop(['date_account_created'], axis=1,inplace=True)
 tfa = np.vstack(train.timestamp_first_active.astype(str ).apply(lambda x: list(map(int, x.split('-')))).values)
 train['tfa_year'] = tfa[:,0]
train['tfa_month'] = tfa[:,1]
train['tfa_day'] = tfa[:,2]
train.drop(['timestamp_first_active'], axis=1,inplace=True)
 dac = np.vstack(test.date_account_created.astype(str ).apply(lambda x: list(map(int, x.split('-')))).values)
 test['dac_year'] = dac[:,0]
test['dac_month'] = dac[:,1]
test['dac_day'] = dac[:,2]
test.drop(['date_account_created'], axis=1,inplace=True)
 tfa = np.vstack(test.timestamp_first_active.astype(str ).apply(lambda x: list(map(int, x.split('-')))).values)
 test['tfa_year'] = tfa[:,0]
test['tfa_month'] = tfa[:,1]
test['tfa_day'] = tfa[:,2]
test.drop(['timestamp_first_active'], axis=1,inplace=True )<categorify>
datasets = [X_train, X_test]
titles = {"Mr": 1, "Miss": 2, "Mrs": 3, "Master": 4, "Rare": 5}
for dataset in datasets:
dataset['Title'] = dataset['Name'].str.extract('([A-Za-z]+)\.', expand=False)
 dataset['Title'] = dataset['Title'].replace(['Don', 'Rev', 'Dr', 'Major', 'Sir', 'Col', 'Capt', 'Jonkheer', 'Countess', 'Lady', 'Dona'], 'Rare')
 dataset['Title'] = dataset['Title'].replace(['Ms', 'Mlle'], 'Miss')
 dataset['Title'] = dataset['Title'].replace('Mme', 'Miss')
 dataset['Title'] = dataset['Title'].map(titles)
 dataset['Title'] = dataset['Title'].astype(int)
 dataset.drop('Name', axis=1, inplace=True )
le = LabelEncoder()
train['gender']= le.fit_transform(train['gender'])
 train['signup_method']= le.fit_transform(train['signup_method'])
 train['first_affiliate_tracked']= le.fit_transform(train['first_affiliate_tracked'])
 train['signup_method']= le.fit_transform(train['signup_method'])
 train['language']= le.fit_transform(train['language'])
 train['affiliate_channel']= le.fit_transform(train['affiliate_channel'])
 train['affiliate_provider']= le.fit_transform(train['affiliate_provider'])
 train['signup_app']= le.fit_transform(train['signup_app'])
 train['first_device_type']= le.fit_transform(train['first_device_type'])
 train['first_browser']= le.fit_transform(train['first_browser'])
 train['signup_flow']= le.fit_transform(train['signup_flow'] )<categorify>
dep = {'A': 1, 'B': 2, 'C': 3, 'D': 4, 'E': 5, 'F': 6, 'G': 7, 'U': 8}
for dataset in datasets:
dataset['Cabin'] = dataset['Cabin'].fillna("U0")
 dataset['Deck'] = dataset['Cabin'].map(lambda x: re.compile('([a-zA-Z])+' ).search(x ).group())
 dataset['Deck'] = dataset['Deck'].map(dep)
 dataset['Deck'].fillna(0, inplace=True)
 dataset['Deck'] = dataset['Deck'].astype(int)
 dataset.drop('Cabin', axis=1, inplace=True )
le = LabelEncoder()
test['gender']= le.fit_transform(test['gender'])
 test['signup_method']= le.fit_transform(test['signup_method'])
 test['first_affiliate_tracked']= le.fit_transform(test['first_affiliate_tracked'])
 test['signup_method']= le.fit_transform(test['signup_method'])
 test['language']= le.fit_transform(test['language'])
 test['affiliate_channel']= le.fit_transform(test['affiliate_channel'])
 test['affiliate_provider']= le.fit_transform(test['affiliate_provider'])
 test['signup_app']= le.fit_transform(test['signup_app'])
 test['first_device_type']= le.fit_transform(test['first_device_type'])
 test['first_browser']= le.fit_transform(test['first_browser'])
 test['signup_flow']= le.fit_transform(test['signup_flow'] )<categorify>
for dataset in datasets:
dataset['relatives'] = dataset['SibSp'] + dataset['Parch']
dataset.loc[dataset['relatives'] > 0, 'is_alone'] = 0
dataset.loc[dataset['relatives'] == 0, 'is_alone'] = 1
dataset['is_alone'] = dataset['is_alone'].astype(int )
train.country_destination.replace('NDF',0,inplace=True)
 train.country_destination.replace('US',1,inplace=True)
 train.country_destination.replace('other',2,inplace=True)
 train.country_destination.replace('FR',3,inplace=True)
 train.country_destination.replace('CA',4,inplace=True)
 train.country_destination.replace('GB',5,inplace=True)
 train.country_destination.replace('ES',6,inplace=True)
 train.country_destination.replace('IT',7,inplace=True)
 train.country_destination.replace('PT',8,inplace=True)
 train.country_destination.replace('NL',9,inplace=True)
 train.country_destination.replace('DE',10,inplace=True)
 train.country_destination.replace('AU',11,inplace=True )<split>
for dataset in datasets:
dataset.loc[(dataset.Sex == "male"), 'Sex'] = 0
dataset.loc[(dataset.Sex == "female"), 'Sex'] = 1
y=train['country_destination']
X=train.drop(['country_destination','id'],axis=1)
 smotetomek = SMOTETomek(sampling_strategy='auto')
 X_train1, X_test1, y_train1, y_test1 = train_test_split(X, y, test_size=0.30, random_state=1,shuffle=True,stratify=y)
 X_train, y_train = smotetomek.fit_resample(X_train1, y_train1 )<create_dataframe>
for dataset in datasets:
dataset["Embarked"].fillna("S", inplace=True)
 dataset.loc[(dataset.Embarked == "S"), "Embarked"] = 0
dataset.loc[(dataset.Embarked == "Q"), "Embarked"] = 1
dataset.loc[(dataset.Embarked == "C"), "Embarked"] = 2
x=pd.DataFrame(X_train)
 Y=pd.DataFrame(y_train)
 result = pd.concat([x, Y], axis=1, join='inner')
 sns.countplot(result.country_destination )<choose_model_class>
for dataset in datasets:
Age_avg = dataset['Age'].mean()
Age_std = dataset['Age'].std()
Age_null_count = dataset['Age'].isnull().sum()
Age_null_random_list = np.random.randint(Age_avg - Age_std, Age_avg + Age_std, size=Age_null_count)
 dataset['Age'][np.isnan(dataset['Age'])] = Age_null_random_list
dataset['Age'] = dataset['Age'].astype(int)
 dataset.loc[(dataset.Age <= 18), 'Age'] = 0
dataset.loc[(dataset.Age > 18)&(dataset.Age <= 23), 'Age'] = 1
dataset.loc[(dataset.Age > 23)&(dataset.Age <= 29), 'Age'] = 2
dataset.loc[(dataset.Age > 29)&(dataset.Age <= 35), 'Age'] = 3
dataset.loc[(dataset.Age > 35)&(dataset.Age <= 42), 'Age'] = 4
dataset.loc[(dataset.Age > 42), 'Age'] = 5
target_names = ['NDF', 'US', 'other', 'FR', 'CA', 'GB', 'ES', 'IT', 'PT', 'NL','DE', 'AU']
rf=RandomForestClassifier()
rf.fit(X_train,y_train)
 y_predrf=rf.predict(X_test1)
 print(classification_report(y_test1, y_predrf, target_names=target_names))<define_variables>
X_test['Fare'].fillna(X_test['Fare'].mean() , inplace=True)
 for dataset in datasets:
dataset.loc[(dataset.Fare <= 7.775), 'Fare'] = 0
dataset.loc[(dataset.Fare > 7.775)&(dataset.Fare <= 8.662), 'Fare'] = 1
dataset.loc[(dataset.Fare > 8.662)&(dataset.Fare <= 14.454), 'Fare'] = 2
dataset.loc[(dataset.Fare > 14.454)&(dataset.Fare <= 26), 'Fare'] = 3
dataset.loc[(dataset.Fare > 26)&(dataset.Fare <= 52.369), 'Fare'] = 4
dataset.loc[(dataset.Fare > 52.369), 'Fare'] = 5
dataset['Fare'] = dataset['Fare'].astype(int)
 dataset.drop(['Ticket', 'PassengerId'], axis=1, inplace=True )
feature_names=list(X_train1.columns.values.tolist() )<define_variables>
print("----- After cleaning -----")
 print(missing_data(X_train))
print(missing_data(X_test))
pred_country={0:"NDF", 1:"US", 2:"other", 3:"FR", 4:"CA", 5:"GB", 6:"ES", 7:"IT", 8:"PT", 9:"DE", 10:"NL", 11:"AU"}<train_model>
SVC_model = SVC(gamma='scale')
 SVC_model.fit(X_train, y_train)
 SVC_score = round(accuracy_score(SVC_model.predict(X_train), y_train)* 100, 2)
 KNN_model = KNeighborsClassifier()
KNN_model.fit(X_train, y_train)
 KNN_score = round(accuracy_score(KNN_model.predict(X_train), y_train)* 100, 2)
 DC_model = DecisionTreeClassifier()
DC_model.fit(X_train, y_train)
 DC_score = round(accuracy_score(DC_model.predict(X_train), y_train)* 100, 2)
 XGB_model = XGBClassifier()
XGB_model.fit(X_train, y_train)
 XGB_score = round(accuracy_score(XGB_model.predict(X_train), y_train)* 100, 2)
 RF_model = RandomForestClassifier(n_estimators=100)
 RF_model.fit(X_train, y_train)
 RF_score = round(accuracy_score(RF_model.predict(X_train), y_train)* 100, 2)
 GNB_model = GaussianNB()
GNB_model.fit(X_train, y_train)
 GNB_score = round(accuracy_score(GNB_model.predict(X_train), y_train)* 100, 2 )
xgb = XGBClassifier()
xgb.fit(X_train,y_train)
 y_predxgb=xgb.predict(X_test1)
 print(classification_report(y_test1, y_predxgb, target_names=target_names)) 
<predict_on_test>
ranking = pd.DataFrame({'Model': ['Support Vector Classification', 'KNN', 'Decision Classifier', 'Random Forest', 'Naive Bayes', 'XGBClassifer'], 'Score': [SVC_score, KNN_score, DC_score, RF_score, GNB_score, XGB_score]})
 ranking = ranking.sort_values(by='Score', ascending=False ).set_index('Score')
 ranking
predictionsxgb=xgb.predict(test.drop(['id'],axis=1))<define_variables>
rf = RandomForestClassifier(n_estimators=100, oob_score=True, random_state=1, n_jobs=-1)
 rf.fit(X_train, y_train)
 print("Cross val score:", round(cross_val_score(rf, X_train, y_train, cv=8, scoring='accuracy' ).mean() , 3), '(+/-', round(cross_val_score(rf, X_train, y_train, cv=8, scoring='accuracy' ).std() , 2), '%)')
 print("Oob score:", round(rf.oob_score_, 4)*100, "%" )
resultsxgb=[]
for i in predictionsxgb:
resultsxgb.append(pred_country[i] )<predict_on_test>
rf = RandomForestClassifier(criterion="gini", min_samples_leaf=1, min_samples_split=35, n_estimators=1000, oob_score=True, random_state=1, n_jobs=-1)
 rf.fit(X_train, y_train)
 print("oob score:", round(rf.oob_score_, 4)*100, '%' )
mlp = MLPClassifier(activation='identity', solver='sgd',learning_rate='adaptive', alpha=0.0001, batch_size='auto')
 mlp.fit(X_train,y_train)
 predsmlp = mlp.predict(X_test1)
 print(classification_report(y_test1, predsmlp, target_names=target_names))<train_on_grid>
predictions = cross_val_predict(rf, X_train, y_train, cv=5)
 print("Precision:", precision_score(y_train, predictions))
print("Recall:", recall_score(y_train, predictions))
print("F1:", f1_score(y_train, predictions))
y_scores = rf.predict_proba(X_train)
 y_scores = y_scores[:, 1]
<compute_train_metric><EOS> 
r_a_score = roc_auc_score(y_train, y_scores)
 print("ROC-AUC-Score:", r_a_score)
 preds = rf.predict(X_test)
 submission = pd.DataFrame({'PassengerId': test_data['PassengerId'], 'Survived': preds})
 submission.to_csv('Titanic Predictions Final Submission.csv', index=False )