泰坦尼克号轮船沉没事件python完整代码
时间: 2023-06-17 17:09:10 浏览: 173
以下是泰坦尼克号数据集的 Python 完整代码:
```python
# 导入必要的库
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
# 读取数据
train_data = pd.read_csv('train.csv')
test_data = pd.read_csv('test.csv')
# 数据探索
print(train_data.info()) # 查看训练集信息
print('-'*30)
print(train_data.describe()) # 查看训练集数值型特征的统计信息
print('-'*30)
print(train_data.describe(include=['O'])) # 查看训练集非数值型特征的统计信息
print('-'*30)
print(train_data.head()) # 查看训练集前几行数据
# 数据清洗
train_data.drop(['PassengerId', 'Name', 'Ticket', 'Cabin'], axis=1, inplace=True)
test_data.drop(['Name', 'Ticket', 'Cabin'], axis=1, inplace=True)
# 填充缺失值
train_data['Age'].fillna(train_data['Age'].median(), inplace=True)
train_data['Embarked'].fillna(train_data['Embarked'].mode()[0], inplace=True)
test_data['Age'].fillna(test_data['Age'].median(), inplace=True)
test_data['Fare'].fillna(test_data['Fare'].median(), inplace=True)
# 特征工程
# 创建新特征 FamilySize 和 IsAlone
train_data['FamilySize'] = train_data['SibSp'] + train_data['Parch'] + 1
train_data['IsAlone'] = 1
train_data['IsAlone'].loc[train_data['FamilySize'] > 1] = 0
test_data['FamilySize'] = test_data['SibSp'] + test_data['Parch'] + 1
test_data['IsAlone'] = 1
test_data['IsAlone'].loc[test_data['FamilySize'] > 1] = 0
# 将 Pclass、Sex 和 Embarked 特征进行独热编码
train_onehot = pd.get_dummies(train_data[['Pclass', 'Sex', 'Embarked']])
test_onehot = pd.get_dummies(test_data[['Pclass', 'Sex', 'Embarked']])
# 合并数据集
train_data = pd.concat([train_data, train_onehot], axis=1)
test_data = pd.concat([test_data, test_onehot], axis=1)
# 删除原始特征
train_data.drop(['Pclass', 'Sex', 'Embarked'], axis=1, inplace=True)
test_data.drop(['Pclass', 'Sex', 'Embarked'], axis=1, inplace=True)
# 数据归一化
from sklearn.preprocessing import StandardScaler
scaler = StandardScaler()
train_data[['Age', 'Fare']] = scaler.fit_transform(train_data[['Age', 'Fare']])
test_data[['Age', 'Fare']] = scaler.transform(test_data[['Age', 'Fare']])
# 模型训练
from sklearn.model_selection import train_test_split
train_X, val_X, train_y, val_y = train_test_split(train_data.drop('Survived', axis=1), train_data['Survived'], test_size=0.2, random_state=0)
from sklearn.linear_model import LogisticRegression
from sklearn.ensemble import RandomForestClassifier
from sklearn.svm import SVC
from sklearn.neighbors import KNeighborsClassifier
from sklearn.naive_bayes import GaussianNB
models = [
('LR', LogisticRegression()),
('RF', RandomForestClassifier()),
('SVM', SVC()),
('KNN', KNeighborsClassifier()),
('NB', GaussianNB())
]
for model_name, model in models:
model.fit(train_X, train_y)
train_score = model.score(train_X, train_y)
val_score = model.score(val_X, val_y)
print('{} train score: {:.4f}, validation score: {:.4f}'.format(model_name, train_score, val_score))
# 模型优化
from sklearn.model_selection import GridSearchCV
# 随机森林
param_grid_rf = {
'n_estimators': [50, 100, 200],
'max_depth': [3, 5, 7],
'min_samples_split': [2, 4, 6],
'min_samples_leaf': [1, 2, 4]
}
grid_rf = GridSearchCV(RandomForestClassifier(), param_grid_rf, cv=5, n_jobs=-1)
grid_rf.fit(train_X, train_y)
print('Random Forest best parameters:', grid_rf.best_params_)
print('Random Forest best score:', grid_rf.best_score_)
# 支持向量机
param_grid_svm = {
'C': [0.1, 1, 10, 100],
'gamma': [0.01, 0.1, 1, 10],
'kernel': ['rbf', 'linear', 'poly', 'sigmoid']
}
grid_svm = GridSearchCV(SVC(), param_grid_svm, cv=5, n_jobs=-1)
grid_svm.fit(train_X, train_y)
print('SVM best parameters:', grid_svm.best_params_)
print('SVM best score:', grid_svm.best_score_)
# 最终模型
rf = RandomForestClassifier(n_estimators=100, max_depth=7, min_samples_leaf=2, min_samples_split=4)
rf.fit(train_X, train_y)
print('Random Forest train score: {:.4f}, validation score: {:.4f}'.format(rf.score(train_X, train_y), rf.score(val_X, val_y)))
# 预测测试集结果
test_y = rf.predict(test_data.drop('PassengerId', axis=1))
submission = pd.DataFrame({'PassengerId': test_data['PassengerId'], 'Survived': test_y})
submission.to_csv('submission.csv', index=False)
```
这个代码中包含了数据清洗、特征工程、模型训练和模型优化等步骤,使用的是随机森林模型进行预测。最终预测结果保存在 `submission.csv` 文件中。
阅读全文