Kaggle案例(一)Titanic: Machine Learning from Disaste

1. 案例简介

Titanic 案例是Kaggle 入门案例,链接地址https://www.kaggle.com/c/titanic 。以下是摘自官网上的描述信息:

2. 分析数据

2.1 读取数据

加载训练数据

data_train = pd.read_csv("./input/train.csv")

预览数据

data_train.head()


训练集数据说明:

查看数据集信息

data_train.info()

查看有缺失值的列

ata_train.columns[data_train.isnull().any()].tolist()

计算缺失数

age_null_count = data_train.Age.isnull().sum()
cabin_null_count = data_train.Cabin.isnull().sum()
embarked_null_count = data_train.Embarked.isnull().sum()
print(‘Age列缺失:%s‘ %age_null_count)
print(‘Cabin列缺失:%s‘ %cabin_null_count)
print(‘Embarked列缺失:%s‘ %embarked_null_count)

2.2 处理数据

Age列缺失值
使用Age列中位数填充缺失值

data_train.Age.fillna(data_train.Age.median())

Cabin列缺失值
Cabin列数据缺失条目较多,计算Survived列与Cabin列数据关系

Survived_cabin = data_train.Survived[pd.notnull(data_train.Cabin)].value_counts()
print(Survived_cabin)

Survived_nocabin = data_train.Survived[pd.isnull(data_train.Cabin)].value_counts()
print(Survived_nocabin)

可以发现有Cabin信息的乘客获救几率要大。将Cabin列数据作为一个分类标签处理

Embarked列缺失值
使用Embarked列众数填充缺失值

data_train.Embarked.fillna(data_train.Embarked.mode())

2.3 数据展现

获救人数情况

# 绘制获救人数情况
data_train.Survived.value_counts().plot(kind=‘bar‘)
plt.title("获救情况")
plt.xticks([0,1], ["未获救","获救"], rotation=0)
plt.ylabel("人数")


各等级的乘客年龄分布

data_train.Age[data_train.Pclass == 1].plot(kind=‘kde‘)
data_train.Age[data_train.Pclass == 2].plot(kind=‘kde‘)
data_train.Age[data_train.Pclass == 3].plot(kind=‘kde‘)
plt.xlabel("年龄")
plt.ylabel("密度")
plt.title("各等级的乘客年龄分布")
plt.legend((‘一等舱‘, ‘二等舱‘,‘三等舱‘),loc=‘best‘)

各乘客等级的获救情况

Survived_0 = data_train.Pclass[data_train.Survived == 0].value_counts()
Survived_1 = data_train.Pclass[data_train.Survived == 1].value_counts()
df=pd.DataFrame({‘获救‘:Survived_1, ‘未获救‘:Survived_0})
df.plot(kind=‘bar‘, stacked=True)
plt.title("船舱等级的获救情况")
plt.xlabel("船舱等级")
plt.ylabel("人数")
plt.xticks(rotation=0)

绘制登船口岸上船人数

data_train.Embarked.value_counts().plot(kind=‘bar‘)
plt.title("各登船口岸上船人数")
plt.ylabel("人数")
plt.xticks(rotation=0)

各登录港口的获救情况

Survived_0 = data_train.Embarked[data_train.Survived == 0].value_counts()
Survived_1 = data_train.Embarked[data_train.Survived == 1].value_counts()
df=pd.DataFrame({‘获救‘:Survived_1, ‘未获救‘:Survived_0})
df.plot(kind=‘bar‘, stacked=True)
plt.title("登陆港口乘客的获救情况")
plt.xlabel("登陆港口")
plt.ylabel("人数")
plt.xticks(rotation=0)

各性别的获救情况

Survived_m = data_train.Survived[data_train.Sex == ‘male‘].value_counts()
Survived_f = data_train.Survived[data_train.Sex == ‘female‘].value_counts()
df=pd.DataFrame({‘男性‘:Survived_m, ‘女性‘:Survived_f})
df.plot(kind=‘bar‘, stacked=True)
plt.title("男女性别获救情况")
plt.xlabel("性别")
plt.ylabel("人数")
plt.xticks([0,1], ["未获救","获救"], rotation=0)

SibSp字段获救情况

SibSp_0 = data_train.SibSp[data_train.Survived == 0].value_counts()
SibSp_1 = data_train.SibSp[data_train.Survived == 1].value_counts()
SibSp_df=pd.DataFrame({‘未获救‘:SibSp_0, ‘获救‘:SibSp_1})
SibSp_df.plot(kind=‘bar‘,stacked=True)
plt.title("堂兄弟/妹个数获救情况")
plt.xlabel("堂兄弟/妹个数")
plt.ylabel("人数")
plt.xticks(rotation=0)

Parch字段获救情况

Parch_0 = data_train.Parch[data_train.Survived == 0].value_counts()
Parch_1 = data_train.Parch[data_train.Survived == 1].value_counts()
Parch_df=pd.DataFrame({‘未获救‘:Parch_0, ‘获救‘:Parch_1})
Parch_df.plot(kind=‘bar‘,stacked=True)
plt.title("父母与小孩个数获救情况")
plt.xlabel("父母与小孩个数")
plt.ylabel("人数")
plt.xticks(rotation=0)

原文地址:https://blog.51cto.com/12631595/2391944

时间: 2024-10-30 23:57:03

Kaggle案例(一)Titanic: Machine Learning from Disaste的相关文章

机器学习案例学习【每周一例】之 Titanic: Machine Learning from Disaster

https://zhuanlan.zhihu.com/p/25185856 [Kaggle实例分析]Titanic Machine Learning from Disasterhttp://blog.csdn.net/wiking__acm/article/details/42742961 Titanic: Machine Learning from Disaster(Kaggle 数据挖掘竞赛)http://blog.csdn.net/han_xiaoyang/article/details/

[kaggle入门] Titanic Machine Learning from Disaster

Titanic Data Science Solutions¶ https://www.kaggle.com/startupsci/titanic-data-science-solutions 数据挖掘竞赛七个步骤:¶ Question or problem definition. Acquire training and testing data. Wrangle, prepare, cleanse the data. Analyze, identify patterns, and explo

Kaggle竞赛题目之——Titanic: Machine Learning from Disaster

The sinking of the RMS Titanic is one of the most infamous shipwrecks in history.  On April 15, 1912, during her maiden voyage, the Titanic sank after colliding with an iceberg, killing 1502 out of 2224 passengers and crew. This sensational tragedy s

Titanic Machine Learning from Disaster

1. ipython demo http://nbviewer.ipython.org/github/agconti/kaggle-titanic/blob/master/Titanic.ipynb

我的第一个 Kaggle 比赛学习 - Titanic

背景 Titanic: Machine Learning from Disaster - Kaggle 2 年前就被推荐照着这个比赛做一下,结果我打开这个页面便蒙了,完全不知道该如何下手. 两年后,再次打开这个页面,看到清清楚楚的Titanic Tutorial - Kaggle,完全傻瓜式的照着做就能做下来.当年是什么蒙蔽了我的眼睛~ Target use machine learning to create a model that predicts which passengers sur

吴恩达《深度学习》-课后测验-第三门课 结构化机器学习项目(Structuring Machine Learning Projects)-Week2 Autonomous driving (case study) (case study)( 自动驾驶 (案例研究))

Week2 Autonomous driving (case study) (case study)( 自动驾驶 (案例研究)) \1. To help you practice strategies for machine learning, in this week we'll present another scenario and ask how you would act. We think this "simulator" of working in a machine l

【机器学习实战】Machine Learning in Action 代码 视频 项目案例

MachineLearning 欢迎任何人参与和完善:一个人可以走的很快,但是一群人却可以走的更远 Machine Learning in Action (机器学习实战) | ApacheCN(apache中文网) 视频每周更新:如果你觉得有价值,请帮忙点 Star[后续组织学习活动:sklearn + tensorflow] ApacheCN - 学习机器学习群[629470233] 第一部分 分类 1.) 机器学习基础 2.) k-近邻算法 3.) 决策树 4.) 基于概率论的分类方法:朴素

Python (1) - 7 Steps to Mastering Machine Learning With Python

Step 1: Basic Python Skills install Anacondaincluding numpy, scikit-learn, and matplotlib Step 2: Foundational Machine Learning Skills Unofficial Andrew Ng course notes Tom Mitchell Machine Learning Lectures Step 3: Scientific Python Packages Overvie

机器学习(Machine Learning)&深度学习(Deep Learning)资料

机器学习(Machine Learning)&深度学习(Deep Learning)资料 機器學習.深度學習方面不錯的資料,轉載. 原作:https://github.com/ty4z2008/Qix/blob/master/dl.md 原作作者會不斷更新.本文更新至2014-12-21 <Brief History of Machine Learning> 介绍:这是一篇介绍机器学习历史的文章,介绍非常全面.从感知机.神经网络.决策树.SVM.Adaboost到随机森林.Deep L