泰坦尼克号生存率预测(持续更新中,探索性数据分析...)

这篇博客通过探索性数据分析研究泰坦尼克号数据集,关注乘客的存活率与社会经济地位(pclass)、年龄、性别等因素的关系。作者填补了Age变量的缺失值,发现Age、Pclass、Sex与生存率显著相关,而Cabin变量对生存率影响不大,因此被舍弃。分析结果显示女性和较高社会经济地位的乘客生存率更高。
摘要由CSDN通过智能技术生成
import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt

%matplotlib inline

探索性数据分析:

目的是了解数据集的要点,即对数据的初步了解。当你收到一个数据集时,你需要对数据集的变量、数据类型和变量之间的关系有一个概念。在大多数情况下,我们会在数据集中发现不完整的数据,原因可能是用户可能不想填写数据,在互联网上传输时数据丢失,或者用户可能没有数据可以填写。这是需要解决的第一步。不匹配的数据类型,如DateTime变量一般被读成对象(String),或者有时整数被读成浮点数,反之亦然。

df = pd.read_csv("D:/data_training/train.csv")
df.head(10)
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
5 6 0 3 Moran, Mr. James male NaN 0 0 330877 8.4583 NaN Q
6 7 0 1 McCarthy, Mr. Timothy J male 54.0 0 0 17463 51.8625 E46 S
7 8 0 3 Palsson, Master. Gosta Leonard male 2.0 3 1 349909 21.0750 NaN S
8 9 1 3 Johnson, Mrs. Oscar W (Elisabeth Vilhelmina Berg) female 27.0 0 2 347742 11.1333 NaN S
9 10 1 2 Nasser, Mrs. Nicholas (Adele Achem) female 14.0 1 0 237736 30.0708 NaN C

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-EZ4BYUxI-1670256264640)(attachment:data%20dictionary.png)]

Variable Notes
pclass: A proxy for socio-economic status (SES)
1st = Upper
2nd = Middle
3rd = Lower

age: Age is fractional if less than 1. If the age is estimated, is it in the form of xx.5

sibsp: The dataset defines family relations in this way…
Sibling = brother, sister, stepbrother, stepsister
Spouse = husband, wife (mistresses and fiancés were ignored)

parch: The dataset defines family relations in this way…
Parent = mother, father
Child = daughter, son, stepdaughter, stepson
Some children travelled only with a nanny, therefore parch=0 for them.

# 查看行列
df.shape
(891, 12)
# 查看各个变量的情况,发现Age以及Cabin存在大量缺失值,Embarked存在少量缺失值
df.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 891 entries, 0 to 890
Data columns (total 12 columns):
PassengerId    891 non-null int64
Survived       891 non-null int64
Pclass         891 non-null int64
Name           891 non-null object
Sex            891 non-null object
Age            714 non-null float64
SibSp          891 non-null int64
Parch          891 non-null int64
Ticket         891 non-null object
Fare           891 non-null float64
Cabin          204 non-null object
Embarked       889 non-null object
dtypes: float64(2), int64(5), object(5)
memory usage: 83.7+ KB
# 也可以通过isunll()的命令更直观地查看缺失值情况
df.isnull().sum()
PassengerId      0
Survived         0
Pclass           0
Name             0
Sex              0
Age            177
SibSp            0
Parch            0
Ticket           0
Fare             0
Cabin          687
Embarked         2
dtype: int64
# Age是数值变量,可以考虑用中位数或者平均值进行数据填充,考虑到性别和飞机舱位对乘客特征具有重要影响,因此采用该两个特征变量分组下的均值or中位数填充,
df.gr
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值