Kaggle competition: Titanic - Machine Learning from Disaster
<SOS> metric: categorizationaccuracy Kaggle data source: titanic-machine-learning-from-disaster<rename_columns>
%matplotlib inline
warnings.filterwarnings('ignore')
 plt.style.use("ggplot")
train.columns = [x.lower() for x in train.columns]
test.columns = [x.lower() for x in test.columns]<drop_column>
data.isnull().sum()
train.drop(['passengerid','name','ticket'],axis=1,inplace=True)
 train.head(2 )<count_missing_values>
data.isnull().sum()
train.isnull().sum()<count_missing_values>
data.groupby(['Survived'])['Embarked'].value_counts()
train.isnull().sum()<drop_column>
data['Embarked'].fillna('S', inplace=True)
 data['Embarked'].isnull().sum()
train.drop('cabin',axis=1,inplace=True)
 train.head()<count_missing_values>
def extract_title(x):
res = re.findall(r'[A-Za-z]+[.]', x)
 if res:
return res[0]
else:
return None
data['NameTitle'] = data['Name'].apply(extract_title)
 data.head(3 )
train.isnull().sum()<count_missing_values>
data.loc[data['Age'].isnull() & data['NameTitle'].str.contains('Master.'), 'Age'] = 5
data.loc[data['Age'].isnull() & data['NameTitle'].str.contains('Miss.'), 'Age'] = 22
data.loc[data['Age'].isnull() & data['NameTitle'].str.contains('Mr.'), 'Age'] = 32
data.loc[data['Age'].isnull() & data['NameTitle'].str.contains('Mrs.'), 'Age'] = 36
data.loc[data['Age'].isnull() & data['NameTitle'].str.contains('Other.'), 'Age'] = 46
data['Age'].isnull().any()
train.isnull().sum()<data_type_conversions>
data['NameTitle'].value_counts()
age_mean = train['age'].mean()
train['age'] = train['age'].fillna(age_mean)
 train.isnull().sum() 
<count_missing_values>
data.drop('PassengerId', axis=1, inplace=True)
 data.drop('Ticket', axis=1, inplace=True)
 data.drop('Cabin', axis=1, inplace=True)
 data.drop('Name', axis=1, inplace=True )
train.isnull().sum()<count_missing_values>
def age_group(x):
if x <= 16:
return 4
elif x > 16 and x <= 32:
return 1
elif x > 32 and x <= 48:
return 2
elif x > 48 and x <= 64:
return 3
else:
return 0
train.isnull().sum()<count_values>
data['AgeGroup'] = data['Age'].apply(age_group)
 data.head()
train['embarked'].value_counts()<filter>
data.drop('AgeBand', axis=1, inplace=True)
 data.drop('Age', axis=1, inplace=True)
 data.head()
train[train['embarked'].isnull() ]<categorify>
def fare_group(x):
if x <= 7.854:
return 0
elif x > 7.854 and x <= 10.5:
return 1
elif x > 10.5 and x <= 21.679:
return 2
elif x > 21.679 and x <= 39.688:
return 3
else:
return 4
<feature_engineering>
data['FareGroup'] = data['Fare'].apply(fare_group)
 data.head()
train['embarked'] = train['embarked'].fillna('S' )<count_missing_values>
data.drop('Fare', axis=1, inplace=True)
 data.drop('FareBand', axis=1, inplace=True)
 data.head()
train.isnull().sum()<count_missing_values>
data['Embarked'] = data['Embarked'].map({'S': 0, 'Q': 1, 'C': 2} ).astype(int)
 data['Sex'] = data['Sex'].map({'male': 0, 'female': 1} ).astype(int)
 data['NameTitle'] = data['NameTitle'].map({'Mr.': 0, 'Other.': 1, 'Master.': 2, 'Miss.': 3, 'Mrs.': 4} ).astype(int)
 data.head()
train.isnull().sum()<count_values>
data.drop('Sex', axis=1, inplace=True)
 data.head()
train['survived'].value_counts()<sort_values>
data['FamilySize'] = data['SibSp'] + data['Parch']
data.head()
train[['pclass','survived']].groupby(['pclass'],as_index=False ).mean().sort_values(by='survived',ascending=False )<sort_values>
data.drop('SibSp', axis=1, inplace=True)
 data.drop('Parch', axis=1, inplace=True )
train[['sex','survived']].groupby(['sex'],as_index=False ).mean().sort_values(by='survived',ascending=False )<count_values>
X = data[data.columns[1:]]
y = data[data.columns[0]]
X_train, X_val, y_train, y_val = train_test_split(X, y, test_size=0.2, random_state=42 )
train.sibsp.value_counts()<sort_values>
label = ['Linear SVM', 'Radial SVM', 'Decision Tree', 'Random Forest', 'Logistic Regression']
models = [svm.LinearSVC() , svm.SVC() , DecisionTreeClassifier() , RandomForestClassifier(n_estimators=100), LogisticRegression() ]
accuracy = []
fscore = []
for model in models:
model.fit(X_train, y_train)
 prediction = model.predict(X_val)
 accuracy.append(metrics.accuracy_score(prediction, y_val))
fscore.append(metrics.f1_score(y_val, prediction))
model_df = pd.DataFrame({
'accuracy': accuracy,
'fscore': fscore
}, index=label)
 model_df
train[["sibsp", "survived"]].groupby(['sibsp'], as_index=False ).mean().sort_values(by='survived', ascending=False )<count_values>
cv_accuracy = []
cv_std = []
kfold = KFold(n_splits=10, random_state=23)
 for model in models:
cv_result = cross_val_score(model, X, y, cv=kfold)
 cv_accuracy.append(cv_result.mean())
 cv_std.append(cv_result.std())
 model_df['cv_accuracy'] = cv_accuracy
model_df['cv_std'] = cv_std
model_df
train.parch.value_counts()<sort_values>
estimators = [10, 100, 500, 1000]
criterions = ['gini', 'entropy']
bootstraps = [True, False]
parameters = {'n_estimators': estimators, 'criterion': criterions, 'bootstrap': bootstraps}
gd = GridSearchCV(RandomForestClassifier(random_state=23), parameters)
 gd.fit(X, y)
 print("Best Score: ", gd.best_score_)
 print("Best Model: ", gd.best_estimator_ )
train[["parch", "survived"]].groupby(['parch'], as_index=False ).mean().sort_values(by='survived', ascending=False )<categorify>
n_estimators = list(range(100,1100,100))
learn_rate = [0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1]
hyper={'n_estimators':n_estimators,'learning_rate':learn_rate}
gd=GridSearchCV(estimator=AdaBoostClassifier(random_state=23),param_grid=hyper,verbose=True)
 gd.fit(X, y)
 print("Best Score: ", gd.best_score_)
 print("Best Model: ", gd.best_estimator_ )
sex = pd.get_dummies(train['sex'],drop_first=True)
 sex.head()<categorify>
n_estimators = list(range(100,1100,100))
learn_rate = [0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1]
depth = [1,2,3]
hyper={'n_estimators':n_estimators,'learning_rate':learn_rate,'max_depth':depth}
gd=GridSearchCV(estimator=GradientBoostingClassifier(random_state=23),param_grid=hyper,verbose=True)
 gd.fit(X, y)
 print("Best Score: ", gd.best_score_)
 print("Best Model: ", gd.best_estimator_ )
pclass = pd.get_dummies(train['pclass'],drop_first= True)
 pclass.head()<categorify>
n_estimators = list(range(100,1100,100))
learn_rate = [0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1]
gd=GridSearchCV(estimator=XGBClassifier(random_state=23),param_grid=hyper,verbose=True)
 gd.fit(X, y)
 print("Best Score: ", gd.best_score_)
 print("Best Model: ", gd.best_estimator_ )
embarked = pd.get_dummies(train['embarked'],drop_first= True)
 embarked.head()<concatenate>
f,(ax1, ax2)= plt.subplots(2,2,figsize=(15,7))
model1 = RandomForestClassifier(bootstrap=True, class_weight=None, criterion='entropy',
max_depth=None, max_features='auto', max_leaf_nodes=None,
min_impurity_decrease=0.0, min_impurity_split=None,
min_samples_leaf=1, min_samples_split=2,
min_weight_fraction_leaf=0.0, n_estimators=100, n_jobs=1,
oob_score=False, random_state=23, verbose=0, warm_start=False)
 model1.fit(X,y)
 pd.Series(model1.feature_importances_, X.columns ).sort_values(ascending=True ).plot.barh(width=0.8,ax=ax1[0])
 ax1[0].set_title('Feature Importance in Random Forests')
 model2 = AdaBoostClassifier(algorithm='SAMME.R', base_estimator=None,
learning_rate=0.1, n_estimators=100, random_state=23)
 model2.fit(X,y)
 pd.Series(model2.feature_importances_, X.columns ).sort_values(ascending=True ).plot.barh(width=0.8,ax=ax1[1])
 ax1[1].set_title('Feature Importance in AdaBoost')
 model3 = GradientBoostingClassifier(criterion='friedman_mse', init=None,
learning_rate=0.5, loss='deviance', max_depth=2,
max_features=None, max_leaf_nodes=None,
min_impurity_decrease=0.0, min_impurity_split=None,
min_samples_leaf=1, min_samples_split=2,
min_weight_fraction_leaf=0.0, n_estimators=300,
presort='auto', random_state=23, subsample=1.0, verbose=0,
warm_start=False)
 model3.fit(X,y)
 pd.Series(model3.feature_importances_, X.columns ).sort_values(ascending=True ).plot.barh(width=0.8,ax=ax2[0])
 ax2[0].set_title('Feature Importance in Gradient Boosting')
 model4 = XGBClassifier(base_score=0.5, booster='gbtree', colsample_bylevel=1,
colsample_bytree=1, gamma=0, learning_rate=0.3, max_delta_step=0,
max_depth=3, min_child_weight=1, missing=None, n_estimators=200,
n_jobs=1, nthread=None, objective='binary:logistic', random_state=23,
reg_alpha=0, reg_lambda=1, scale_pos_weight=1, seed=None,
silent=True, subsample=1)
 model4.fit(X,y)
 pd.Series(model4.feature_importances_, X.columns ).sort_values(ascending=True ).plot.barh(width=0.8,ax=ax2[1])
 ax2[1].set_title('Feature Importance in XGBoost' )
final_train = pd.concat([train,sex,pclass,embarked],axis = 1)
 final_train.head()<drop_column>
test = pd.read_csv('.. /input/test.csv')
 test.head()
final_train = final_train.drop(['sex','embarked','pclass'],axis = 1 )<rename_columns>
test.isnull().sum()
final_train.columns = ['survived','age','sibsp','parch','fare','sex_male','pclass_2','pclass_3','embarked_q','embarked_s']
final_train.head()<count_missing_values>
test.isnull().sum()
test.isnull().sum()<count_missing_values>
test['NameTitle'] = test['Name'].apply(extract_title)
 fix_title(test)
 test.loc[test['Age'].isnull() & test['NameTitle'].str.contains('Master.'), 'Age'] = 5
test.loc[test['Age'].isnull() & test['NameTitle'].str.contains('Miss.'), 'Age'] = 22
test.loc[test['Age'].isnull() & test['NameTitle'].str.contains('Mr.'), 'Age'] = 32
test.loc[test['Age'].isnull() & test['NameTitle'].str.contains('Mrs.'), 'Age'] = 36
test.loc[test['Age'].isnull() & test['NameTitle'].str.contains('Other.'), 'Age'] = 46
test['Age'].isnull().any()
test['AgeGroup'] = test['Age'].apply(age_group)
 test['Fare'].fillna(test['Fare'].dropna().median() , inplace=True)
 test['FareGroup'] = test['Fare'].apply(fare_group)
 test['Embarked'] = test['Embarked'].map({'S': 0, 'Q': 1, 'C': 2} ).astype(int)
 test['Sex'] = test['Sex'].map({'male': 0, 'female': 1} ).astype(int)
 test['NameTitle'] = test['NameTitle'].map({'Mr.': 0, 'Other.': 1, 'Master.': 2, 'Miss.': 3, 'Mrs.': 4, 'Dona.': 1} ).astype(int )
test.isnull().sum()<data_type_conversions>
test['FamilySize'] = test['SibSp'] + test['Parch']
test.head()
test.drop(['passengerid','name','ticket'],axis=1,inplace=True)
 test.drop('cabin',axis=1,inplace=True)
 age_mean = test['age'].mean()
test['age'] = test['age'].fillna(age_mean)
 test.isnull().sum() 
<feature_engineering>
test_id = test[test.columns[0]]
test.drop('PassengerId', axis=1, inplace=True)
 test.drop('Name', axis=1, inplace=True)
 test.drop('Sex', axis=1, inplace=True)
 test.drop('Age', axis=1, inplace=True)
 test.drop('Ticket', axis=1, inplace=True)
 test.drop('Fare', axis=1, inplace=True)
 test.drop('Cabin', axis=1, inplace=True)
 test.drop('SibSp', axis=1, inplace=True)
 test.drop('Parch', axis=1, inplace=True)
 test.head()
test['fare'] = test['fare'].fillna(test['fare'].mean() )<count_missing_values>
prediction = model3.predict(test)
 submission = pd.DataFrame({
"PassengerId": test_id,
"Survived": prediction
} )
test.isnull().sum()<count_missing_values>
submission.to_csv('submission.csv', index=False )
<categorify><EOS> 
submission.to_csv('submission.csv', index=False )