Titanic数据分析

人工智能34

背景

题目参见:Titanic
实际就是根据乘客的性别、年龄、舱位等级等信息预测乘客的成活率。很多人也说这个数据集比较小,使用一些tricky的技术提高准群率意义不大,但是作为练手的项目还是很好的。帮助理解各种数据预处理方法、分类模型还是很有帮助的。

分析和处理

  1. 纯Naive的做法:不管三七二十一,输出Survived=0或者1.这当然是一种做法,但是基本没啥意义,但是可以作为之后分类器准确率的参照;
  2. 对数据稍作分析:
df = pd.read_csv('train.csv')
dd = df.groupby('Survived')['Survived'].count()
print(dd.head())
dd.plot(kind='pie', autopct='%1.1f')
plt.show()

从饼状图上看,幸存的人数仍然很少,这实际上可以预测测试集上左右两名乘客的死亡情况,这样正确率可以达到50%以上。当然,这种做法还是没有任何意义,但我们被给予了最低基准,准确率不能低于61%,否则模型肯定有问题,最好不要使用模型。

[En]

Judging from the pie chart, there are still a small number of survivors, which can actually predict the death of both left and right passengers on the test set, so that the correct rate can be more than 50%. Of course, this practice still does not make any sense, but we are given the lowest benchmark, the accuracy rate can not be less than 61%, otherwise there must be something wrong with the model, it is better not to use the model.

Titanic数据分析
3. 那我们分各个维度来看看有没有明显的差异。
3.1 性别

df = pd.read_csv('train.csv')
df = df.groupby('Survived')
df0 = df.get_group(0).groupby('Sex')['Sex'].count()
df1 = df.get_group(1).groupby('Sex')['Sex'].count()
df = pd.DataFrame({"S": df1, "D": df0})
df.plot(kind='pie', autopct='%1.1f', subplots=True, figsize=(8, 8))
plt.show()

可见大部分男性都死亡了,大部分女性都存活了。
Titanic数据分析
使用pivot_table看看

df = pd.read_csv('train.csv')
dd = pd.pivot_table(df, index=['Sex'], columns=['Survived'], values=['Pclass'], aggfunc=[len])
print(dd)

在jupyter里看起来是这样的:
Titanic数据分析
加入我们,简单地说,所有的男人都死了,所有的女人都活着,可以达到预期的争夺率。

[En]

Join us to simply say that all men are dead, all women are alive, and the expected contention rate can be achieved.

233 + 468 81 + 233 + 468 + 109 = 78.6 % \frac {233+468} {81+233+468+109} = 78.6\%8 1 +2 3 3 +4 6 8 +1 0 9 2 3 3 +4 6 8 ​=7 8 .6 %
使用这种方式提交,我们确实可以得到一个比较高的正确率,数据证明可以达到76.55%。所以如果一个模型的正确率连76.55%都打不到,那数据的处理一定是有问题,要么就是模型学习不充分或者过拟合,要么就是特征处理有问题。
3.2 年龄
年龄的处理比较特别:

  • 年龄有177字段是缺失的
  • 年龄有些小数的数值
  • 因为年龄跨度大,但是样本比较少,某些年龄数据可能会很稀疏

对于缺失字段,我们可以1. drop缺失字段;2. 使用缺省值、特殊值进行填充(比如-1或者0等);3. 使用均值或者众数或者分组众数进行填充;
对于2和3,我们可以使用类似方法进行处理:对不同的年龄进行分组(分桶)。分桶的方式有:1. 区间段分组;2. 哈希分组。相邻的年龄具有相关性,当然使用哈希分组更合理。
在处理丢失的数据时,我们可能会产生更多有用的信息,或者我们可能会丢失信息。事实上,在分析数据之前,我们还不知道结论。因此,在分析中,我们首先处理的是年龄是否缺失。为了简化处理过程,我们将年龄划分为5岁为间隔,遗漏任务为-5岁作为特殊处理。

[En]

When dealing with missing data, we may produce more useful information, or we may lose information. In fact, we don't know the conclusion before the data are analyzed. Therefore, in the analysis, we first deal with whether the age is missing or not. In order to simplify the processing process, we divide the age into 5 years old as the interval, and the missing assignment is-5 years old as a special treatment.

df = pd.read_csv('models/titanic/train.csv')
df.loc[df.Age.isnull(),'Age']=-5
df.Age = df.Age // 5 * 5
dd = df.pivot_table(index=['Age'], columns = ['Survived'], values=['PassengerId'],aggfunc=[len]).copy()
dd['Win'] = np.where(dd[('len', 'PassengerId', 0)]>dd[('len', 'PassengerId', 1)],dd[('len', 'PassengerId', 0)],dd[('len', 'PassengerId', 1)])
dd['Win'].sum()/891

Titanic数据分析
如果按照年龄来推断是否存活,则胜率可以得到62%(其实并不高)
各个年龄段的分布如下
Titanic数据分析

3.3 舱位

df = pd.read_csv('models/titanic/train.csv')
df.pivot_table(index=['Pclass'], columns = ['Survived'], values=['PassengerId'],aggfunc=[len])

诚然,不同客舱的乘客存活率差异很大,但如果仅利用客舱信息进行预测,准确率可达67%,仅略高于完全存活或完全死亡。

[En]

It is true that the survival rate of passengers in different cabins varies greatly, but if only cabin information is used for prediction, the accuracy can reach 67%, which is only slightly higher than full survival or total death.

Titanic数据分析

这里就有两个问题了:

  1. 如何更快的定位哪个特征最重要?(pandas 相关性分析、决策树的顶层节点)
  2. 组合特征是否能取得更好的效果?
    我们把舱位和性别两个特征结合起来看
df = pd.read_csv('models/titanic/train.csv')
df.pivot_table(index=['Pclass','Sex'], columns = ['Survived'], values=['PassengerId'],aggfunc=[len])

Titanic数据分析
诚然,划分更详细,但总结后发现,仅以性别作为特征并不能提高准确率(或所有男性都死了,女性都活着)

[En]

It is true that the division is more detailed, but after the summary, it is found that using only gender as a feature does not improve the accuracy (or all men are dead and women are alive)

df = pd.read_csv('models/titanic/train.csv')
df.loc[df.Age.isnull(),'Age'] = df.Age.mean()
df.Age = df['Age'] // 3 * 3
mapping = {'male':0,'female':1}
df['Sex']=df['Sex'].map(lambda r: mapping[r])
df.loc[df.Age.isnull(),'Age']=0
df = df[['Survived', 'Age','Pclass', 'Sex','SibSp','Fare']]
df.corr()

Titanic数据分析
从这张图表还可以看出,相关性最大的数据是存活率和性别,以及门票和级别(0.5以上),这更自然。

[En]

It can also be seen from this chart that the data with the greatest correlation are survival rate and sex, as well as ticket and class (above 0.5), which is more natural.

Original: https://blog.csdn.net/zhshuai1/article/details/121759428
Author: zhshuai1
Title: Titanic数据分析

相关文章
自动驾驶-激光雷达预处理/特征提取 人工智能

自动驾驶-激光雷达预处理/特征提取

0. 简介 激光雷达作为自动驾驶最常用的传感器,经常需要使用激光雷达来做建图、定位和感知等任务。而这时候使用降低点云规模的预处理方法,可以能够去除无关区域的点以及降低点云规模。并能够给后续的PCL点云...
OWLAPI 人工智能

OWLAPI

基础知识 RDF Formats 开源库 JENA OWLAPI OWLAPI使用 protege ExtensionLanguage.ttlTurtle.ntN-Triples.nqN-Quads....
快速上手:图聚类入门 Graph Clustering 人工智能

快速上手:图聚类入门 Graph Clustering

硕士研究工作基本告一段落了,静候佳音中~ 其实一直想总结一下图节点聚类的一些工作,算是一个 逗号吧。 个人总结,若有错误欢迎指正。 本文从问题定义入手,再到近几年的工作,最后进行横向对比,并提供一些个...
智能遥感:AI赋能遥感技术 人工智能

智能遥感:AI赋能遥感技术

随着人工智能的发展和落地应用,以地理空间大数据为基础,利用人工智能技术对遥感数据智能分析与解译成为未来发展趋势。本文以遥感数据转化过程中对观测对象的整体观测、分析解译与规律挖掘为主线,通过综合国内外文...
PCL实现对点云指定区域的分割 人工智能

PCL实现对点云指定区域的分割

PCL实现对点云指定区域的分割 通常我们从激光雷达或者深度相机中拿到的数据中包含有大量的背景区域,而我们想要的数据有时候时候可能只是其中的一小部分。对于目标区域的提取通常采用算法的形式,但是在点云数据...