[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 如果这些列具有有用信息(在未丢失的位置),则在删除列时,模型将失去对此信息的访问权限. 此外, ...
- [Ruby on Rails系列]4、专题:Rails应用的国际化[i18n]
1. 什么是internationalization(i18n)? 国际化,英文简称i18n,按照维基百科的定义:国际化是指在设计软件,将软件与特定语言及地区脱钩的过程.当软件被移植到不同的语言及地区 ...
- [干货]2017已来,最全面试总结——这些Android面试题你一定需要
地址.http://blog.csdn.net/xhmj12/article/details/54730883 相关阅读: 吊炸天!74款APP完整源码! [干货精品,值得收藏]超全的一线互联 ...
- Git之(一)Git是什么[转]
为什么使用Git 孔子曾经曰过的,名正则言顺 言顺则事成. 我们在学习一项新技术之前,弄清楚为什么要学它至关重要,至于为什么要学习Git,我用一段if-else语句告诉你原因: if(你相信我){ 我 ...
- [caffe]linux下安装caffe(无cuda)以及python接口
昨天在mac上折腾了一天都没有安装成功,晚上在mac上装了一个ParallelDesktop虚拟机,然后装了linux,十分钟就安装好了,我也是醉了=.= 主要过程稍微记录一下: 1.安装BLAS s ...
- [Swift]基础
[Swift]基础 一, 常用变量 var str = "Hello, playground" //变量 let str1="Hello xmj112288" ...
- [译]一个灵活的 Trello 敏捷工作流
[译]一个灵活的 Trello 敏捷工作流 翻译自 An Agile Trello Workflow That Keeps Tasks Flexible Getting things done 可不只 ...
- iOS10收集IDFA,植入第三方广告[终结]--ADMob
[PS: 前段时间,公司做ASO推广,需要在应用中收集IDFA值,跟广告平台做交互!于是有了这个需求--] 1.首先,考虑了一下情况(自己懒 -_-#),就直接在首页上写了一个Banner,循环加载广 ...
随机推荐
- css中使用if条件在各大浏览器(IE6\IE7\IE8)中hack方法解决教程
一个滚动代码,其他浏览器都滚的好好的,就IE出现错误,DIV+CSS if条件hack,这里DIVCSS5为大家介绍针对各大浏览器(IE6\IE7\IE8)中使用if条件hack方法教程,DIV CS ...
- web打印总结
一.打印样式 区别显示和打印的样式 使用media="print"控制打印时的样式,如下: 打印时不显示打印按钮,设置页面宽度 <style media="prin ...
- 《深入理解Java虚拟机:JVM高级属性与最佳实践》读书笔记(更新中)
第一章:走进Java 概述 Java技术体系 Java发展史 Java虚拟机发展史 1996年 JDK1.0,出现Sun Classic VM HotSpot VM, 它是 Sun JDK 和 Ope ...
- 【练习】jQuery
作业要求: 参考下图,点击展示不同内容. 例: <!DOCTYPE html> <html lang="en"> <head> <meta ...
- iOS SQLite解密之SQLCipher
开门见山,本文主要讲在Mac下使用SQLCipher编译解密数据库文件方法,iOS项目集成SQLCipher自己可以百度,网上帖子很多. 官方集成文档:https://www.zetetic.net/ ...
- 关于linux下的date日期,并以日期给文件命名
在linux的终端中,我们输入date后会有以下显示: 然后博主也扩展了一下date的基础用法: date + "%-": %y 输出年份的后2位:%Y 输出完整年份 %m 输出月 ...
- 我的Python学习笔记(三):私有变量
一.私有变量的定义 在Python中,有以下几种方式来定义变量: xx:公有变量 _xx:单前置下划线,私有化属性或方法,类对象和子类可以访问,from somemodule import *禁止导入 ...
- Virtualbox虚拟机安装与设置
Virtualbox与VMware类似,都是虚拟机软件,在win10下安装Virtualbox直接默认安装即可.版本:VirtualBox-5.2.0-118431-Win.exe 安装完成后,点击左 ...
- fgets的用法
fgets的用法: char *fgets(char *s, int size, FILE *stream); fgets() reads in at most one les ...
- Windows下使用pip安装mysql-python
安装的过程很煎熬,留个爪,希望对其他人有帮助. 先声明我安装前的电脑配置: Win10: Python2和Python3共存(备注一个好用的方法,感谢知乎大神:https://www.zhihu.co ...