<jupyter_start><jupyter_text>本文为Gitchat上的Chat[数据科学 Kaggle 比赛项目实战：Titanic](https://gitbook.cn/gitchat/activity/5bceccc2bdcbc133c64e98ee)了解详细的项目讲解，请扫下方二维码：![](pic/chat.png)<jupyter_code>import pandas as pd
import numpy as np
import random as rnd
import seaborn as sns
import matplotlib.pyplot as plt
from sklearn.linear_model import LogisticRegression
from sklearn.svm import SVC, LinearSVC
from sklearn.ensemble import RandomForestClassifier
from sklearn.neighbors import KNeighborsClassifier
from sklearn.naive_bayes import GaussianNB
from sklearn.linear_model import Perceptron
from sklearn.linear_model import SGDClassifier
from sklearn.tree import DecisionTreeClassifier
from sklearn.model_selection import KFold, cross_val_score,GridSearchCV
from sklearn.ensemble import VotingClassifier
import warnings
warnings.filterwarnings("ignore")

%matplotlib inline
train_df = pd.read_csv('../input/train.csv')
test_df = pd.read_csv('../input/test.csv')
combine = [train_df, test_df]
train_df.head()
train_df.describe()
train_df.describe(include=['O'])
train_df[['Pclass', 'Survived']].groupby(['Pclass'], as_index=False).mean().sort_values(by='Survived', ascending=False)
train_df[["Sex", "Survived"]].groupby(['Sex'], as_index=False).mean().sort_values(by='Survived', ascending=False)
train_df[["SibSp", "Survived"]].groupby(['SibSp'], as_index=False).mean().sort_values(by='Survived', ascending=False)
train_df[["Parch", "Survived"]].groupby(['Parch'], as_index=False).mean().sort_values(by='Survived', ascending=False)
g = sns.FacetGrid(train_df, col='Survived')
g.map(plt.hist, 'Age', bins=20)
grid = sns.FacetGrid(train_df, col='Survived', row='Pclass', size=2.2, aspect=1.6)
grid.map(plt.hist, 'Age', alpha=.5, bins=20)
grid.add_legend()
train_df = train_df.drop(['Ticket', 'Cabin'], axis=1)
test_df = test_df.drop(['Ticket', 'Cabin'], axis=1)
combine = [train_df, test_df]
for dataset in combine:
    dataset['Title'] = dataset.Name.str.extract(' ([A-Za-z]+)\.', expand=False)

pd.crosstab(train_df['Title'], train_df['Sex'])
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()
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 = train_df.drop(['Name', 'PassengerId'], axis=1)
test_df = test_df.drop(['Name'], axis=1)
combine = [train_df, test_df]
for dataset in combine:
    dataset['Sex'] = dataset['Sex'].map( {'female': 1, 'male': 0} ).astype(int)
guess_ages = np.zeros((2,3))

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_mean = guess_df.mean()
            # age_std = guess_df.std()
            # age_guess = rnd.uniform(age_mean - age_std, age_mean + age_std)

            age_guess = guess_df.median()

            # Convert random age float to nearest .5 age
            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)
freq_port = train_df.Embarked.dropna().mode()[0]

for dataset in combine:
    dataset['Embarked'] = dataset['Embarked'].fillna(freq_port)
test_df['Fare'].fillna(test_df['Fare'].dropna().median(), inplace=True)
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)
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']
train_df = train_df.drop(['AgeBand'], axis=1)
combine = [train_df, test_df]
for dataset in combine:
    dataset['FamilySize'] = dataset['SibSp'] + dataset['Parch'] + 1
for dataset in combine:
    dataset['IsAlone'] = 0
    dataset.loc[dataset['FamilySize'] == 1, 'IsAlone'] = 1
train_df = train_df.drop(['Parch', 'SibSp', 'FamilySize'], axis=1)
test_df = test_df.drop(['Parch', 'SibSp', 'FamilySize'], axis=1)
combine = [train_df, test_df]
for dataset in combine:
    dataset['Age*Class'] = dataset.Age * dataset.Pclass
for dataset in combine:
    dataset['Embarked'] = dataset['Embarked'].map( {'S': 0, 'C': 1, 'Q': 2} ).astype(int)
train_df['FareBand'] = pd.qcut(train_df['Fare'], 4)

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]
X_train = train_df.drop("Survived", axis=1)
Y_train = train_df["Survived"]
X_test  = test_df.drop("PassengerId", axis=1).copy()
logreg = LogisticRegression()
logreg.fit(X_train, Y_train)
Y_pred = logreg.predict(X_test)
acc_log = round(logreg.score(X_train, Y_train) * 100, 2)
acc_log
coeff_df = pd.DataFrame(train_df.columns.delete(0))
coeff_df.columns = ['Feature']
coeff_df["Correlation"] = pd.Series(logreg.coef_[0])

coeff_df.sort_values(by='Correlation', ascending=False)
svc = SVC()

param_grid={'C':[0.45, 0.5, 1]}
grid_svc = GridSearchCV(estimator=svc,param_grid=param_grid)
grid_svc.fit(X_train,Y_train)
print(grid_svc.best_params_)

Y_pred = grid_svc.predict(X_test)
acc_svc = round(grid_svc.score(X_train, Y_train) * 100, 2)
acc_svc
knn = KNeighborsClassifier(n_neighbors = 3)
param_grid={'n_neighbors':[4,5,6],'weights':['uniform','distance'],'algorithm':['auto','ball_tree','kd_tree','brute']}
grid_knn = GridSearchCV(estimator=knn,param_grid=param_grid)
grid_knn.fit(X_train,Y_train)
print(grid_knn.best_params_)

Y_pred = grid_knn.predict(X_test)
acc_knn = round(grid_knn.score(X_train, Y_train) * 100, 2)
acc_knn
gaussian = GaussianNB()
gaussian.fit(X_train, Y_train)
Y_pred = gaussian.predict(X_test)
acc_gaussian = round(gaussian.score(X_train, Y_train) * 100, 2)
acc_gaussian
perceptron = Perceptron()
param_grid={'penalty':[None,'l2','l1','elasticnet'],'eta0':[1,1.2,1.4,1.5]}
grid_perceptron = GridSearchCV(estimator=perceptron,param_grid=param_grid)
grid_perceptron.fit(X_train,Y_train)
print(grid_perceptron.best_params_)

Y_pred = grid_perceptron.predict(X_test)
acc_perceptron = round(grid_perceptron.score(X_train, Y_train) * 100, 2)
acc_perceptron
linear_svc = LinearSVC()
param_grid={'loss':['hinge','squared_hinge'],'C':[0.3,0.5,0.7,1]}
grid_linear_svc = GridSearchCV(estimator=linear_svc,param_grid=param_grid)
grid_linear_svc.fit(X_train,Y_train)
print(grid_linear_svc.best_params_)

Y_pred = grid_linear_svc.predict(X_test)
acc_linear_svc = round(grid_linear_svc.score(X_train, Y_train) * 100, 2)
acc_linear_svc
sgd = SGDClassifier()
sgd.fit(X_train, Y_train)
Y_pred = sgd.predict(X_test)
acc_sgd = round(sgd.score(X_train, Y_train) * 100, 2)
acc_sgd
decision_tree = DecisionTreeClassifier()
param_grid={'splitter':['best','random'],'max_features':['auto', 'sqrt', 'log2', None]}
grid_decision_tree = GridSearchCV(estimator=decision_tree,param_grid=param_grid)
grid_decision_tree.fit(X_train,Y_train)
print(grid_decision_tree.best_params_)

Y_pred = grid_decision_tree.predict(X_test)
acc_decision_tree = round(grid_decision_tree.score(X_train, Y_train) * 100, 2)
acc_decision_tree
random_forest = RandomForestClassifier(n_estimators=100)
param_grid={'n_estimators':[10,15,20,25,30],'max_features':['auto','sqrt','log2',None]}
grid_random_forest = GridSearchCV(estimator=random_forest,param_grid=param_grid)
grid_random_forest.fit(X_train,Y_train)
print(grid_random_forest.best_params_)

Y_pred = grid_random_forest.predict(X_test)
acc_random_forest = round(grid_random_forest.score(X_train, Y_train) * 100, 2)
acc_random_forest
models = []
kfold = KFold(n_splits=5,random_state=5)
models.append(('logreg',logreg))
models.append(('grid_svc',grid_svc))
models.append(('grid_knn',grid_knn))
models.append(('gaussian',gaussian))
models.append(('grid_perceptron',grid_perceptron))
models.append(('grid_linear_svc',grid_linear_svc))
models.append(('sgd',sgd))
models.append(('grid_decision_tree',grid_decision_tree))
models.append(('grid_random_forest',grid_random_forest))
ensemble_model = VotingClassifier(estimators=models)

cross_val_score(ensemble_model, X_train, Y_train, cv=kfold)

ensemble_model.fit(X_train,Y_train)
acc_ensemble_model = round(ensemble_model.score(X_train, Y_train) * 100, 2)
acc_ensemble_model
submission = pd.DataFrame({
        "PassengerId": test_df["PassengerId"],
        "Survived": ensemble_model.predict(X_test)
    })
submission.to_csv('../submission.csv', index=False)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1763779527559055, 'fraction_numerical': 0.02047244094488189, 'mean_word_length': 6.483110761979575, 'pattern_counts': {'":': 7, '<': 12, '<?xml version=': 0, '>': 12, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '406041', 'license': 'MIT', 'n_tokens_mistral': 3871, 'n_tokens_neox': 3640, 'n_words': 589, 'path': 'code/Titanic_tutorials_for_gitchat.ipynb', 'repo_name': 'MachineLearningLiuMing/Titanic-tutorials-CN', 'size': '9525'}