[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 ...
随机推荐
- 选择排序Java版
package dataStructureAlgorithmReview.day01; import java.util.Arrays; /** * * @author shundong * */ p ...
- Java运算符和引用数据类型(Scanner、Random)
运算符 算术运算符: 运算符 运算规则 范例 结果 + 正号 +3 3 + 加 2+3 5 + 连接字符串 “中”+“国” “中国” - 负号 int a=3;-a -3 - 减 3-1 2 * 乘 ...
- linux报错汇总
一.出现cannot send message: Process exited with a non-zero status错误 查看log文件:sudo cat /var/log/mail.err, ...
- python3 rrdtool 使用
源自 python自动化运维:技术与最佳实践 并做略微修改 安装 yum install python-rrdtoolyum install rrdtool-devel #因为采集用了psutil模块 ...
- SQL语法基础之ALTER语句
SQL语法基础之ALTER语句 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.查看ALTER的帮助信息 mysql> ? ALTER Many help items fo ...
- mssql的 for xml path 与 mysql中的group_concat类似MSSQL For xml Path
/****** Script for SelectTopNRows command from SSMS ******/ SELECT D_ID,[D_Name] as Name FROM [LFBMP ...
- Sidetiq 定时任务
class SidekiqCreateMonthPlanWorker #定时自动生成下月计划 include Sidekiq::Worker include Sidetiq::Schedulable ...
- 【leetcode-69】 x 的平方根
(主要是越界问题) 实现 int sqrt(int x) 函数. 计算并返回 x 的平方根,其中 x 是非负整数. 由于返回类型是整数,结果只保留整数的部分,小数部分将被舍去. 示例 1: 输入: 4 ...
- java项目中文件含义
1. java项目 .project:是工程构建配置文件 .classpath:保存的是项目所用的外部引用包的路径 .settings:记录项目配置变化的记录文件夹 src:sourcefolder项 ...
- spring中整合memcached,以及创建memcache的put和get方法
spring中整合memcached,以及创建memcache的put和get方法: 1:在项目中导入memcache相关的jar包 2:memcache在spring.xml的配置: 代码: < ...