当前位置:网站首页>hands-on-data-analysis 第三单元 模型搭建和评估
hands-on-data-analysis 第三单元 模型搭建和评估
2022-06-24 12:48:00 【51CTO】
hands-on-data-analysis 第三单元 模型搭建和评估
Table of Contents
1.模型搭建
1.1.导入相关库
1.2.数据集的载入
输出为:
1.3.数据集分析
输出为:
| 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 |
可以看到这些数据还是需要清洗的,清洗过后的数据集如下:
| PassengerId | Pclass | Age | SibSp | Parch | Fare | Sex_female | Sex_male | Embarked_C | Embarked_Q | Embarked_S | |
|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | 0 | 3 | 22.0 | 1 | 0 | 7.2500 | 0 | 1 | 0 | 0 | 1 |
| 1 | 1 | 1 | 38.0 | 1 | 0 | 71.2833 | 1 | 0 | 1 | 0 | 0 |
| 2 | 2 | 3 | 26.0 | 0 | 0 | 7.9250 | 1 | 0 | 0 | 0 | 1 |
| 3 | 3 | 1 | 35.0 | 1 | 0 | 53.1000 | 1 | 0 | 0 | 0 | 1 |
| 4 | 4 | 3 | 35.0 | 0 | 0 | 8.0500 | 0 | 1 | 0 | 0 | 1 |
输出为:
1.4.模型搭建
sklearn的算法选择路径

分割数据集
输出为:
输出为:
输出为:
1.5.导入模型
1.5.1.默认参数的逻辑回归模型
输出为:
1.5.2.调节参数的逻辑回归模型
输出为:
输出为:
1.5.3.默认参数的随机森林分类模型
输出为:
输出为:
1.5.4.调整参数后的随机森林分类模型
输出为:
输出为:
1.6.预测模型
一般监督模型在sklearn里面有个predict能输出预测标签,predict_proba则可以输出标签概率
输出为:
输出为:
2.模型评估
2.1.交叉验证
交叉验证有很多种,第一种是最简单的,也是很容易就想到的:把数据集分成两部分,一个是训练集(training set),一个是测试集(test set)。
不过,这个简单的方法存在两个弊端。
1.最终模型与参数的选取将极大程度依赖于你对训练集和测试集的划分方法。
2.该方法只用了部分数据进行模型的训练,未能充分利用数据集的数据。
为了解决这个问题,后面的技术人员进行了多种优化,接下来提到的就是K折交叉验证:
我们每次的测试集将不再只包含一个数据,而是多个,具体数目将根据K的选取决定。比如,如果K=5,那么我们利用七折交叉验证的步骤就是:
1.将所有数据集分成7份
2.不重复地每次取其中一份做测试集,用其他 6 份做训练集训练模型,之后计算该模型在测试集上的MSE
3.将7次的取平均得到最后的MSE

输出:
输出:
2.2.混淆矩阵
混淆矩阵是用来总结一个分类器结果的矩阵。对于k元分类,其实它就是一个k x k的表格,用来记录分类器的预测结果。

混淆矩阵的方法在sklearn中的sklearn.metrics模块
混淆矩阵需要输入真实标签和预测标签
精确率、召回率以及f-分数可使用classification_report模块
实际上模型的好坏,看混淆矩阵的主对角线即可。
2.3.ROC曲线
ROC曲线起源于第二次世界大战时期雷达兵对雷达的信号判断。当时每一个雷达兵的任务就是去解析雷达的信号,但是当时的雷达技术还没有那么先进,存在很多噪声,所以每当有信号出现在雷达屏幕上,雷达兵就需要对其进行破译。有的雷达兵比较谨慎,凡是有信号过来,他都会倾向于解析成是敌军轰炸机,有的雷达兵又比较神经大条,会倾向于解析成是飞鸟。在这种情况下就急需一套评估指标来帮助他汇总每一个雷达兵的预测信息以及来评估这台雷达的可靠性。于是,最早的ROC曲线分析方法就诞生了。在那之后,ROC曲线就被广泛运用于医学以及机器学习领域。
ROC的全称是Receiver Operating Characteristic Curve,中文名字叫【受试者工作特征曲线】
ROC曲线在sklearn中的模块为sklearn.metrics
ROC曲线下面所包围的面积越大越好

3.参考资料
边栏推荐
- Smart Policing: how to use video intelligent analysis technology to help urban policing visual comprehensive supervision and command system
- Resolve symbol conflicts for dynamic libraries
- 不用Home Assistant,智汀也开源接入HomeKit、绿米设备?
- 使用 Abp.Zero 搭建第三方登录模块(一):原理篇
- Babbitt | metauniverse daily must read: 618 scores have been announced. How much contribution has the digital collection made behind this satisfactory answer
- WPF from zero to 1 tutorial details, suitable for novices on the road
- 爱可可AI前沿推介(6.24)
- 线程同步的基石AbstractQueuedSynchronizer详解
- Beauty of script │ VBS introduction interactive practice
- Parse NC format file and GRB format file dependent package edu ucar. API learning of netcdfall
猜你喜欢

Yolov6: the fast and accurate target detection framework is open source

Detailed explanation of abstractqueuedsynchronizer, the cornerstone of thread synchronization

The agile way? Is agile development really out of date?

A hero's note stirred up a thousand waves across 10 countries, and the first-line big factories sent people here- Gwei 2022 Singapore

Babbitt | metauniverse daily must read: 618 scores have been announced. How much contribution has the digital collection made behind this satisfactory answer

爱可可AI前沿推介(6.24)

Main steps of system test

"I, an idiot, have recruited a bunch of programmers who can only" Google "

C语言中常量的定义和使用

Mlife forum | microbiome and data mining
随机推荐
C语言中常量的定义和使用
On the value foam of digital copyright works from the controversial nature of "Meng Hua Lu"
脚本之美│VBS 入门交互实战
Process basic properties
TCP triple handshake
How can ffmpeg streaming to the server save video as a file through easydss video platform?
Istio Troubleshooting: uneven grpc service load
强化学习之父Richard Sutton论文:追寻智能决策者的通用模型
Kubernetes集群部署
Comparator 排序函数式接口
"I, an idiot, have recruited a bunch of programmers who can only" Google "
Resolve symbol conflicts for dynamic libraries
SMS SMS
Experience of IOS interview strategy - App testing and launching
CVPR 2022 | 美团技术团队精选论文解读
LVGL库入门教程 - 颜色和图像
Megacli online management raid installation and use steps
Gateway processing flow of zuul source code analysis
1、贪吃蛇游戏设计
实现领域驱动设计 - 使用ABP框架 - 创建实体