官方链接: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 随机填充缺失值的更多相关文章

  1. [sklearn]官方例程-Imputing missing values before building an estimator 随机填充缺失值

    官方链接:http://scikit-learn.org/dev/auto_examples/plot_missing_values.html#sphx-glr-auto-examples-plot- ...

  2. Handling Missing Values

    1) A Simple Option: Drop Columns with Missing Values 如果这些列具有有用信息(在未丢失的位置),则在删除列时,模型将失去对此信息的访问权限. 此外, ...

  3. Multi-batch TMT reveals false positives, batch effects and missing values(解读人:胡丹丹)

    文献名:Multi-batch TMT reveals false positives, batch effects and missing values (多批次TMT定量方法中对假阳性率,批次效应 ...

  4. HarmonyOS(LiteOs_m) 官方例程移植到STM32初体验

    HarmonyOS(LiteOs_m) 官方例程移植到STM32初体验 硬件平台 基于正点原子战舰V3开发板 MCU:STM32F103ZET6 片上SRAM大小:64KBytes 片上FLASH大小 ...

  5. Dapp开发petshop——truffle官方例程

    truffle-pet-shop pet-shop是truffle的官方例程. 之前参考https://learnblockchain.cn/2018/01/12/first-dapp/的中文教程,但 ...

  6. 第2季:从官方例程深度学习海思SDK及API

    2.1.官方mppsample的总体分析2.1.sample的整体架构(1)sample其实是很多个例程,所以有很多个main(2)每一个例程面向一个典型应用,common是通用性主体函数,我们只分析 ...

  7. USB3.0 图像视频传输 开发 CYUSB3014开发基础(导入官方例程) 转

    CYPREE提供的FX3_SDK开发包里面有很多基础的内容,除了前面提到的几个pdf文件外,还有三个文件夹,是官方提供的基础例程.学习CYUSB3014应该就从这里开始,从这几个例程开始.例程共有三个 ...

  8. KEIL5 使用STM32 官方例程

    1. 安装keil5,破解 网上很多安装包/教程,跳过 2.下载官方固件库 https://www.st.com/content/st_com/en.html 在这里找微处理器,STM32 stand ...

  9. STM32F746G-DISCO官方例程烧写

    1. 首先安装STM32 ST-LINK Utility v3.9.0.exe,必须V3.9版本(官方说的) 2. 打开软件,选择External Loader,选择N25Q128A_STM32F74 ...

随机推荐

  1. M1-Flask-Day3

    内容概要: websocket mysql连接池 sqlalchemy flask-sqlalchemy 练习: 1. 谈谈Flask和Django的认识? Django大而全的框架,把Web相关设计 ...

  2. Python递归_打印节点信息

    Python递归_打印节点信息 递归特性:1.必须由一个明确的结束条件2.每次进入更深一层递归时,问题规模相比上一次递归都应该有所减少3.递归效率不高,递归层次过多会导致栈溢出(在计算机中,函数调用时 ...

  3. Java的一些基本术语

    1. 反射 获取类本身,就叫“反射”,有以下3种方式: // 通过“实例”获取类 String str = "hello"; Class cls1 = str.getClass() ...

  4. MySQL5.7延迟复制半同步复制

    MySQL5.7延迟复制半同步复制 作者:尹正杰  版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.延迟复制 1>.什么是延迟复制 延迟复制是指定从库对主库的延迟至少是指定的这个间隔时间 ...

  5. 《Apache Kafka实战》读书笔记-调优Kafka集群

    <Apache Kafka实战>读书笔记-调优Kafka集群 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.确定调优目标 1>.常见的非功能性要求 一.性能( ...

  6. java 可设置最大内存

    测试方法:在命令行下用 java -XmxXXXXM -version 命令来进行测试,然后逐渐的增大XXXX的值,如果执行正常就表示指定的内存大小可用,否则会打印错误信息. 堆(Heap)和非堆(N ...

  7. MapReduce-WordCount

    pom.xml <?xml version="1.0" encoding="UTF-8"?> <project xmlns="htt ...

  8. [NIO-2]通道和FileChannel

    通道是什么 通道式(Channel)是java.nio的第二个主要创新.通道既不是一个扩展也不是一项增强,而是全新的.极好的Java I/O示例,提供与I/O服务的直接连接.Channel用于在字节缓 ...

  9. JQuery和ASP.NET分别实现级联下拉框效果

    在学习JQuery之前知道下拉框的级联效果可以通过asp.net控件实现,现在学习了JQuery,知道了JQuery和select也能实现.我分别举两个小例子说明这两种方法如何实现. 1.用JQuer ...

  10. 032、学容器必须懂bridge网络(2019-02-19 周二)

    参考https://www.cnblogs.com/CloudMan6/p/7066851.html   docker安装时会创建一个名为 docker0 的linuxbridge.如果不指定 --n ...