Python: Kaggle Titannic数据集处理

该博客探讨了在Kaggle的Titanic数据集中,如何利用性别、船舱等级和登陆港口等因素影响乘客的存活率。通过分析,发现女性的存活率显著高于男性,船舱等级与存活率负相关,尤其是3等舱。年龄也是一个重要因素,15岁以下乘客存活率较高,40岁以上则差异不大。初步数据处理和特征选择能有效提升预测模型的性能。
摘要由CSDN通过智能技术生成
# list Titannic_all file

想法1:能否用某个算法求个权值矩阵,后用其.x数据,再用k-means聚类

想法2 :直接先用逻辑回归,随机森林

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns

import warnings
warnings.filterwarnings('ignore')

#% matplotlib inline
train_data = pd.read_csv('train.csv')
test_data = pd.read_csv('test.csv')
train_data.head()
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
train_data.info()
print("-" * 40)
test_data.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.6+ KB
----------------------------------------
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 418 entries, 0 to 417
Data columns (total 11 columns):
PassengerId    418 non-null int64
Pclass         418 non-null int64
Name           418 non-null object
Sex            418 non-null object
Age            332 non-null float64
SibSp          418 non-null int64
Parch          418 non-null int64
Ticket         418 non-null object
Fare           417 non-null float64
Cabin          91 non-null object
Embarked       418 non-null object
dtypes: float64(2), int64(4), object(5)
memory usage: 36.0+ KB
train_data.describe()
PassengerId Survived Pclass Age SibSp Parch Fare
count 891.000000 891.000000 891.000000 714.000000 891.000000 891.000000 891.000000
mean 446.000000 0.383838 2.308642 29.699118 0.523008 0.381594 32.204208
std 257.353842 0.486592 0.836071 14.526497 1.102743 0.806057 49.693429
min 1.000000 0.000000 1.000000 0.420000 0.000000 0.000000 0.000000
25% 223.500000 0.000000 2.000000 20.125000 0.000000 0.000000 7.910400
50% 446.000000 0.000000 3.000000 28.000000 0.000000 0.000000 14.454200
75% 668.500000 1.000000 3.000000 38.000000 1.000000 0.000000 31.000000
max 891.000000 1.000000 3.000000 80.000000 8.000000 6.000000 512.329200

性别对存活率影响

train_data['Survived'].value_counts().plot.pie(autopct = '%1.2f%%')
<matplotlib.axes._subplots.AxesSubplot at 0x1acc7eab518>

在这里插入图片描述

男性人数是女性两倍,但女性存活率大很多

train_data.groupby(['Sex','Survived']).size().plot.bar()
<matplotlib.axes._subplots.AxesSubplot at 0x1acc9f482b0>

在这里插入图片描述

train_data[['Sex','Survived']].groupby(['Sex']).sum().plot.bar()
<matplotlib.axes._subplots.AxesSubplot at 0x1acc9fa5b70>

在这里插入图片描述

train_data[['Sex','Survived']].groupby(['Sex']).mean().plot.bar()
<matplotlib.axes._subplots.AxesSubplot at 0x1acca00aa58>

在这里插入图片描述

船舱等级似乎影响更大,3等舱人最多,Survived最少

train_data.groupby(['Pclass','Sex']).size().plot.bar()
<matplotlib.axes._subplots.AxesSubplot at 0x1acca081f28>

在这里插入图片描述

train_data[['Pclass','Survived']].groupby(['Pclass']).sum().plot.bar()
<matplotlib.axes._subplots.AxesSubplot at 0x1acca0e9400>

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值