一、任务基础

泰坦尼克号沉没是历史上最著名的沉船事故之一。1912年4月15日,在她的处女航中,泰坦尼克号在与冰山相撞后沉没,在2224名乘客和机组人员中造成1502人死亡。这场耸人听闻的悲剧震惊了国际社会,并为船舶制定了更好的安全规定。造成海难失事的原因之一是乘客和机组人员没有足够的救生艇。尽管幸存下沉有一些运气因素,但有些人比其他人更容易生存,例如妇女,儿童和上流社会。在这个案例中我们将运用机器学习来预测哪些乘客可以幸免于悲剧。

数据集链接:https://pan.baidu.com/s/1bVnIM5JVZjib1znZIDn10g 。提取码:1htm 。

读取titanic_train数据集

import pandas# 读取数据集
titanic = pandas.read_csv('titanic_train.csv')
titanic.head(10)

查看数据集前10行

特征名词解释

特征名称 特征解释
PassengerId    乘客id,对结果没有影响
Survived 1表示存活,0表示未存活
Pclass 船舱等级,越有钱船舱等级越高,所以对结果有影响
Name 乘客名字,先暂时认为对结果没有影响
Sex 性别,毫无疑问,女生优先,所以肯定对结果有影响
Age 年龄,不用说这列也有影响
SibSp 兄弟姐妹,对结果也有影响
Parch 父母和小孩,不用说也有影响
Ticket 票的编号,貌似没啥影响
Fare  船票价格,和船舱等级一样,不能忽略
Cabin 船舱号,应该也没啥影响
Embarked 登船地点,不同地点登船可能身份不一样

二、数据预处理

可以看到Age列有缺失值(NaN)。一般来说,数据发生缺失的话有两种处理方法,一种填充缺失值,一种直接舍弃这个特征。这里一般来说Age对结果是有较大影响的,我们可以对缺失值进行填充,这里可以填充平均值 。

# Age 缺失值填充
titanic['Age'] = titanic['Age'].fillna(titanic['Age'].median())
print(titanic.describe())

填充后查看数据集的描述

  

机器学习算法一般来说解决不了对字符的分类。因为我们是要对Survived这列‘’0‘’和"1"进行分类嘛。所以我们就要把"Sex"这一列的数据进行处理,把它映射为数值型。那我们就把"male"和“female”进行处理,分别用0和1替代。

# print(titanic['Sex'].unique())# Replace all the occurences of male with the number 0.
# 用数字0替换所有出现的男性。
titanic.loc[titanic["Sex"] == "male", "Sex"] = 0
titanic.loc[titanic["Sex"] == "female", "Sex"] = 1

同时,我们也把"Embarked"这一列数据进行同样的处理

# print(titanic['Embarked'].unique())# Embarked:上船港口,有三个取值,C/S/Q,是文字形式,不利于分析,
# 故可能需要映射到数值的值,而且有2个缺失值
titanic['Embarked'] = titanic['Embarked'].fillna('S') # 缺失值填充为这一列的众数'S'
titanic.loc[titanic["Embarked"] == "S", "Embarked"] = 0
titanic.loc[titanic["Embarked"] == "C", "Embarked"] = 1
titanic.loc[titanic["Embarked"] == "Q", "Embarked"] = 2

三、分类任务

首先使用线性回归算法来进行分类

# Import the linear regression(回归) class
# 注意不要导错库
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import KFold# The columns we'll use to predict the target
predictors = ['Pclass', 'Sex', 'Age', 'SibSp', 'Parch', 'Fare', 'Embarked']# Initialize our algorithm class
alg = LinearRegression()# Generate cross validation folds for the titanic dataset. It return the row indices
# corresponding(相应的) to train and test.
# 为Titanic数据集生成交叉验证折叠。它返回与训练和测试相对应的行索引。
# We set random_state to ensure we get the same splits every time we run this.
# kf = KFold(titanic.shape[0], n_folds=3, random_state=1) 写法错误已被弃用# 样本平均分成3份,3折交叉验证
kf = KFold(n_splits=3,shuffle=False, random_state=1)# 注意这里不是kf.split(titanic.shape[0]),会报如下错误:
# Singleton array array(891) cannot be considered a valid collection.predictions = []# 交叉验证 划分训练集 验证集
for train, test in  kf.split(titanic):# The predictors we're using the train the algorithm. Note how we only take# the rows in the train folds# 注意我们只得到训练集的rowstrain_predictors = titanic[predictors].iloc[train, :]# The target we're using to train the algorithm.train_target = titanic['Survived'].iloc[train]# Training the algorithm using the predictors and targetalg.fit(train_predictors, train_target)# We can now make predictions on the test foldtest_predictions = alg.predict(titanic[predictors].iloc[test, :])predictions.append(test_predictions)

查看线性回归准确率

import numpy as np# The predictions are in three separate numpy arrays. Concatenate them into one.
# We concatenate them on asix 0, as they only have one axis.
predictions = np.concatenate(predictions,axis=0)# Map predictions to outcomes (only possible outcomes are 1 and 0)
predictions[predictions > .5] = 1  # 映射成分类结果 计算准确率
predictions[predictions <= .5] = 0# 注意这一行与源代码有出入
accuracy = sum(predictions==titanic['Survived'])/len(predictions)# 验证集的准确率
print(accuracy)

得到的准确率为

0.7833894500561167

对于一个二分类问题来说,这个准确率似乎不太行,接下来用逻辑回归算法试下

from sklearn.model_selection import cross_val_score
from sklearn.linear_model import LogisticRegressionalg = LogisticRegression(random_state=1)
# Compute the accuracy score for all the cross validation folds,
# (much simper than what we did before!)
scores = cross_val_score(alg, titanic[predictors], titanic["Survived"], cv=3)
# Take the mean of the scores (because we have one for each fold)
print(scores.mean())

得到的准确率为,可以发现效果要好了一点。

0.8047138047138048

上面得到的结果都是对交叉验证后的验证集来进行分类,在实际结果中,应该使用测试数据集来进行分类。

读取测试数据集并填充数据集,然后进行数值映射,与上面类似。

titanic_test = pandas.read_csv("test.csv")
titanic_test["Age"] = titanic_test["Age"].fillna(titanic["Age"].median())
titanic_test["Fare"] = titanic_test["Fare"].fillna(titanic_test["Fare"].median())
titanic_test.loc[titanic_test["Sex"] == "male", "Sex"] = 0
titanic_test.loc[titanic_test["Sex"] == "female", "Sex"] = 1
titanic_test["Embarked"] = titanic_test["Embarked"].fillna("S")titanic_test.loc[titanic_test["Embarked"] == "S", "Embarked"] = 0
titanic_test.loc[titanic_test["Embarked"] == "C", "Embarked"] = 1
titanic_test.loc[titanic_test["Embarked"] == "Q", "Embarked"] = 2

通过上面发现,似乎线性回归,逻辑回归这类算法似乎不太行,那这次再用随机森林算法来试下(一般来说随机森林算法比线性回归和逻辑回归算法的效果好一点),注意随机森林参数的变化。

from sklearn.model_selection import cross_val_score
from sklearn.model_selection import KFold
from sklearn.ensemble import RandomForestClassifierpredictors = ["Pclass", "Sex", "Age", "SibSp", "Parch", "Fare", "Embarked"]# Initialize our algorithm with the default paramters
# n_estimators is the number of trees we want to make
# min_samples_split is the minimum number of rows we need to make a split
# min_samples_leaf is the minimum number of samples we can have at the place where a
# tree branch(分支) ends (the bottom points of the tree)
alg = RandomForestClassifier(random_state=1,n_estimators=10,min_samples_split=2,min_samples_leaf=1)
# Compute the accuracy score for all the cross validation folds.  (much simpler than what we did before!)
kf = KFold(n_splits=3, shuffle=False, random_state=1)
scores = cross_val_score(alg, titanic[predictors], titanic["Survived"], cv=kf)# Take the mean of the scores (because we have one for each fold)
print(scores.mean())

准确率为:

0.7856341189674523

发现准确率还是不太行。在机器学习中,调整参数也是非常重要的,一般通过参数的调整来对模型进行优化。这次调整随机森林的参数。

alg = RandomForestClassifier(random_state=1,n_estimators=100,min_samples_split=4,min_samples_leaf=2)
# Compute the accuracy score for all the cross validation folds.  (much simpler than what we did before!)
kf = KFold(n_splits=3, shuffle=False, random_state=1)
scores = cross_val_score(alg, titanic[predictors], titanic["Survived"], cv=kf)# Take the mean of the scores (because we have one for each fold)
print(scores.mean())

得到的准确率为:

0.8148148148148148

未完待续。。。

转载于:https://www.cnblogs.com/xiaoyh/p/11321780.html

机器学习项目实战----泰坦尼克号获救预测(一)相关推荐

  1. 机器学习项目实战----泰坦尼克号获救预测(二)

    四.特征重要性衡量 通过上面可以发现准确率有小幅提升,但是似乎得到的结果还是不太理想.我们可以发现模型似乎优化的差不多了,使用的特征似乎也已经使用完了.准确率已经达到了瓶颈,但是如果我们还想提高精度的 ...

  2. 机器学习项目:泰坦尼克号幸存者预测

    随机森林分类模型 import pandas as pd from sklearn.ensemble import RandomForestClassifier from sklearn.model_ ...

  3. python泰坦尼克号数据预测_机器学习入门之Python机器学习:泰坦尼克号获救预测一...

    本文主要向大家介绍了机器学习入门之Python机器学习:泰坦尼克号获救预测一,通过具体的内容向大家展现,希望对大家学习机器学习入门有所帮助. 一.项目概要 1.应用 模式识别.数据挖掘(核心).统计学 ...

  4. 机器学习项目实战(五) 住房价格预测

    机器学习项目实战系列   住房价格预测 目录 机器学习项目实战系列   住房价格预测 一.概述 二.分析数据 1.数据导入 2.基础统计运算 3.特征观察 4.建立模型 5.分析模型表现 (1)学习曲 ...

  5. Python机器学习/数据挖掘项目实战 波士顿房价预测 回归分析

    Python机器学习/数据挖掘项目实战 波士顿房价预测 回归分析 此数据源于美国某经济学杂志上,分析研究波士顿房价( Boston HousePrice)的数据集. 在这个项目中,你将利用马萨诸塞州波 ...

  6. python智慧城市_智慧城市背景下Python机器学习项目实战案例分享

    首先,何为智慧城市?智慧城市的"智慧"源自何处? 智慧城市的特征在于运用大数据和数字技术提高居民生活质量. 机构获得的数据越全面.越实时,它们就越有能力观测事件发生的详情.分析需求 ...

  7. 机器学习项目实战——02回归算法之葡萄酒质量与时间的关系

    数据集,机器学习项目实战--数据集与代码-机器学习文档类资源-CSDN文库 采用的是一元线性回归模型 import numpy as np import matplotlib.pyplot as pl ...

  8. 智慧城市背景下Python机器学习项目实战案例分享

    首先,何为智慧城市?智慧城市的"智慧"源自何处? 智慧城市的特征在于运用大数据和数字技术提高居民生活质量. 机构获得的数据越全面.越实时,它们就越有能力观测事件发生的详情.分析需求 ...

  9. 机器学习项目实战-能源利用率 Part-3(特征工程与特征筛选)

    博主前期相关的博客可见下: 机器学习项目实战-能源利用率 Part-1(数据清洗) 机器学习项目实战-能源利用率 Part-2(探索性数据分析) 这部分进行的特征工程与特征筛选. 三 特征工程与特征筛 ...

最新文章

  1. python time datetime string 相互转换
  2. java字符串格式化:String.format()方法的使用
  3. 如何让for循环中最后一次的输出和循环体的不一样
  4. C#8.0可空引用类型的使用注意要点 1
  5. 它打败了欧几里得空间,踹飞了数学怪物,成为全世界的焦点
  6. linux中ssh启动报错,Linux(Ubuntu18)中启动ssh时的报错
  7. Laravel核心解读--Database(三) 模型CRUD
  8. 开发者必备Mysql命令
  9. 超能搜索系统输入关键字_电商后台设计——搜索
  10. 怎么用计算机拨号手机,教你如何用电脑连接手机自动打电话
  11. win10无法安装迅雷精简版解决办法
  12. 编程实现英语句子按单词倒叙-C语言
  13. shineblink LD3320A语音识别
  14. python怎么选取不连续的列_用pandas中的DataFrame时选取行或列的方法
  15. 软考初级信息处理技术员基础知识考点(建议收藏)
  16. 台式计算机开机黑屏,电脑开不了机屏幕没显示如何办?台式电脑开机黑屏维修技巧...
  17. 由中序后序序列求前序序列
  18. 稀缺性原则适用于软件产品
  19. CVPR2021 最佳论文 Giraffe,当之无愧的最佳,或开创新的篇章
  20. 手机长曝光是什么意思_手机摄影丨手把手教你如何实现手机长曝光

热门文章

  1. python 不显示图片 直接保存图片 matplotlib 后端执行指令
  2. oracle中序号生成器,Oracle序列生成器
  3. 编写一个Applet,画出20条水平的、随机颜色的平行线。要求线的长度相同,整条线段都要在可视区域内
  4. 华为的鸿蒙系统是安卓吗,华为鸿蒙,一个本属于2025年的产品
  5. 只在用户登陆时运行_linux 系统的7个运行级别
  6. tomcat上的javaweb项目如何将ip地址更换为域名_Java Web 路线规划
  7. 计算机视觉基础:图像处理 Task 03 - 颜色空间互转
  8. 爱奇艺NLP:BiLSTM_CRF的关键词自动抽取
  9. 高等院校计算机考试等级,全国高等院校计算机等级试考试大纲.doc
  10. slf4j mysql_能显示sql语句,没有报错,但是MySQL里没有表?还有,slf4j是必须的吗?...