import numpy as np
import pandas as pd
import seaborn as sns
from sklearn.model_selection import cross_val_score
from sklearn.ensemble import RandomForestClassifier
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import GradientBoostingClassifier
from sklearn.linear_model import LinearRegression
from sklearn.naive_bayes import GaussianNB
# 处理训练集
train_df = pd.read_csv('train.csv')
train_df = train_df[['Survived', 'Pclass', 'Sex', 'Age', 'SibSp', 'Parch', 'Fare', 'Embarked']]
train_df['Age'] = train_df['Age'].fillna(train_df['Age'].mean())
train_df['Embarked'] = train_df['Embarked'].fillna('S')
train_df = pd.get_dummies(train_df)
# 取出标签数据
train_y = train_df['Survived']
train_X = train_df.drop(['Survived'], axis=1)
# 归一化处理
is_norm = True
if is_norm:
columns = ['Pclass', 'Age', 'SibSp', 'Parch', 'Fare']
for column in columns:
train_X[column] = (train_X[column]-train_X[column].min())/(train_X[column].max()-train_X[column].min())
train_X
# 处理测试集
test_df = pd.read_csv('test.csv')
test_df_PassengerId = test_df['PassengerId']
test_df = test_df[['Pclass', 'Sex', 'Age', 'SibSp', 'Parch', 'Fare', 'Embarked']]
test_df['Age'] = test_df['Age'].fillna(test_df['Age'].mean())
test_df['Embarked'] = test_df['Embarked'].fillna('S')
test_df['Fare'] = test_df['Fare'].fillna(test_df['Fare'].mean()) # test这个才又缺失
test_df = pd.get_dummies(test_df)
test_df
test_X = test_df.copy()
# # 归一化处理
is_norm = True
if is_norm:
columns = ['Pclass', 'Age', 'SibSp', 'Parch', 'Fare']
for column in columns:
test_X[column] = (test_X[column]-test_X[column].min())/(test_X[column].max()-test_X[column].min())
test_X
# 交叉验证下哪个模型更好
clf = LinearRegression()
scores = cross_val_score(clf, train_X, train_y, cv=5)
print(scores.mean())
clf = GaussianNB()
scores = cross_val_score(clf, train_X, train_y, cv=5)
print(scores.mean())
clf = DecisionTreeClassifier(max_depth=None, min_samples_split=2, random_state=0)
scores = cross_val_score(clf, train_X, train_y, cv=5)
print(scores.mean())
clf = RandomForestClassifier(n_estimators=10, max_depth=None, min_samples_split=2, random_state=0)
scores = cross_val_score(clf, train_X, train_y, cv=5)
print(scores.mean())
clf = GradientBoostingClassifier(n_estimators=100, learning_rate=1.0, max_depth=1, random_state=0)
scores = cross_val_score(clf, train_X, train_y, cv=5)
print(scores.mean())
# 打印结果为:
# 0.36997733678159256
# 0.789031476490955
# 0.7688948491499535
# 0.7969095538067237
# 0.8137383781115382
# 看起来GradientBoostingClassifier结果更好,所以我们用这个来预测test数据
clf = GradientBoostingClassifier(n_estimators=100, learning_rate=1.0, max_depth=1, random_state=0).fit(train_X, train_y)
predictions = clf.predict(test_X)
submission = pd.DataFrame({ 'PassengerId': test_df_PassengerId,
'Survived': predictions })
submission.to_csv("submission.csv", index=False)