使用逻辑回归训练模型(max_iter=10000)对泰坦尼克号乘客生存预测,输出预测结果(截屏)
时间: 2023-12-26 22:06:57 浏览: 58
预测泰坦尼克号乘客幸存率
抱歉,我无法进行截屏操作,但我可以提供代码演示如何使用逻辑回归对泰坦尼克号乘客生存进行预测,您可以在本地运行该代码并查看预测结果。
```python
import pandas as pd
from sklearn.linear_model import LogisticRegression
# 加载数据集
train_data = pd.read_csv('train.csv')
test_data = pd.read_csv('test.csv')
# 特征选择
features = ['Pclass', 'Sex', 'Age', 'SibSp', 'Parch', 'Fare', 'Embarked']
train_features = train_data[features]
train_labels = train_data['Survived']
test_features = test_data[features]
# 特征预处理
train_features['Age'].fillna(train_features['Age'].mean(), inplace=True)
test_features['Age'].fillna(test_features['Age'].mean(), inplace=True)
train_features['Fare'].fillna(train_features['Fare'].mean(), inplace=True)
test_features['Fare'].fillna(test_features['Fare'].mean(), inplace=True)
train_features['Embarked'].fillna('S', inplace=True)
test_features['Embarked'].fillna('S', inplace=True)
train_features = pd.get_dummies(train_features)
test_features = pd.get_dummies(test_features)
# 模型训练
lr = LogisticRegression(max_iter=10000)
lr.fit(train_features, train_labels)
# 预测
predictions = lr.predict(test_features)
print(predictions)
```
输出的预测结果为一个包含 0 和 1 的一维数组,表示每个乘客是否幸存。
阅读全文