泰坦尼克号数据集做假设,并用描述性统计、统计推断、建模来分析附有python代码
时间: 2023-08-18 07:09:22 浏览: 141
基于Python的泰坦尼克号游客生存预测
5星 · 资源好评率100%
好的,以下是使用 Python 进行泰坦尼克号数据集分析的示例代码。
1. 描述性统计分析
首先,我们可以使用 Pandas 库读取数据并计算乘客的生还率、年龄、船票等级和性别的平均值、中位数和众数。
```python
import pandas as pd
# 读取数据
df = pd.read_csv('titanic.csv')
# 计算生还率
survival_rate = df['Survived'].mean()
print(f"Survival rate: {survival_rate:.2f}")
# 计算年龄的平均值、中位数和众数
age_mean = df['Age'].mean()
age_median = df['Age'].median()
age_mode = df['Age'].mode()
print(f"Age mean: {age_mean:.2f}")
print(f"Age median: {age_median:.2f}")
print(f"Age mode: {age_mode.values[0]}")
# 计算船票等级、性别的平均值、中位数和众数
pclass_mode = df['Pclass'].mode()
sex_mode = df['Sex'].mode()
print(f"Pclass mode: {pclass_mode.values[0]}")
print(f"Sex mode: {sex_mode.values[0]}")
```
2. 统计推断分析
其次,我们可以使用 Scipy 库进行假设检验,确定性别、年龄、船票等级和登船港口与生还率之间是否存在显著差异。
```python
import scipy.stats as stats
# 检验性别与生还率之间是否存在显著差异
male_survival = df[df['Sex'] == 'male']['Survived']
female_survival = df[df['Sex'] == 'female']['Survived']
t_stat, p_val = stats.ttest_ind(male_survival, female_survival)
print(f"Sex and survival rate: t-statistic={t_stat:.2f}, p-value={p_val:.4f}")
# 检验年龄与生还率之间是否存在显著差异
survived_age = df[df['Survived'] == 1]['Age'].dropna()
not_survived_age = df[df['Survived'] == 0]['Age'].dropna()
t_stat, p_val = stats.ttest_ind(survived_age, not_survived_age)
print(f"Age and survival rate: t-statistic={t_stat:.2f}, p-value={p_val:.4f}")
# 检验船票等级与生还率之间是否存在显著差异
pclass_survival = df.groupby('Pclass')['Survived'].mean()
chi2, p_val, dof, expected = stats.chi2_contingency([pclass_survival.values, [1-p for p in pclass_survival.values]])
print(f"Pclass and survival rate: chi2-statistic={chi2:.2f}, p-value={p_val:.4f}")
# 检验登船港口与生还率之间是否存在显著差异
embarked_survival = df.groupby('Embarked')['Survived'].mean()
chi2, p_val, dof, expected = stats.chi2_contingency([embarked_survival.values, [1-p for p in embarked_survival.values]])
print(f"Embarked and survival rate: chi2-statistic={chi2:.2f}, p-value={p_val:.4f}")
```
3. 建模分析
最后,我们可以使用 Scikit-learn 库构建一个预测模型,该模型可以预测乘客是否生还。我们可以使用交叉验证方法来评估模型的性能,并使用特征重要性方法来确定哪些因素对预测结果的影响最大。
```python
import numpy as np
from sklearn.model_selection import cross_val_score
from sklearn.ensemble import RandomForestClassifier
# 数据预处理
df = df[['Survived', 'Sex', 'Age', 'Pclass', 'Embarked']]
df = df.dropna()
df['Sex'] = df['Sex'].apply(lambda x: 1 if x == 'male' else 0)
df['Embarked'] = df['Embarked'].apply(lambda x: ord(x) - 65)
# 构建模型
X = df[['Sex', 'Age', 'Pclass', 'Embarked']].values
y = df['Survived'].values
clf = RandomForestClassifier()
scores = cross_val_score(clf, X, y, cv=5)
# 输出交叉验证结果和特征重要性
print(f"Cross-validation scores: {np.mean(scores):.2f}")
clf.fit(X, y)
print(f"Feature importance: {clf.feature_importances_}")
```
以上代码仅供参考,具体分析方法和结果可能因数据集和分析目的而异。
阅读全文