结构化数据处理案例:泰坦尼克号幸存者预测
- 数据分析
- 建模预测
案例介绍:当年泰坦尼克号的沉没造成了很多人的死亡,救生艇不足是造成如此多人死亡的主要原因。尽管能否活下来要看运气,但是有些群体的存活概率比其他人更高。现在给出一些乘客的信息,包括他最后是否生还。根据这些信息,我们要对其他乘客是否生还进行预测。
数据集包含两个文件:train.csv和test.csv
#解压数据集
!unzip data/data31483/titanic.zip -d work/
Archive: data/data31483/titanic.zip
inflating: work/gender_submission.csv
inflating: work/test.csv
inflating: work/train.csv
#导入需要用到的第三方库
import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline
import seaborn as sns
#加载数据集
trainSet = pd.read_csv('work/train.csv')
testSet = pd.read_csv('work/test.csv')
- 训练数据集共有891个数据,其中每个数据包含12个特征
- 测试数据集共有418个数据,其中每个数据包含11个特征,测试数据集没有Survived特征,这个特征我们要经行预测。
特征说明:
- PassengerID(ID)
- Survived(存活与否)
- Pclass(客舱等级)
- Name(姓名)
- Sex(性别)
- Age(年龄)
- Parch(直系亲属数目)
- SibSp(旁系亲属数目)
- Ticket(票编号)
- Fare(票价)
- Cabin(客舱编号)
- Embarked(上船的港口编号)
print("测试数据集大小:",testSet.shape)
print("训练数据集大小:",trainSet.shape)
trainSet.head()
测试数据集大小: (418, 11)
训练数据集大小: (891, 12)
PassengerId | Survived | Pclass | Name | Sex | Age | SibSp | Parch | Ticket | Fare | Cabin | Embarked | |
---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | 1 | 0 | 3 | Braund, Mr. Owen Harris | male | 22.0 | 1 | 0 | A/5 21171 | 7.2500 | NaN | S |
1 | 2 | 1 | 1 | Cumings, Mrs. John Bradley (Florence Briggs Th... | female | 38.0 | 1 | 0 | PC 17599 | 71.2833 | C85 | C |
2 | 3 | 1 | 3 | Heikkinen, Miss. Laina | female | 26.0 | 0 | 0 | STON/O2. 3101282 | 7.9250 | NaN | S |
3 | 4 | 1 | 1 | Futrelle, Mrs. Jacques Heath (Lily May Peel) | female | 35.0 | 1 | 0 | 113803 | 53.1000 | C123 | S |
4 | 5 | 0 | 3 | Allen, Mr. William Henry | male | 35.0 | 0 | 0 | 373450 | 8.0500 | NaN | S |
数据分析
#绘制相关矩阵
ax = sns.heatmap(trainSet[["Survived","SibSp","Parch","Age","Fare"]].corr(),annot=True, cmap = "coolwarm")
由上图可知,sibsp 和 parch 的相关度挺高,在特征处理是可能用到
#数据总览
trainSet.hist(figsize=(15,10))
array([[<matplotlib.axes._subplots.AxesSubplot object at 0x7f725650cc90>,
<matplotlib.axes._subplots.AxesSubplot object at 0x7f725650cc50>,
<matplotlib.axes._subplots.AxesSubplot object at 0x7f7255969710>],
[<matplotlib.axes._subplots.AxesSubplot object at 0x7f7255963350>,
<matplotlib.axes._subplots.AxesSubplot object at 0x7f725366d890>,
<matplotlib.axes._subplots.AxesSubplot object at 0x7f7253695e90>],
[<matplotlib.axes._subplots.AxesSubplot object at 0x7f7253648b10>,
<matplotlib.axes._subplots.AxesSubplot object at 0x7f72535fb410>,
<matplotlib.axes._subplots.AxesSubplot object at 0x7f72535a1d10>]],
dtype=object)
查看不同特征的存活情况
print(trainSet[["Pclass","Survived"