请给我jupyter代码,对泰坦尼克号进行生存预测。
时间: 2024-02-24 20:57:38 浏览: 20
好的,以下是使用Python和Jupyter Notebook进行泰坦尼克号生存预测的代码:
首先,我们需要导入所需的Python库:
```python
import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
%matplotlib inline
```
接着,我们需要读取训练数据和测试数据:
```python
train_df = pd.read_csv('train.csv')
test_df = pd.read_csv('test.csv')
```
我们可以使用`head()`函数查看数据集的前几行:
```python
train_df.head()
```
接下来,我们需要进行数据清洗和特征工程。这个过程包括:
1. 处理缺失值
2. 特征选择
3. 特征变换
这里我们先处理缺失值,使用`isnull()`函数检查缺失值:
```python
print(train_df.isnull().sum())
```
可以看到,`Age`、`Cabin`和`Embarked`这三个特征存在缺失值。我们可以使用均值填充`Age`特征的缺失值,使用众数填充`Embarked`特征的缺失值。由于`Cabin`的缺失值较多,我们可以将其删除。
```python
train_df['Age'].fillna((train_df['Age'].mean()), inplace=True)
train_df['Embarked'].fillna(train_df['Embarked'].mode()[0], inplace=True)
train_df.drop('Cabin', axis=1, inplace=True)
```
接下来,我们需要进行特征选择和特征变换。这里我们选择使用`Pclass`、`Sex`、`Age`、`SibSp`、`Parch`和`Embarked`这几个特征。我们将`Sex`特征变换为二进制,将`Embarked`特征变换为数值。最后,我们将训练数据集和测试数据集合并,用于模型训练和预测。
```python
train_df['Sex'] = train_df['Sex'].apply(lambda x: 1 if x == 'male' else 0)
train_df['Embarked'] = train_df['Embarked'].map({'S': 0, 'C': 1, 'Q': 2})
test_df['Sex'] = test_df['Sex'].apply(lambda x: 1 if x == 'male' else 0)
test_df['Embarked'] = test_df['Embarked'].map({'S': 0, 'C': 1, 'Q': 2})
test_df['Age'].fillna((test_df['Age'].mean()), inplace=True)
test_df['Fare'].fillna((test_df['Fare'].mean()), inplace=True)
X_train = train_df[['Pclass', 'Sex', 'Age', 'SibSp', 'Parch', 'Embarked']]
y_train = train_df['Survived']
X_test = test_df[['Pclass', 'Sex', 'Age', 'SibSp', 'Parch', 'Embarked']]
```
最后,我们使用逻辑回归模型进行训练和预测。
```python
from sklearn.linear_model import LogisticRegression
model = LogisticRegression()
model.fit(X_train, y_train)
predictions = model.predict(X_test)
```
如果需要评估模型的性能,可以将训练数据集划分为训练集和验证集,使用交叉验证方法进行模型评估。
```python
from sklearn.model_selection import train_test_split, cross_val_score
X_train, X_val, y_train, y_val = train_test_split(X_train, y_train, test_size=0.1)
model = LogisticRegression()
model.fit(X_train, y_train)
scores = cross_val_score(model, X_train, y_train, cv=5)
print("Accuracy: %0.2f (+/- %0.2f)" % (scores.mean(), scores.std() * 2))
```