[sklearn] 官方例程-Imputing missing values before building an estimator 随机填充缺失值
官方链接:http://scikit-learn.org/dev/auto_examples/plot_missing_values.html#sphx-glr-auto-examples-plot-missing-values-py
该例程是为了说明对缺失值的随即填充训练出的estimator表现优于直接删掉有缺失字段值的estimator
例程代码及附加注释如下:
---------------------------------------------
import numpy as np from sklearn.datasets import load_boston
from sklearn.ensemble import RandomForestRegressor
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import Imputer
from sklearn.model_selection import cross_val_score
# 设定随机数种子
rng = np.random.RandomState(0)
# 载入数据 波士顿房价
dataset = load_boston()
X_full, y_full = dataset.data, dataset.target
n_samples = X_full.shape[0]
n_features = X_full.shape[1] # Estimate the score on the entire dataset, with no missing values
# 随机森林--回归 random_state-随机种子 n_estimator 森林里树的数目
estimator = RandomForestRegressor(random_state=0, n_estimators=100)
# 交叉验证分类器的准确率
score = cross_val_score(estimator, X_full, y_full).mean()
print("Score with the entire dataset = %.2f" % score) # Add missing values in 75% of the lines
missing_rate = 0.75
n_missing_samples = int(np.floor(n_samples * missing_rate))
# hstack 把两个数组拼接起来-行数需要一致
missing_samples = np.hstack((np.zeros(n_samples - n_missing_samples,
dtype=np.bool),
np.ones(n_missing_samples,
dtype=np.bool)))
# 打乱随机数组顺序
rng.shuffle(missing_samples)
missing_features = rng.randint(0, n_features, n_missing_samples) # Estimate the score without the lines containing missing values
X_filtered = X_full[~missing_samples, :]
y_filtered = y_full[~missing_samples]
estimator = RandomForestRegressor(random_state=0, n_estimators=100)
score = cross_val_score(estimator, X_filtered, y_filtered).mean()
print("Score without the samples containing missing values = %.2f" % score) # Estimate the score after imputation of the missing values
X_missing = X_full.copy()
X_missing[np.where(missing_samples)[0], missing_features] = 0
y_missing = y_full.copy()
estimator = Pipeline([("imputer", Imputer(missing_values=0,
strategy="mean",
axis=0)),
("forest", RandomForestRegressor(random_state=0,
n_estimators=100))])
score = cross_val_score(estimator, X_missing, y_missing).mean()
print("Score after imputation of the missing values = %.2f" % score) ---------------------------------------------------
补充:
A. numpy.where()用法:
[sklearn] 官方例程-Imputing missing values before building an estimator 随机填充缺失值的更多相关文章
- [sklearn]官方例程-Imputing missing values before building an estimator 随机填充缺失值
官方链接:http://scikit-learn.org/dev/auto_examples/plot_missing_values.html#sphx-glr-auto-examples-plot- ...
- Handling Missing Values
1) A Simple Option: Drop Columns with Missing Values 如果这些列具有有用信息(在未丢失的位置),则在删除列时,模型将失去对此信息的访问权限. 此外, ...
- Multi-batch TMT reveals false positives, batch effects and missing values(解读人:胡丹丹)
文献名:Multi-batch TMT reveals false positives, batch effects and missing values (多批次TMT定量方法中对假阳性率,批次效应 ...
- HarmonyOS(LiteOs_m) 官方例程移植到STM32初体验
HarmonyOS(LiteOs_m) 官方例程移植到STM32初体验 硬件平台 基于正点原子战舰V3开发板 MCU:STM32F103ZET6 片上SRAM大小:64KBytes 片上FLASH大小 ...
- Dapp开发petshop——truffle官方例程
truffle-pet-shop pet-shop是truffle的官方例程. 之前参考https://learnblockchain.cn/2018/01/12/first-dapp/的中文教程,但 ...
- 第2季:从官方例程深度学习海思SDK及API
2.1.官方mppsample的总体分析2.1.sample的整体架构(1)sample其实是很多个例程,所以有很多个main(2)每一个例程面向一个典型应用,common是通用性主体函数,我们只分析 ...
- USB3.0 图像视频传输 开发 CYUSB3014开发基础(导入官方例程) 转
CYPREE提供的FX3_SDK开发包里面有很多基础的内容,除了前面提到的几个pdf文件外,还有三个文件夹,是官方提供的基础例程.学习CYUSB3014应该就从这里开始,从这几个例程开始.例程共有三个 ...
- KEIL5 使用STM32 官方例程
1. 安装keil5,破解 网上很多安装包/教程,跳过 2.下载官方固件库 https://www.st.com/content/st_com/en.html 在这里找微处理器,STM32 stand ...
- STM32F746G-DISCO官方例程烧写
1. 首先安装STM32 ST-LINK Utility v3.9.0.exe,必须V3.9版本(官方说的) 2. 打开软件,选择External Loader,选择N25Q128A_STM32F74 ...
随机推荐
- 物理服务器Linux下软RAID和UUID方式挂载方法--Megacli64
一.业务部门需求说明:公司最近来了一批服务器,用于大数据业务部署.数据节点服务器由14块物理磁盘,其中有2块是900G的盘,12块是4T的盘.在服务器系统安装时,进入系统的BIOS界面:1)将2块90 ...
- Centos 6.8编译安装LNMP环境
Centos 6.8编译安装LNMP环境 参考资料: http://www.jb51.net/article/107429.htm https://phperzh.com/articles/1360 ...
- java和c#通过esb服务互调用组件
场景:java和c#写的服务.站点,互相任意调用.实现一切即服务. 解决方案:使用这种轻量级的esb架构,通过tcp通信解决通信传输问题,总线服务解决服务地址问题,契约解决数据交互问题.由于组件封装了 ...
- eclipse将javaSE项目导出成可执行jar包
将第三方包和项目打包到一块 step1:选中要导出的项目,右键选择Export step2:选择java/Runable JAR file step3:选择main主程序,选择第三方包打包的形式,推荐 ...
- Dev-Tips
186 Chrome DevTools: How to use Logpoints for quicker JavaScript debugging You can use the new Logpo ...
- vue-router 编程式路由
$route -> 使用它的属性 $router-> 使用它的方法 编程式的导航,即js控制跳转 //声明式:<router-link :to="..."> ...
- SSH框架的搭建和测试(Spring + Struts2 + Hibernate)
SSH框架实现了视图.控制器.和模型的彻底分离,同时还实现了业务逻辑层与持久层的分离. Spring实现了MVC中的 Controller的功能,Struts实现Web视图的功能,Hibernate则 ...
- JS 数组中对象去重 reduce 用法
对于数组对象,传统的去重方法无能为力,至于forEach().filter()等迭代方法也不好使:真正能做到优雅去重的,是ES5新增加的一个方法——reduce() 高手给的,完美方法 let log ...
- 患者信息SQL v1
select THEN '是' ELSE '否' END AS returnFlag, -- 是否退号 THEN '是' END as isAppointResource, -- 是否指定医生 a.c ...
- Entity Framework 6.0 常见异常及解决办法
Ø 简介 本文主要记录 EF(Entity Framework) 在平时的开发中可能遇到的异常,以及应该如何去解决. 1. System.InvalidOperationException 1) ...