Kaggle competition: Titanic - Machine Learning from Disaster
combine.BsmtQual.value_counts()<count_values>
def clean_data(data):
data["Fare"] = data["Fare"].fillna(data["Fare"].dropna().median())
 data["Age"] = data["Age"].fillna(data["Age"].dropna().median())
 data.loc[data["Sex"] == "male", "Sex"] = 0
data.loc[data["Sex"] == "female", "Sex"] = 1
data["Embarked"] = data["Embarked"].fillna("S")
 data.loc[data["Embarked"] == "S", "Embarked"] = 0
data.loc[data["Embarked"] == "C", "Embarked"] = 1
data.loc[data["Embarked"] == "Q", "Embarked"] = 2
combine['BsmtQual']=combine['BsmtQual'].fillna('None')
 combine.BsmtQual.value_counts()<count_values>
warnings.filterwarnings("ignore" )
combine.BsmtCond.value_counts()<count_values>
train = pd.read_csv("/kaggle/input/titanic/train.csv")
 clean_data(train)
 target = train['Survived'].values
features = train[['Pclass', 'Age', 'Fare', 'Embarked', 'Sex', 'SibSp', 'Parch']].values
classifier = linear_model.LogisticRegression()
classifier_ = classifier.fit(features, target)
 print(classifier_.score(features, target))
combine['BsmtCond']=combine['BsmtCond'].fillna('None')
 combine.BsmtCond.value_counts()<count_values>
poly = preprocessing.PolynomialFeatures(degree=2)
 poly_features = poly.fit_transform(features)
 classifier_ = classifier.fit(poly_features, target)
 print(classifier_.score(poly_features, target))
combine.BsmtExposure.value_counts()<count_values>
train = pd.read_csv("/kaggle/input/titanic/train.csv")
 clean_data(train)
 target = train["Survived"].values
features = train[["Pclass", "Age", "Fare", "Embarked", "Sex", "SibSp", "Parch"]].values
decision_tree = tree.DecisionTreeClassifier(random_state = 42)
 decision_tree_ = decision_tree.fit(features, target)
 print(decision_tree_.score(features, target))
combine['BsmtExposure']=combine['BsmtExposure'].fillna('None')
 combine.BsmtExposure.value_counts()<count_values>
generalized_tree = tree.DecisionTreeClassifier(
 random_state = 1,
max_depth = 7,
min_samples_split = 2)
 generalized_tree_ = generalized_tree.fit(features, target)
 scores = model_selection.cross_val_score(generalized_tree, features, target, scoring = 'accuracy', cv = 50)
 print(scores)
 print(scores.mean() )
combine.BsmtFinType1.value_counts()<count_values>
data = export_graphviz(DecisionTreeClassifier(max_depth=3 ).fit(features, target), out_file=None,
feature_names = ['Pclass', 'Age', 'Fare', 'Embarked', 'Sex', 'SibSp', 'Parch'],
class_names = ['Survived(0)', 'Survived(1)'],
filled = True, rounded = True, special_characters = True)
 graph = graphviz.Source(data)
 graph
combine['BsmtFinType1']=combine['BsmtFinType1'].fillna('None')
 combine.BsmtFinType1.value_counts()<filter>
forest = RandomForestClassifier(random_state = 1)
 n_estimators = [1740, 1742, 1745, 1750]
max_depth = [6, 7, 8]
min_samples_split = [4, 5, 6]
min_samples_leaf = [4, 5, 6]
oob_score = ['True']
hyperF = dict(n_estimators = n_estimators, max_depth = max_depth, min_samples_split = min_samples_split, min_samples_leaf = min_samples_leaf, oob_score = oob_score)
 gridF = GridSearchCV(forest, hyperF, verbose = 1, n_jobs = 4)
 bestF = gridF.fit(features, target )
combine.BsmtFinType1[combine.BsmtFinSF1.isnull() ]<count_missing_values>
train = pd.read_csv("/kaggle/input/titanic/train.csv")
 clean_data(train)
 target = train["Survived"].values
features = train[["Pclass", "Age", "Fare", "Embarked", "Sex", "SibSp", "Parch"]].values
r_forest = RandomForestClassifier(criterion='gini',bootstrap=True,
n_estimators=1745,
max_depth=7,
min_samples_split=6,
min_samples_leaf=6,
max_features='auto',
oob_score=True,
random_state=123,
n_jobs=-1,
verbose=0)
 rf_clf = r_forest.fit(features, target)
 print(rf_clf.score(features, target))
combine['BsmtFinSF1']=combine['BsmtFinSF1'].fillna(0)
 combine.BsmtFinSF1.isnull().sum()<count_values>
rf_clf.oob_score_
combine.BsmtFinType2.value_counts()<count_values>
value = 1.50
width = 0.75
clf1 = LogisticRegression(random_state=0)
 clf2 = RandomForestClassifier(random_state=0)
 clf3 = DecisionTreeClassifier(random_state=0)
 eclf = EnsembleVoteClassifier(clfs=[clf1, clf2, clf3], weights=[1, 1, 1], voting='soft')
 X_list = train[["Pclass", "Age", "Fare", "Embarked", "Sex", "SibSp", "Parch"]]
X = np.asarray(X_list, dtype=np.float32)
 y_list = train["Survived"]
y = np.asarray(y_list, dtype=np.int32)
 gs = gridspec.GridSpec(2, 2)
 fig = plt.figure(figsize=(10, 8))
labels = ['Logistic Regression',
'Random Forest',
'Decision Tree',
'Ensemble']
for clf, lab, grd in zip([clf1, clf2, clf3, eclf],
labels,
itertools.product([0, 1],
repeat=2)) :
clf.fit(X, y)
 ax = plt.subplot(gs[grd[0], grd[1]])
 fig = plot_decision_regions(X=X, y=y, clf=clf,
filler_feature_values={2: value, 3: value, 4: value, 5: value, 6: value},
filler_feature_ranges={2: width, 3: width, 4: width, 5: width, 6: width},
legend=2)
 plt.title(lab)
 plt.show()
combine['BsmtFinType2']=combine['BsmtFinType2'].fillna('None')
 combine.BsmtFinType2.value_counts()<count_missing_values>
test = pd.read_csv("/kaggle/input/titanic/test.csv")
 clean_data(test)
 prediction = rf_clf.predict(test[["Pclass", "Age", "Fare", "Embarked", "Sex", "SibSp", "Parch"]])
 output = pd.DataFrame({'PassengerId': test.PassengerId, 'Survived': prediction})
 output.to_csv('titanic_submission.csv', index=False)
 print("Submission successful" )
combine['BsmtFinSF2']=combine['BsmtFinSF2'].fillna(0)
 combine.BsmtFinSF2.isnull().sum()<count_missing_values>
def clean_data(data):
data["Fare"] = data["Fare"].fillna(data["Fare"].dropna().median())
 data["Age"] = data["Age"].fillna(data["Age"].dropna().median())
 data.loc[data["Sex"] == "male", "Sex"] = 0
data.loc[data["Sex"] == "female", "Sex"] = 1
data["Embarked"] = data["Embarked"].fillna("S")
 data.loc[data["Embarked"] == "S", "Embarked"] = 0
data.loc[data["Embarked"] == "C", "Embarked"] = 1
data.loc[data["Embarked"] == "Q", "Embarked"] = 2
combine['BsmtUnfSF']=combine['BsmtUnfSF'].fillna(0)
 combine.BsmtUnfSF.isnull().sum()<feature_engineering>
warnings.filterwarnings("ignore" )
combine['TotalBsmtSF']=combine['TotalBsmtSF'].fillna(0)
 combine.TotalBsmtSF.isnull().sum()<count_values>
train = pd.read_csv("/kaggle/input/titanic/train.csv")
 clean_data(train)
 target = train['Survived'].values
features = train[['Pclass', 'Age', 'Fare', 'Embarked', 'Sex', 'SibSp', 'Parch']].values
classifier = linear_model.LogisticRegression()
classifier_ = classifier.fit(features, target)
 print(classifier_.score(features, target))
combine.Electrical.value_counts()<filter>
poly = preprocessing.PolynomialFeatures(degree=2)
 poly_features = poly.fit_transform(features)
 classifier_ = classifier.fit(poly_features, target)
 print(classifier_.score(poly_features, target))
combine.Neighborhood[combine.Electrical.isnull() ]<count_values>
train = pd.read_csv("/kaggle/input/titanic/train.csv")
 clean_data(train)
 target = train["Survived"].values
features = train[["Pclass", "Age", "Fare", "Embarked", "Sex", "SibSp", "Parch"]].values
decision_tree = tree.DecisionTreeClassifier(random_state = 42)
 decision_tree_ = decision_tree.fit(features, target)
 print(decision_tree_.score(features, target))
combine[combine.Neighborhood == 'Timber'].Electrical.value_counts()<count_values>
generalized_tree = tree.DecisionTreeClassifier(
 random_state = 1,
max_depth = 7,
min_samples_split = 2)
 generalized_tree_ = generalized_tree.fit(features, target)
 scores = model_selection.cross_val_score(generalized_tree, features, target, scoring = 'accuracy', cv = 50)
 print(scores)
 print(scores.mean() )
combine['Electrical']=combine['Electrical'].fillna('SBrkr')
 combine.Electrical.value_counts()<count_values>
data = export_graphviz(DecisionTreeClassifier(max_depth=3 ).fit(features, target), out_file=None,
feature_names = ['Pclass', 'Age', 'Fare', 'Embarked', 'Sex', 'SibSp', 'Parch'],
class_names = ['Survived(0)', 'Survived(1)'],
filled = True, rounded = True, special_characters = True)
 graph = graphviz.Source(data)
 graph
combine.BsmtFullBath.value_counts()<count_values>
forest = RandomForestClassifier(random_state = 1)
 n_estimators = [1740, 1742, 1745, 1750]
max_depth = [6, 7, 8]
min_samples_split = [4, 5, 6]
min_samples_leaf = [4, 5, 6]
oob_score = ['True']
hyperF = dict(n_estimators = n_estimators, max_depth = max_depth, min_samples_split = min_samples_split, min_samples_leaf = min_samples_leaf, oob_score = oob_score)
 gridF = GridSearchCV(forest, hyperF, verbose = 1, n_jobs = 4)
 bestF = gridF.fit(features, target )
combine['BsmtFullBath']=combine['BsmtFullBath'].fillna(0)
 combine.BsmtFullBath.value_counts()<count_values>
train = pd.read_csv("/kaggle/input/titanic/train.csv")
 clean_data(train)
 target = train["Survived"].values
features = train[["Pclass", "Age", "Fare", "Embarked", "Sex", "SibSp", "Parch"]].values
r_forest = RandomForestClassifier(criterion='gini',bootstrap=True,
n_estimators=1745,
max_depth=7,
min_samples_split=6,
min_samples_leaf=6,
max_features='auto',
oob_score=True,
random_state=123,
n_jobs=-1,
verbose=0)
 rf_clf = r_forest.fit(features, target)
 print(rf_clf.score(features, target))
combine.BsmtHalfBath.value_counts()<count_values>
rf_clf.oob_score_
combine['BsmtHalfBath']=combine['BsmtHalfBath'].fillna(0)
 combine.BsmtHalfBath.value_counts()<count_values>
value = 1.50
width = 0.75
clf1 = LogisticRegression(random_state=0)
 clf2 = RandomForestClassifier(random_state=0)
 clf3 = DecisionTreeClassifier(random_state=0)
 eclf = EnsembleVoteClassifier(clfs=[clf1, clf2, clf3], weights=[1, 1, 1], voting='soft')
 X_list = train[["Pclass", "Age", "Fare", "Embarked", "Sex", "SibSp", "Parch"]]
X = np.asarray(X_list, dtype=np.float32)
 y_list = train["Survived"]
y = np.asarray(y_list, dtype=np.int32)
 gs = gridspec.GridSpec(2, 2)
 fig = plt.figure(figsize=(10, 8))
labels = ['Logistic Regression',
'Random Forest',
'Decision Tree',
'Ensemble']
for clf, lab, grd in zip([clf1, clf2, clf3, eclf],
labels,
itertools.product([0, 1],
repeat=2)) :
clf.fit(X, y)
 ax = plt.subplot(gs[grd[0], grd[1]])
 fig = plot_decision_regions(X=X, y=y, clf=clf,
filler_feature_values={2: value, 3: value, 4: value, 5: value, 6: value},
filler_feature_ranges={2: width, 3: width, 4: width, 5: width, 6: width},
legend=2)
 plt.title(lab)
 plt.show()
combine.KitchenQual.value_counts()<count_values>
test = pd.read_csv("/kaggle/input/titanic/test.csv")
 clean_data(test)
 prediction = rf_clf.predict(test[["Pclass", "Age", "Fare", "Embarked", "Sex", "SibSp", "Parch"]])
 output = pd.DataFrame({'PassengerId': test.PassengerId, 'Survived': prediction})
 output.to_csv('titanic_submission.csv', index=False)
 print("Submission successful" )