Kaggle competition: Titanic - Machine Learning from Disaster
print('_'*40)
 test_df.isnull().sum()<sort_values>
titanic = pd.read_csv(".. /input/train.csv")
 titanic_t = pd.read_csv(".. /input/test.csv" )
train_df[['Pclass', 'Survived']].groupby(['Pclass'], as_index=False ).mean().sort_values(by='Survived',
ascending=False )<sort_values>
titanic['Sex'].value_counts()
train_df[["Sex", "Survived"]].groupby(['Sex'], as_index=False ).mean().sort_values(by='Survived',
ascending=False )<sort_values>
titanic = titanic.drop(['Cabin'], axis=1)
 titanic_t = titanic_t.drop(['Cabin'], axis=1 )
train_df[["SibSp", "Survived"]].groupby(['SibSp'], as_index=False ).mean().sort_values(by='Survived',
ascending=False )<sort_values>
def impute_age(cols):
Age = cols[0]
Pclass = cols[1]
if pd.isnull(Age):
if Pclass == 1:
return 37
elif Pclass == 2:
return 29
else:
return 24
else:
return Age
train_df[["Parch", "Survived"]].groupby(['Parch'], as_index=False ).mean().sort_values(by='Survived',
ascending=False )<feature_engineering>
titanic['Age'] = titanic[['Age','Pclass']].apply(impute_age,axis=1)
 titanic_t['Age'] = titanic_t[['Age','Pclass']].apply(impute_age,axis=1)
 titanic_t['Fare'] = titanic_t['Fare'].fillna(fare_mean )
for dataset in combine:
dataset['Title'] = dataset.Name.str.extract('([A-Za-z]+)\.', expand=False)
 pd.crosstab(train_df['Title'], train_df['Sex'] )<feature_engineering>
titanic['Embarked'].value_counts()
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()<categorify>
titanic['Embarked'] = titanic['Embarked'].fillna('S')
 titanic_t['Embarked'] = titanic_t['Embarked'].fillna('S' )
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()<drop_column>
titanic = titanic.drop(['PassengerId', 'Ticket'], axis=1)
 titanic_t = titanic_t.drop(['PassengerId', 'Ticket'], axis=1 )
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<data_type_conversions>
titanic['With_someone'] = titanic['SibSp'] | titanic['Parch']
titanic_t['With_someone'] = titanic_t['SibSp'] | titanic_t['Parch']
for dataset in combine:
dataset['Sex'] = dataset['Sex'].map({'female': 1, 'male': 0} ).astype(int)
 train_df.head()<define_variables>
titanic['With_someone'] = titanic['With_someone'].apply(lambda x:1 if x >=1 else 0)
 titanic_t['With_someone'] = titanic_t['With_someone'].apply(lambda x:1 if x >=1 else 0 )
guess_ages = np.zeros(( 2,3))
guess_ages<categorify>
titanic['Title'] = titanic['Name'].str.extract('([A-Za-z]+)\.', expand=False)
 titanic_t['Title'] = titanic_t['Name'].str.extract('([A-Za-z]+)\.', expand=False)
 titanic['Title'].value_counts()
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()<sort_values>
title_mapping = {"Mr": 0, "Miss": 1, "Mrs": 2,
"Master": 3, "Dr": 3, "Rev": 3, "Col": 3, "Major": 3, "Mlle": 3,"Countess": 3,
"Ms": 3, "Lady": 3, "Jonkheer": 3, "Don": 3, "Dona" : 3, "Mme": 3,"Capt": 3,"Sir": 3 }
titanic['Title'] = titanic['Title'].map(title_mapping)
 titanic_t['Title'] = titanic_t['Title'].map(title_mapping )
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 )<feature_engineering>
titanic = titanic.drop(['Name'], axis=1)
 titanic_t = titanic_t.drop(['Name'], axis=1 )
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()<drop_column>
titanic['family members'] = titanic['SibSp'] + titanic['Parch'] + 1
titanic_t['family members'] = titanic_t['SibSp'] + titanic_t['Parch'] + 1
train_df = train_df.drop(['AgeBand'], axis=1)
 combine = [train_df, test_df]
train_df.head()<sort_values>
titanic = pd.get_dummies(titanic, columns = ['Pclass', 'Sex', 'Embarked', 'Title'], drop_first = True)
 titanic_t = pd.get_dummies(titanic_t, columns = ['Pclass', 'Sex', 'Embarked', 'Title'], drop_first = True )
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 )<feature_engineering>
titanic = titanic.drop(['SibSp', 'Parch', 'Age'], axis=1)
 titanic_t = titanic_t.drop(['SibSp', 'Parch', 'Age'], axis=1 )
for dataset in combine:
dataset['IsAlone'] = 0
dataset.loc[dataset['FamilySize'] == 1, 'IsAlone'] = 1
train_df[['IsAlone', 'Survived']].groupby(['IsAlone'], as_index=False ).mean()<drop_column>
X = titanic.drop(['Survived'], axis=1)
 y = titanic['Survived']
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()<set_options>
X_t = titanic_t
freq_port = train_df.Embarked.dropna().mode() [0]
print(freq_port )<sort_values>
X_scale = X[['Fare', 'family members']]
X_noscale = X.drop(['Fare', 'family members'], axis=1)
 X_scale_t = X_t[['Fare', 'family members']]
X_noscale_t = X_t.drop(['Fare', 'family members'], axis=1 )
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 )<data_type_conversions>
sc_X = MinMaxScaler()
X_scaled = sc_X.fit_transform(X_scale)
 X_scaled_t = sc_X.fit_transform(X_scale_t )
for dataset in combine:
dataset['Embarked'] = dataset['Embarked'].map({'S': 0, 'C': 1, 'Q': 2} ).astype(int)
 train_df.head()<data_type_conversions>
X_scaled = pd.DataFrame(X_scaled, columns=['Fare', 'family members'])
 X_scaled_t = pd.DataFrame(X_scaled_t, columns=['Fare', 'family members'] )
test_df['Fare'].fillna(test_df['Fare'].dropna().median() , inplace=True )<sort_values>
X = pd.concat([X_scaled, X_noscale], axis=1)
 X_t = pd.concat([X_scaled_t, X_noscale_t], axis=1 )
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 )<data_type_conversions>
k_range = [4]
weight_options = ['uniform']
norm = [1]
algo = ['ball_tree']
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 )<split>
param_grid = dict(n_neighbors = k_range, weights = weight_options, p = norm, algorithm = algo)
 param_grid
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 )<compute_train_metric>
from sklearn.model_selection import GridSearchCV, RandomizedSearchCV
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 )<compute_train_metric>
knn = KNeighborsClassifier()
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 )<compute_train_metric>
grid_knn = GridSearchCV(knn, param_grid, cv = 10, scoring='accuracy', return_train_score=False)
 grid_knn.fit(X, y )
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 )<compute_train_metric>
grid_knn.best_params_
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 )<compute_train_metric>
grid_knn.best_score_
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 )<compute_train_metric>
forest_clf = RandomForestClassifier()
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 )<compute_train_metric>
param_grid = dict(n_estimators = [10], criterion = ['gini'], max_depth = [135, 140, 145] )
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 )<compute_train_metric>
grid_forest = GridSearchCV(forest_clf, param_grid, cv = 10, scoring='accuracy', return_train_score=False)
 grid_forest.fit(X, y )
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 )<train_model>
grid_forest.best_params_
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 )<create_dataframe>
grid_forest.best_score_
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 )<predict_on_test>
clf = svm.SVC(probability = False )
Y_pred = linear_svc.predict(test_df )<save_to_csv>
param_grid = dict(C = [34], kernel = ['poly'], gamma = ['scale'], degree = [2])
 param_grid
submission = pd.read_csv('.. /input/titanic/gender_submission.csv')
 submission['Survived'] = Y_pred
submission.to_csv('submission.csv', index=False )<load_from_csv>
grid_svm = GridSearchCV(clf, param_grid, cv = 10, scoring='accuracy', return_train_score=True)
 grid_svm.fit(X, y)
data_raw = pd.read_csv('.. /input/titanic/train.csv')
 data_val = pd.read_csv('.. /input/titanic/test.csv')
 data1 = data_raw.copy(deep=True)
 data_cleaner = [data1, data_val]
Target = ['Survived']<feature_engineering>
grid_svm.best_params_
for dataset in data_cleaner:
dataset.Age.fillna(dataset.Age.median() , inplace=True)
 dataset.Embarked.fillna('S', inplace=True)
 dataset.Fare.fillna(dataset.Fare.median() , inplace=True)
 dataset['Family_members'] = dataset.Parch + dataset.SibSp<drop_column>
grid_svm.best_score_
data1.drop(['Name', 'PassengerId', 'Ticket', 'SibSp', 'Parch'], axis=1, inplace=True )<drop_column>
from sklearn.linear_model import LogisticRegression
for dataset in data_cleaner:
dataset['Cabin_Allotted'] = np.where(dataset.Cabin.isnull() , 0, 1)
 dataset.drop('Cabin', axis=1, inplace=True )<categorify>
clf_lr = LogisticRegression()
lb = LabelEncoder()
for dataset in data_cleaner:
dataset['Sex_labeled'] = lb.fit_transform(dataset.Sex)
 dataset['AgeBin'] = pd.qcut(dataset.Age, 3)
 dataset['Age_labeled'] = lb.fit_transform(dataset['AgeBin'])
 dataset['FareBin'] = pd.qcut(dataset.Fare, 4)
 dataset['Fare_labeled'] = lb.fit_transform(dataset['FareBin'])
 dataset['Embarked_labeled'] = lb.fit_transform(dataset.Embarked )<count_values>
param_grid = dict(penalty = ['l2'], solver = ['newton-cg', 'lbfgs'])
 param_grid
print(data1['Age_labeled'].value_counts())
 print(data1['Fare_labeled'].value_counts() )<set_options>
grid_lr = RandomizedSearchCV(clf_lr, param_grid, cv = 10, scoring='accuracy', return_train_score=True, n_iter=150)
 grid_lr.fit(X, y )
MLA = [
ensemble.AdaBoostClassifier() ,
ensemble.BaggingClassifier() ,
ensemble.ExtraTreesClassifier() ,
ensemble.GradientBoostingClassifier() ,
ensemble.RandomForestClassifier() ,
neighbors.KNeighborsClassifier() ,
tree.DecisionTreeClassifier() ,
tree.ExtraTreeClassifier() ,
XGBClassifier(objective='binary:logistic', eval_metric='logloss')
 ]
cv_split = model_selection.ShuffleSplit(n_splits=10, test_size=.2, train_size=.8, random_state=1)
 MLA_columns = ['MLA Name', 'MLA Parameters', 'MLA Train Accuracy Mean', 'MLA Test Accuracy Mean', 'MLA Test Accuracy 3*STD', 'MLA Time']
MLA_compare = pd.DataFrame(columns = MLA_columns)
 row_index = 0
for alg in MLA:
MLA_name = alg.__class__.__name__
MLA_compare.loc[row_index, 'MLA Name'] = MLA_name
MLA_compare.loc[row_index, 'MLA Parameters'] = str(alg.get_params())
 cv_results = model_selection.cross_validate(alg, data1[data1_X], data1[Target].values.reshape(-1,), cv=cv_split, return_train_score=True)
 MLA_compare.loc[row_index, 'MLA Time'] = cv_results['fit_time'].mean()
MLA_compare.loc[row_index, 'MLA Train Accuracy Mean'] = cv_results['train_score'].mean()
MLA_compare.loc[row_index, 'MLA Test Accuracy Mean'] = cv_results['test_score'].mean()
MLA_compare.loc[row_index, 'MLA Test Accuracy 3*STD'] = cv_results['test_score'].std() *3
row_index += 1
MLA_compare<define_search_space>
grid_lr.best_params_
grid_n_estimator = [10, 50, 100, 300]
grid_ratio = [.1,.25,.5,.75, 1.0]
grid_learn = [.01,.03,.05,.1,.25]
grid_max_depth = [1, 2, 3, 4, 5, 6, 7, 8, 10, None]
grid_min_samples = [5, 10,.03,.05,.10]
grid_criterion = ['gini', 'entropy']
grid_seed = [1]
grid_param = [
[{
'learning_rate': grid_learn,
'n_estimators': grid_n_estimator,
'random_state': grid_seed
}],
[{
'max_samples': grid_ratio,
'n_estimators': grid_n_estimator,
'random_state': grid_seed
}],
[{
'n_estimators': grid_n_estimator,
'criterion': grid_criterion,
'max_depth': grid_max_depth,
'random_state': grid_seed
}],
[{
'learning_rate': grid_learn,
'n_estimators': grid_n_estimator,
'max_depth': grid_max_depth,
'random_state': grid_seed
}],
[{
'n_estimators': grid_n_estimator,
'criterion': grid_criterion,
'max_depth': grid_max_depth[:-1],
'random_state': grid_seed,
}],
[{
'n_neighbors': grid_max_depth[:-1],
'weights': ['uniform', 'distance'],
'algorithm': ['auto', 'ball_tree', 'kd_tree', 'brute']
}],
[{
'criterion': grid_criterion,
'max_depth': grid_max_depth,
'random_state': grid_seed,
}],
[{
'criterion': grid_criterion,
'max_depth': grid_max_depth,
'random_state': grid_seed,
}],
[{
'learning_rate': grid_learn,
'max_depth': grid_max_depth[:-1],
'n_estimators': grid_n_estimator,
'seed': grid_seed,
'objective': ['binary:logistic'],
'eval_metric': ['logloss']
}]
]
row_index = 0
start_total = time.perf_counter()
for clf, param in zip(MLA, grid_param):
start = time.perf_counter()
best_search = model_selection.GridSearchCV(estimator=clf, param_grid=param, cv=cv_split, scoring='roc_auc')
 best_search.fit(data1[data1_X], data1[Target].values.reshape(-1,))
run = time.perf_counter() - start
best_param = best_search.best_params_
print(f'The best parameter for {clf.__class__.__name__} is {best_param} with a runtime of {run:.2f} seconds.')
 print('-'*60)
 clf.set_params(**best_param)
 MLA_name = clf.__class__.__name__
MLA_compare.loc[row_index, 'MLA Name'] = MLA_name
MLA_compare.loc[row_index, 'MLA Parameters'] = str(clf.get_params())
 cv_results = model_selection.cross_validate(clf, data1[data1_X], data1[Target].values.reshape(-1,), cv=cv_split, return_train_score=True)
 MLA_compare.loc[row_index, 'MLA Time'] = cv_results['fit_time'].mean()
MLA_compare.loc[row_index, 'MLA Train Accuracy Mean'] = cv_results['train_score'].mean()
MLA_compare.loc[row_index, 'MLA Test Accuracy Mean'] = cv_results['test_score'].mean()
MLA_compare.loc[row_index, 'MLA Test Accuracy 3*STD'] = cv_results['test_score'].std() *3
row_index += 1
run_total = time.perf_counter() - start_total
print('-'*60)
 print(f'Total optimization time was {run_total / 60:.2f} minutes.')
 print('-'*60)
 MLA_compare.sort_values(by=['MLA Test Accuracy Mean'], ascending=False, inplace=True)
 MLA_compare<save_to_csv>
grid_lr.best_score_
model = ensemble.RandomForestClassifier(**{'criterion': 'entropy', 'max_depth': 5, 'n_estimators': 50, 'random_state': 1})
 model.fit(data1[data1_X], data1[Target].values.reshape(-1,))
predictions = model.predict(data_val[data1_X])
 output = pd.DataFrame({'PassengerId': data_val.PassengerId, 'Survived': predictions})
 output.to_csv('./my_submission_RandomForestClassifier_tunned_F4.csv', index=False)
 print("Your submission was successfully saved!" )<load_from_csv>
from sklearn.tree import DecisionTreeClassifier
train_df=pd.read_csv("/kaggle/input/titanic/train.csv")
 test_df=pd.read_csv("/kaggle/input/titanic/test.csv")
 gender_submission_df=pd.read_csv("/kaggle/input/titanic/gender_submission.csv")
 test_PassengerId=test_df["PassengerId"]<sort_values>
clf_dt = DecisionTreeClassifier()
train_df[["Pclass","Survived"]].groupby(["Pclass"],as_index=False ).mean().sort_values(by="Survived",ascending=False )<sort_values>
param_grid = dict(criterion = ['entropy'], max_depth = [6])
 param_grid
train_df[["Sex","Survived"]].groupby(["Sex"],as_index=False ).mean().sort_values(by="Survived",ascending=False )<sort_values>
grid_tree = GridSearchCV(clf_dt, param_grid, cv = 10, scoring='accuracy', return_train_score=False)
 grid_tree.fit(X, y )
train_df[["SibSp","Survived"]].groupby(["SibSp"],as_index=False ).mean().sort_values(by="Survived",ascending=False )<sort_values>
grid_tree.best_params_
train_df[["Parch","Survived"]].groupby(["Parch"],as_index=False ).mean().sort_values(by="Survived",ascending=False )<features_selection>
grid_tree.best_score_
train_df.columns[train_df.isnull().any() ]<features_selection>
from sklearn.ensemble import GradientBoostingClassifier
test_df.columns[test_df.isnull().any() ]<concatenate>
clf_gb = GradientBoostingClassifier()
train_df_len=len(train_df)
 train_df=pd.concat([train_df,test_df],axis=0 ).reset_index(drop=True )<features_selection>
param_grid = dict(loss = ['exponential'], learning_rate = [0.2], n_estimators = [21], max_depth = [4])
 param_grid
train_df.columns[train_df.isnull().any() ]<count_missing_values>
grid_gb = GridSearchCV(clf_gb, param_grid, cv = 10, scoring='accuracy', return_train_score=False)
 grid_gb.fit(X, y )
train_df.isnull().sum()<filter>
grid_gb.best_params_
train_df[train_df["Embarked"].isnull() ]<feature_engineering>
grid_gb.best_score_
train_df["Embarked"]=train_df["Embarked"].fillna("C" )<filter>
from sklearn.ensemble import AdaBoostClassifier
train_df[train_df["Embarked"].isnull() ]<filter>
clf_ab = AdaBoostClassifier()
train_df[train_df["Fare"].isnull() ]<feature_engineering>
param_grid = dict(n_estimators = [65], algorithm = ['SAMME'])
 param_grid
train_df["Embarked"]=train_df["Embarked"].fillna("C" )<feature_engineering>
grid_ab = GridSearchCV(clf_ab, param_grid, cv = 10, scoring='accuracy', return_train_score=False)
 grid_ab.fit(X, y )
train_df.Fare[1043]=train_df[train_df["Pclass"]==3].Fare.mean()<filter>
grid_ab.best_params_
train_df[train_df["Age"].isnull() ]<count_values>
grid_ab.best_score_
train_df.Sex.value_counts()<define_variables>
from sklearn.ensemble import VotingClassifier
index_nan_age=list(train_df["Age"][train_df["Age"].isnull() ].index )<define_variables>
eclf_hard = VotingClassifier(estimators = [('knn', grid_knn),('forest', grid_forest),('svm', grid_svm), 
('Logistic', grid_lr),('tree', grid_tree), 
('GradientBoost', grid_gb), 
('AdaBoost', grid_ab)], voting='hard', weights=[2, 1, 2.5, 0.8, 3, 3.5, 1] )
for i in index_nan_age:
age_predict=train_df["Age"][(( train_df["SibSp"]==train_df.iloc[i]["SibSp"])&(train_df["Parch"]==train_df.iloc[i]["Parch"])&(train_df["Pclass"]==train_df.iloc[i]["Pclass"])) ].median()
age_med=train_df["Age"].median()
if not np.isnan(age_predict):
train_df["Age"].iloc[i]=age_predict
else:
train_df["Age"].iloc[i]=age_med 
<filter>
eclf_hard = eclf_hard.fit(X, y )
train_df[train_df["Age"].isnull() ]<string_transform>
y_pred = eclf_hard.predict(X_t )
s="Braund, Mr.Owen Harris"
s.split("." )<string_transform>
sub = pd.DataFrame(y_pred, columns=['Survived'] )
s.split(".")[0].split("," )<string_transform>
titanic = pd.read_csv(".. /input/test.csv" )
s.split(".")[0].split(",")[1]<string_transform>
submission = pd.concat([titanic['PassengerId'], sub], axis=1 )
s.split(".")[0].split(",")[-1]<feature_engineering>
submission.to_csv('sub.csv', index=False )