Kaggle之Titanic: Machine Learning from Disaster

本文基于Kaggle的Titanic数据科学解决方案,分析乘客数据,探索性别、年龄、船舱等级等因素与生存率的关系,进行数据清洗、特征工程,并建立预测模型解决问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

注:本文是从kaggle官方教程Titanic Data Science Solutions翻译过来的,原则上这并不是一篇翻译文章,因为我们并没有完全的忠实于原文,权当一份注解文档更为恰当。如果有不到之处,还望谅解。如有问题,请联系hzsong@outlook.com。

一般的工作流程

  1. 定义问题
  2. 获取训练和预测数据
  3. 清洗数据
  4. 分析、识别模式并探索数据
  5. 建立模型,预测数据,解决问题
  6. 可视化工作
  7. 提交答案

问题的定义

给你一组训练数据,里面包含性别、年龄、是否存活等特征,根据这组训练数据进行数据分析,建立模型,评估模型,选择一种模型来预测另一组测试数据(其中不包括是否存活特征)每个样本的存活特征是0还是1。详见Kaggle


数据假设

从题目描述中得到的假设

  1. 女人的存活率比较高
  2. 儿童(age<?)的存活率比较高
  3. 上层阶级(Pclass=1)的存活率比较高

工作目标

Classifying:
分类,我们需要理解不同分类与我们的目标之间的关系。

Correlating
相关度,我们需要了解每个特征对目标的贡献值,也就是相关度,要了解是否具有相关性,是正相关还是负相关或者其他。

Coverting
转换,我们根据不同的模型需要可能需要把某特征的类型转变为合适的类型,比如将String类型转变为数值类型。

Completing
补全,我们需要为某些特征的缺少值插入合适的值,以方便建模

Correcting:
修正,我们需要修正某些特征可能存在的一些不正确的值,比如年龄大于四百,这显然是不正确的

Creating:
创新,我们可以根据需要利用现存的一些特征勾勒出一个或几个全新的特征。

Charting
图表,我们使用可视化的图表来揭示数据和目标之间的内涵。详见如何选择正确的图表


分析数据

# data analysis and wrangling
import pandas as pd
import numpy as np
import random as rnd

# visualization
import seaborn as sns
import matplotlib.pyplot as plt
%matplotlib inline

# machine learning
from sklearn.linear_model import LogisticRegression
from sklearn.svm import SVC, LinearSVC
from sklearn.ensemble import RandomForestClassifier
from sklearn.neighbors import KNeighborsClassifier
from sklearn.naive_bayes import GaussianNB
from sklearn.linear_model import Perceptron
from sklearn.linear_model import SGDClassifier
from sklearn.tree import DecisionTreeClassifier

获取数据

train_df = pd.read_csv('../input/train.csv')
test_df = pd.read_csv('../input/test.csv')
combine = [train_df, test_df]

初步观察数据

# preview the data
train_df.head()

这里写图片描述

train_df.tail()

这里写图片描述

特征分类

Categorical

Categorical:Survived, Sex
Ordinal:Pclass

Numerical

Continue:Age
Discrete:SibSp,Parch

Mixed data types

Ticket is a mix of numeric and alphanumeric data types. Cabin is alphanumeric

Errors or typos

Name feature may contain errors or typos

总体特征观察

train_df.info()
print('_'*40)
test_df.info()

这里写图片描述——–

缺失值

训练数据(891):Cabin(204),Age(714),Embarked(889)
测试数据(418):Cabin(91),Age(332)

特征类型

训练数据:7个特征,5个特征类型为String
测试数据:6个特征,5个特征类型为String


查看数据分布

数值类型数据分布

train_df.describe()

这里写图片描述

结论
a. 不到1%的乘客的年龄在65-80。(Age)
b. 大多数乘客(>75%)没有携带家属。(SibSp,Parch)
c. 票价差异很大,只有不到1%的乘客购买了512$的票(Fare)

离散类型数据分布

train_df.describe(include=['O'])
这里写图片描述 结论
a. Name特征的取值是唯一的。(count=unique=891)
b. Sex特征只有两个取值,其中65%为male。(top=male, freq=577/count=891)
c. Tickect特征办含有22%的数据冗余。(count=891,unique=681)
d. Cabin特征的取值有一些冗余值。(count=204,unique=147)
e. Embarked特征的取值只有三个,出现最多的是‘S’

通过图表进行数据分析

各个离散型特征和Survived的相关性

train_df[['Pclass', 'Survived']].groupby(['Pclass'], as_index=False).mean().sort_values(by='Survived', ascending=False)

这里写图片描述

train_df[["Sex", "Survived"]].groupby(['Sex'], as_index=False).mean().sort_values(by='Survived', ascending=False)

这里写图片描述

train_df[["SibSp", "Survived"]].groupby(['SibSp'], as_index=False).mean().sort_values(by='Survived', ascending=False)

1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。
### Kaggle Machine Learning Datasets and Tutorials Kaggle is a platform that provides an extensive collection of datasets, kernels (notebooks), and competitions to help individuals learn about data science and machine learning[^1]. The following sections outline the resources available on Kaggle related to machine learning. #### Datasets Kaggle hosts numerous datasets covering various domains such as healthcare, finance, social media analysis, etc. These datasets are curated by both organizations and individual contributors. Users can download these datasets directly from the website or use APIs provided by Kaggle for programmatic access[^2]. For example, one popular dataset often used in beginner-level projects includes Titanic: Machine Learning from Disaster where participants predict survival outcomes based on passenger information like age, gender, class, fare paid among others. #### Tutorials & Kernels Tutorials come under two categories - guided courses offered through partnership with experts which require registration but offer certification upon completion; secondly there exist community-contributed notebooks known as 'kernels'. Guided Courses cover topics ranging from introductory Python programming all way up advanced neural networks while Community Notebooks provide practical examples demonstrating how specific algorithms work using real-world problems alongside code snippets written primarily either R or python language depending user preference: Here’s a simple illustration showing logistic regression implementation within Jupyter Notebook environment utilizing Scikit-Learn library over Iris flower classification problem: ```python from sklearn.datasets import load_iris from sklearn.linear_model import LogisticRegression from sklearn.model_selection import train_test_split import numpy as np # Load iris dataset data = load_iris() X_train, X_test, y_train, y_test = train_test_split(data.data, data.target) clf = LogisticRegression(random_state=0).fit(X_train, y_train) print(f'Accuracy Score:{np.round(clf.score(X_test,y_test)*100)}%') ``` This script demonstrates loading IRIS sample set into memory then splitting it randomly between training/testing groups before applying standard binary classifier algorithm called Logit Regression finally printing out accuracy percentage achieved during evaluation phase against unseen test cases not part original teaching material given earlier stages process pipeline execution flow sequence order steps taken here shown above clearly explained manner easy understand follow along practice try yourself home computer system setup ready go start experimenting immediately once installed necessary software packages required run successfully without errors encountered runtime exceptions thrown unexpected situations arise need troubleshooting resolve quickly efficiently move forward continue learning journey path success achieve goals aspirations dreams become reality true!
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值