[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,循环加载广 ...
随机推荐
- NoFragment重大bug
在activity中切换fragment,有以下几点问题需要注意: 例如做一个类似于这样的tab切换fragment的,有以下几点问题 1.切换fragment后,前几个fragment能透视,解决方 ...
- vue的挖坑和爬坑之css背景图样式终极解决方法
原问题 #wrapper{ width:100%; height:100%; position:fixed; background-image:url(./img/open_bg.jpg) } 在.v ...
- DBA 优化法则
硬件资源是根本,DBA是为了充分利用硬件资源:(更新中--) 统一SQL语句: 减少SQL嵌套: 执行计划返回结果集(决定计划走向): 合理使用临时表: tempdb分多文件: OLTP 条件使用变量 ...
- RGB颜色 对照表
来自为知笔记(Wiz)
- DM企业建站系统v201710 sql注入漏洞分析 | 新版v201712依旧存在sql注入
0x00 前言 本来呢,这套CMS都不想审的了.下载下来打开一看,各种debug注释,排版烂的不行. 贴几个页面看看 感觉像是新手练手的,没有审下去的欲望了. 但想了想,我tm就是新手啊,然后就继续看 ...
- 【转1】Appium 1.6.3 在Xcode 8, iOS 10.2(模拟器)测试环境搭建 经验总结
Appium 1.6.3 在Xcode 8, iOS 10.2(模拟器)测试环境搭建 经验总结 关于 Appium 1.6.3 在Xcode 8, 10.2 的iOS模拟器上的问题很多,本人也差点放弃 ...
- easyHOOK socket send recv
代码比较简单,就不做注释了. 包含一个sockethookinject.DLL 和sockethook.exe 有一点不清楚, SetExclusiveACL可以添加当前线程的hook, 但是eas ...
- Linux 内核死锁
死锁是指多个进程(线程)因为长久等待已被其他进程占有的的资源而陷入阻塞的一种状态.当等待的资源一直得不到释放,死锁会一直持续下去.死锁一旦发生,程序本身是解决不了的,只能依靠外部力量使得程序恢复运行, ...
- Python模块之信号学习(signal)
信号概述 在学习Python前应该学习下Linux下的信号,软中断信号(signal,又简称为信号)用来通知进程发生了异步事件.进程之间可以互相通过系统调用kill发送软中断信号.内核也可以因为内部事 ...
- Wing ide 6.0 注册 ,python 3.6环境
直接切入主题,套路如下: 1.选择手动输入license license number输入:CN123-12345-12345-12345 2.在下一步中,选择第二项,拷贝的request code ...