声明:版权所有,转载请联系作者并注明出处 http://blog.csdn.net/u013719780?viewmode=contents
博主简介:风雪夜归子(英文名: Allen),机器学习算法攻城狮,喜爱钻研Machine Learning的黑科技,对Deep Learning和Artificial Intelligence充满兴趣,经常关注kaggle数据挖掘竞赛平台,对数据、Machine Learning和Artificial Intelligence有兴趣的各位童鞋可以一起探讨哦,个人CSDN博客: http://blog.csdn.net/u013719780?viewmode=contents
从外部源获取样本数据
如果条件允许,学本系列内容时尽量用你熟悉的数据集;方便起见,我们用scikit-learn的内置数据库。这些内置数据库可用于测试不同的建模技术,如回归和分类。而且这些内置数据库都是非常著名的数据库。这对不同领域的学术论文的作者们来说是很用的,他们可以用这些内置数据库将他们的模型与其他模型进行比较。
推荐使用IPython来运行文中的指令。大内存很重要,这样可以让普通的命令正常运行。如果用IPython Notebook就更好了。如果你用Notebook,记得用
%matplotlib inline
指令,这样图象就会出现在Notebook里面,而不是一个新窗口里。
Getting ready
scikit-learn的内置数据库在datasets
模块里。用如下命令导入:
from sklearn import datasets
import numpy as np
在IPython里面运行datasets.*?
就会看到datasets
模块的指令列表。
How to do it…
datasets
模块主要有两种数据类型。较小的测试数据集在sklearn
包里面,可以通过datasets.load_*?
查看。较大的数据集可以根据需要下载。后者默认情况下不在sklearn
包里面;但是,有时这些大数据集可以更好的测试模型和算法,因为比较复杂足以模拟现实情形。
默认在sklearn
包里面的数据集可以通过datasets.load_*?
查看。另外一些数据集需要通过datasets.fetch_*?
下载,这些数据集更大,没有被自动安装。经常用于测试那些解决实际问题的算法。
首先,加载boston
数据集看看:
boston = datasets.load_boston()
print(boston.DESCR)
DESCR
将列出数据集的一些概况。下面我们来下载一个数据集:
housing = datasets.fetch_california_housing()
print(housing.DESCR)
How it works…
当这些数据集被加载时,它们并不是直接转换成Numpy数组。它们是Bunch
类型。Bunch是Python常用的数据结构。基本可以看成是一个词典,它的键被实例对象作为属性使用。
用data
属性连接数据中包含自变量的Numpy数组,用target
属性连接数据中的因变量。
X, y = boston.data, boston.target
网络上Bunch
对象有不同的实现;自己写一个也不难。scikit-learn用基本模块定义Bunch
。
There's more…
让你从外部源获取数据集时,它默认会被当前文件夹的scikit_learn_data/
放在里面,可以通过两种方式进行配置:
- 设置
SCIKIT_LEARN_DATA
环境变量指定下载位置 fetch_*?
方法的第一个参数是data_home
,可以知道下载位置
通过datasets.get_data_home()
很容易检查默认下载位置。
See also
UCI机器学习库(UCI Machine Learning Repository)是找简单数据集的好地方。很多scikit-learn的数据集都在那里,那里还有更多的数据集。其他数据源还是著名的KDD和Kaggle。