titanic数据集_数据可视化泰坦尼克号图表预测

0782c603e46a72bf4e8e6ed65b60f8a2.png

a.前期准备:获取数据,导入数据分析包

#导入数据,忽略警告提示
import warnings
warnings.filterwarnings('ignore')
#导入处理数据包
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt 
import matplotlib as mpl
#导入数据,训练数据集,这里用train.csv
titanic_data = pd.read_csv('D:/data-simple/titanic/train.csv')
print('训练数据集:',titanic_data.shape)
返回:

训练数据集: (891, 12)

rowNum_titanic_data=titanic_data.shape[0]
print('KAGGLE训练数据集有多少行数据:',rowNum_titanic_data)
返回:

KAGGLE训练数据集有多少行数据: 891

#查看数据
titanic_data.head()

4ebb4c3cb35b323ebdb2b210be181482.png
#获取数据类型的描述统计信息
titanic_data.describe()

534c8fa2c07a4a49820ea4d0512d96fc.png
titanic_data.info()
返回:

<class 'pandas.core.frame.DataFrame'>
RangeIndex: 891 entries, 0 to 890
Data columns (total 12 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 PassengerId 891 non-null int64
1 Survived 891 non-null int64
2 Pclass 891 non-null int64
3 Name 891 non-null object
4 Sex 891 non-null object
5 Age 714 non-null float64
6 SibSp 891 non-null int64
7 Parch 891 non-null int64
8 Ticket 891 non-null object
9 Fare 891 non-null float64
10 Cabin 204 non-null object
11 Embarked 889 non-null object
dtypes: float64(2), int64(5), object(5)
memory usage: 83.7+ KB

b.处理缺失数据

'''
问题分析:数据共有891行,12列,其中3列有缺失数据,
Age列缺失177行,Cabin列缺失687行,缺失严重,
Embarked列缺失2行。Name、Ticket、Cabin对探索问题无帮助。
'''
#删除无用数据
titanic_data=titanic_data.drop(['Name','Ticket','Cabin'],axis=1)
titanic_data.info()
返回:

<class 'pandas.core.frame.DataFrame'>
RangeIndex: 891 entries, 0 to 890
Data columns (total 9 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 PassengerId 891 non-null int64
1 Survived 891 non-null int64
2 Pclass 891 non-null int64
3 Sex 891 non-null object
4 Age 714 non-null float64
5 SibSp 891 non-null int64
6 Parch 891 non-null int64
7 Fare 891 non-null float64
8 Embarked 889 non-null object
dtypes: float64(2), int64(5), object(2)
memory usage: 62.8+ KB

#检查有无重复数据
if ~titanic_data.duplicated().any(): print('titanic_data表格无重复数据') 
else: print('titanic_data有重复数据')
返回:

titanic_data表格无重复数据

#处理Age列缺失数据,用“均值”进行填充,将年龄数据修正为整型
titanic_data['Age']=titanic_data['Age'].fillna( titanic_data['Age'].mean() )
#船票价格填充均值
titanic_data['Fare'] = titanic_data['Fare'].fillna( titanic_data['Fare'].mean() )
#处理Embarked列的数据,用Embarked列的众数填充两个缺失空格
print(titanic_data['Embarked'].value_counts())
返回:

S 644
C 168
Q 77
Name: Embarked, dtype: int64

# 只有两个缺失值,我们将缺失值填充为最频繁出现的值:
# S=英国南安普顿Southampton

titanic_data['Embarked'] = titanic_data['Embarked'].fillna( 'S' )

titanic_data.info()
返回

<class 'pandas.core.frame.DataFrame'>
RangeIndex: 891 entries, 0 to 890
Data columns (total 9 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 PassengerId 891 non-null int64
1 Survived 891 non-null int64
2 Pclass 891 non-null int64
3 Sex 891 non-null object
4 Age 891 non-null float64
5 SibSp 891 non-null int64
6 Parch 891 non-null int64
7 Fare 891 non-null float64
8 Embarked 891 non-null object
dtypes: float64(2), int64(5), object(2)
memory usage: 62.8+ KB

c.展示数据(数据可视化)

#泰坦尼克号乘客生还情况
Survived_count=titanic_data['Survived'].value_counts()
print(Survived_count)
返回

0 549
1 342
Name: Survived, dtype: int64

乘客年龄分布情况

#乘客年龄分布直方图 #创建figure、subplot,并用hist作条形图
fig_Age=plt.figure(figsize=(10,5))
ax_Age=fig_Age.add_subplot(1,2,1)
titanic_data['Age'].hist(bins=10,color='g',alpha=0.3,grid=False)
#设置x轴刻度标签
ax_Age.set_xticks([0,10,20,30,40,50,60,70,80,90,100])
#添加标题,x轴标签,y轴标签
ax_Age.set_title('Hist plot of Age')
ax_Age.set_xlabel('Age')
ax_Age.set_ylabel('number of people')
#乘客年龄分布箱线图
plt.subplot(122)
titanic_data.boxplot(column='Age',showfliers=False)

07e7fc17ae86ba791aceb18b61f5710f.png
titanic_data['Age'].describe()
返回

count 891.000000
mean 29.699118
std 13.002015
min 0.420000
25% 22.000000
50% 29.699118
75% 35.000000
max 80.000000
Name: Age, dtype: float64

  • 结论:乘客年龄大概成正态分布,平均年龄29岁多,最大的80岁,最小的不到1岁(利用int()取整,不到1岁的为0).

兄弟姐妹、配偶在船上的乘客分布情况条形图

#创建figure、subplot,用plot()作柱状图
fig_SibSp = plt.figure(figsize=(10,5))
ax_SibSp=fig_SibSp.add_subplot(1,2,1)
SibSp_count=titanic_data['SibSp'].value_counts()
SibSp_count.plot(kind='bar')
#添加标题,x轴标签,y轴标签
ax_SibSp.set_title('Bar plot of SibSp')
ax_SibSp.set_xlabel('number of SibSp')
ax_SibSp.set_ylabel('number of people')
#拥有各数量的兄弟姐妹、配偶的乘客比例条形图
plt.subplot(122)
SibSp_count.div(SibSp_count.sum()).plot(kind='bar')
#添加标题,x、y轴标签
plt.title('Ratio of people in SibSp')
plt.xlabel('SibSp')
plt.ylabel('ratio')

92833cea405dbbdc58fd6341fb067407.png
  • 结论:在船上没有兄弟姐妹配偶的乘客较多,占68%.

父母、孩子在船上的乘客分布条形图

Parch_count=titanic_data['Parch'].value_counts()
#创建figure、subplot,用plot()作柱状图
fig_Parch=plt.figure(figsize=(10,5))
ax_Parch=fig_Parch.add_subplot(1,2,1)
Parch_count.plot(kind='bar')
#添加标题,x、y轴标签
ax_Parch.set_title('Bar plot of Parch')
ax_Parch.set_ylabel('number of people')
ax_Parch.set_xlabel('number of Parch')
#船上有不同数量父母孩子的乘客人数比例条形图
plt.subplot(122)
Parch_count.div(Parch_count.sum()).plot(kind='bar')
#添加标题,x、y轴标签
plt.title('Ratio of people in Parch')
plt.xlabel('Parch')
plt.ylabel('ratio')

2d90341cb301ad5cc4f96204dadc1739.png

船票价格分布情况

#创建figure、subplot,用hist做直方图
fig_Fare=plt.figure(figsize=(10,5))
ax_Fare=fig_Fare.add_subplot(1,2,1)
titanic_data['Fare'].hist(bins=10,color='g',grid=False)
#添加标题,x、y轴标签
ax_Fare.set_title('Hist plot of Fare')
ax_Fare.set_ylabel('number of people')
ax_Fare.set_xlabel('Fare')
#票价箱线图
#作箱线图
plt.subplot(122)
titanic_data.boxplot(column='Fare',showfliers=False)
#添加标题、y轴标签
plt.title('boxplot of Fare')
plt.ylabel('Fare')

c7dc72d789ce20086a2e43a022e41850.png

各等级船舱中乘客的生还情况

Pclass_data_count=titanic_data.groupby(['Pclass','Survived'])['Survived'].count().unstack()
print(Pclass_data_count)
Pclass_data_count.plot(kind='bar',stacked=True)
#添加标题,y轴标签
plt.title('Survived & Pclass')
plt.ylabel('Number of people')
返回

Survived 0 1
Pclass
1 80 136
2 97 87
3 372 119

a6ebe5a8094a5220a1f585952b3d4f1d.png
  • 结论:一等船舱216人,136人生还,生还率63%; 二等船舱184人,87人生还,生还率47.3%; 三等船舱491人,119人生还,生还率24.2%.
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
泰坦尼克号数据_泰坦尼克号数据分析报告 891名乘客中遇难乘客有549⼈,占61.6%,⽣还乘客342⼈,占38.4%.各等级船舱乘客⼈数 各等级船舱乘客⼈数 Pclass_count=titanic_data['Pclass'].value_counts().sort_index() #⽤Bar_pie()函数作条形图和饼状图 Bar_pie(Pclass_count) 三等船舱乘客最多,占55.1%;⼀等船舱次之占24.2%;⼆级船舱乘客最少,占20.7%.男⼥乘客分布情况 男⼥乘客分布情况 Sex_count=titanic_data['Sex'].value_counts() print(Sex_count) Bar_pie(Sex_count) male 577 female 314 Name: Sex, dtype: int64 男乘客有577⼈,占64.8%;⼥乘客有314⼈,占35.2%.乘客年龄分布情况 乘客年龄分布情况 In [84]: #乘客年龄分布直⽅图 #创建figure、subplot,并⽤hist作条形图 fig_Age=plt.figure(figsize=(10,5)) ax_Age=fig_Age.add_subplot(1,2,1) titanic_data['Age'].hist(bins=10,color='g',alpha=0.3,grid=False) #设置x轴刻度标签 ax_Age.set_xticks([0,10,20,30,40,50,60,70,80,90,100]) #添加标题,x轴标签,y轴标签 ax_Age.set_title('Hist plot of Age') ax_Age.set_xlabel('Age') ax_Age.set_ylabel('number of people') #乘客年龄分布箱线图 #作箱线图 plt.subplot(122) titanic_data.boxplot(column='Age',showfliers=False) #添加y轴标签 plt.ylabel('Age') plt.title('boxplot of Fare') titanic_data['Age'].describe() count 891.000000 mean 29.544332 std 13.013778 min 0.000000 25% 22.000000 50% 29.000000 75% 35.000000 max 80.000000 Name: Age, dtype: float64 乘客年龄⼤概成正态分布,平均年龄29多,最⼤的80,最⼩的不到1(利⽤int()取整,不到1的为0).兄弟姐妹、配偶在船上的 兄弟姐妹、配偶在船上的 乘客分布情况条形图 乘客分布情况条形图 #创建figure、subplot,⽤plot()作柱状图 fig_SibSp=plt.figure(figsize=(10,5)) ax_SibSp=fig_SibSp.add_subplot(1,2,1) SibSp_count=titanic_data['SibSp'].value_counts() SibSp_count.plot(kind='bar') #添加标题,x轴标签,y轴标签 ax_SibSp.set_title('Bar plot of SibSp') ax_SibSp.set_xlabel('number of SibSp') ax_SibSp.set_ylabel('number of people') #拥有各 数量的兄弟姐妹、配偶的乘客⽐例条形图 plt.subplot(122) SibSp_count.div(SibSp_count.sum()).plot(kind='bar') #添加标题,x、y轴 标签 plt.title('Ratio of people in SibSp') plt.xlabel('SibSp') plt.ylabel('ratio') 在船上没有兄弟姐妹配偶的乘客较多,占68.2%.⽗母、孩⼦在船上的乘客分布条形图 ⽗母、孩⼦在船上的乘客分布条形图 Parch_count=titanic_data['Parch'].value_counts() #创建figure、subplot,⽤plot()作柱状图 fig_Parch=plt.figure(figsize=(10,5)) ax_Parch=fig_Parch.add_subplot(1,2,1) Parch_count.plot(kind='bar') #添加标题,x、y轴标签 ax_Parch.set_title('Bar plot of Parch') ax
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值