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

该例程是为了说明对缺失值的随即填充训练出的estimator表现优于直接删掉有缺失字段值的estimator

例程代码及附加注释如下:

---------------------------------------------

  1. import numpy as np
  2.  
  3. from sklearn.datasets import load_boston
  4. from sklearn.ensemble import RandomForestRegressor
  5. from sklearn.pipeline import Pipeline
  6. from sklearn.preprocessing import Imputer
  7. from sklearn.model_selection import cross_val_score

  8. # 设定随机数种子
  9. rng = np.random.RandomState(0)
  10. # 载入数据 波士顿房价
  11. dataset = load_boston()
  12. X_full, y_full = dataset.data, dataset.target
  13. n_samples = X_full.shape[0]
  14. n_features = X_full.shape[1]
  15.  
  16. # Estimate the score on the entire dataset, with no missing values
    # 随机森林--回归 random_state-随机种子 n_estimator 森林里树的数目
  17. estimator = RandomForestRegressor(random_state=0, n_estimators=100)
    # 交叉验证分类器的准确率
  18. score = cross_val_score(estimator, X_full, y_full).mean()
  19. print("Score with the entire dataset = %.2f" % score)
  20.  
  21. # Add missing values in 75% of the lines
  22. missing_rate = 0.75
  23. n_missing_samples = int(np.floor(n_samples * missing_rate))
    # hstack 把两个数组拼接起来-行数需要一致
  24. missing_samples = np.hstack((np.zeros(n_samples - n_missing_samples,
  25. dtype=np.bool),
  26. np.ones(n_missing_samples,
  27. dtype=np.bool)))

  28. # 打乱随机数组顺序
    rng.shuffle(missing_samples)
  29. missing_features = rng.randint(0, n_features, n_missing_samples)
  30.  
  31. # Estimate the score without the lines containing missing values
  32. X_filtered = X_full[~missing_samples, :]
  33. y_filtered = y_full[~missing_samples]
  34. estimator = RandomForestRegressor(random_state=0, n_estimators=100)
  35. score = cross_val_score(estimator, X_filtered, y_filtered).mean()
  36. print("Score without the samples containing missing values = %.2f" % score)
  37.  
  38. # Estimate the score after imputation of the missing values
  39. X_missing = X_full.copy()
  40. X_missing[np.where(missing_samples)[0], missing_features] = 0
  41. y_missing = y_full.copy()
  42. estimator = Pipeline([("imputer", Imputer(missing_values=0,
  43. strategy="mean",
  44. axis=0)),
  45. ("forest", RandomForestRegressor(random_state=0,
  46. n_estimators=100))])
  47. score = cross_val_score(estimator, X_missing, y_missing).mean()
  48. print("Score after imputation of the missing values = %.2f" % score)
  49.  
  50. ---------------------------------------------------
    补充:
    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. [Ruby on Rails系列]4、专题:Rails应用的国际化[i18n]

    1. 什么是internationalization(i18n)? 国际化,英文简称i18n,按照维基百科的定义:国际化是指在设计软件,将软件与特定语言及地区脱钩的过程.当软件被移植到不同的语言及地区 ...

  4. [干货]2017已来,最全面试总结——这些Android面试题你一定需要

        地址.http://blog.csdn.net/xhmj12/article/details/54730883 相关阅读: 吊炸天!74款APP完整源码! [干货精品,值得收藏]超全的一线互联 ...

  5. Git之(一)Git是什么[转]

    为什么使用Git 孔子曾经曰过的,名正则言顺 言顺则事成. 我们在学习一项新技术之前,弄清楚为什么要学它至关重要,至于为什么要学习Git,我用一段if-else语句告诉你原因: if(你相信我){ 我 ...

  6. [caffe]linux下安装caffe(无cuda)以及python接口

    昨天在mac上折腾了一天都没有安装成功,晚上在mac上装了一个ParallelDesktop虚拟机,然后装了linux,十分钟就安装好了,我也是醉了=.= 主要过程稍微记录一下: 1.安装BLAS s ...

  7. [Swift]基础

    [Swift]基础 一, 常用变量 var str = "Hello, playground" //变量 let str1="Hello xmj112288" ...

  8. [译]一个灵活的 Trello 敏捷工作流

    [译]一个灵活的 Trello 敏捷工作流 翻译自 An Agile Trello Workflow That Keeps Tasks Flexible Getting things done 可不只 ...

  9. iOS10收集IDFA,植入第三方广告[终结]--ADMob

    [PS: 前段时间,公司做ASO推广,需要在应用中收集IDFA值,跟广告平台做交互!于是有了这个需求--] 1.首先,考虑了一下情况(自己懒 -_-#),就直接在首页上写了一个Banner,循环加载广 ...

随机推荐

  1. NoFragment重大bug

    在activity中切换fragment,有以下几点问题需要注意: 例如做一个类似于这样的tab切换fragment的,有以下几点问题 1.切换fragment后,前几个fragment能透视,解决方 ...

  2. vue的挖坑和爬坑之css背景图样式终极解决方法

    原问题 #wrapper{ width:100%; height:100%; position:fixed; background-image:url(./img/open_bg.jpg) } 在.v ...

  3. DBA 优化法则

    硬件资源是根本,DBA是为了充分利用硬件资源:(更新中--) 统一SQL语句: 减少SQL嵌套: 执行计划返回结果集(决定计划走向): 合理使用临时表: tempdb分多文件: OLTP 条件使用变量 ...

  4. RGB颜色 对照表

      来自为知笔记(Wiz)

  5. DM企业建站系统v201710 sql注入漏洞分析 | 新版v201712依旧存在sql注入

    0x00 前言 本来呢,这套CMS都不想审的了.下载下来打开一看,各种debug注释,排版烂的不行. 贴几个页面看看 感觉像是新手练手的,没有审下去的欲望了. 但想了想,我tm就是新手啊,然后就继续看 ...

  6. 【转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模拟器上的问题很多,本人也差点放弃 ...

  7. easyHOOK socket send recv

    代码比较简单,就不做注释了.  包含一个sockethookinject.DLL 和sockethook.exe 有一点不清楚, SetExclusiveACL可以添加当前线程的hook, 但是eas ...

  8. Linux 内核死锁

    死锁是指多个进程(线程)因为长久等待已被其他进程占有的的资源而陷入阻塞的一种状态.当等待的资源一直得不到释放,死锁会一直持续下去.死锁一旦发生,程序本身是解决不了的,只能依靠外部力量使得程序恢复运行, ...

  9. Python模块之信号学习(signal)

    信号概述 在学习Python前应该学习下Linux下的信号,软中断信号(signal,又简称为信号)用来通知进程发生了异步事件.进程之间可以互相通过系统调用kill发送软中断信号.内核也可以因为内部事 ...

  10. Wing ide 6.0 注册 ,python 3.6环境

    直接切入主题,套路如下: 1.选择手动输入license license number输入:CN123-12345-12345-12345 2.在下一步中,选择第二项,拷贝的request code ...