scikit-learn:5. 加载内置公用的数据

之前写过一篇如何加载自己的数据,参考:

http://blog.csdn.net/mmc2015/article/details/46852755


本篇主要写如何加载scikit-learn内置数据,参考:

http://scikit-learn.org/stable/datasets/index.html#general-dataset-api

重点介绍“5.7:The 20 newsgroups text dataset”,以练习文本分类问题。

重点介绍“5.9:The Labeled Faces in the Wild face recognition dataset”,以练习图像处理问题。


5.7:The 20 newsgroups text dataset

18000个新闻、可分为20个topics;已经分为training和testing集。


两个loders:

第一个, sklearn.datasets.fetch_20newsgroups,返回原始文档,可进一步使用sklearn.feature_extraction.text.CountVectorizer进行个性化处理;

第二个,sklearn.datasets.fetch_20newsgroups_vectorized,,返回已经处理好的特征(ready-to-use feature),不须再作进一步feature extraction。


加载全部数据和加载部分数据:

from sklearn.datasets import fetch_20newsgroups
newsgroups_train = fetch_20newsgroups(subset='train')
cats = ['alt.atheism', 'sci.space']
newsgroups_train = fetch_20newsgroups(subset='train', categories=cats)

数据存在filenames和target两个属性中,target是数字0到19,使用target_names[target]可以获取具体类别名称。

注意,实际使用中还是先把数据下载下来,再使用这里 http://blog.csdn.net/mmc2015/article/details/46852963提到的办法加载。

加载了数据,就知道该怎么提取tf、tf-idf了吧:http://blog.csdn.net/mmc2015/article/details/46857887。


过滤标题、注脚、引用:

a parameter called remove, telling it what kinds of information to strip out of each file. remove should be a tuple containing any subset of ('headers', 'footers','quotes'), telling it to remove headers, signature blocks, and quotation blocks respectively.

>>> newsgroups_test = fetch_20newsgroups(subset='test',
...                                      remove=('headers', 'footers', 'quotes'),
...                                      categories=categories)



5.9:The Labeled Faces in the Wild face recognition dataset

JPEG人脸图片,典型任务1(Face Verification):给出两张图,判断出是否是同一个人;典型任务2(Face Recognition):给出一张图,判断出是哪个人。


两个loders:

第一个, Face Recognition,多元分类任务:

>>> from sklearn.datasets import <strong>fetch_lfw_people</strong>
>>> lfw_people = fetch_lfw_people(min_faces_per_person=70, resize=0.4)

>>> for name in lfw_people.target_names:
...     print(name)
...
Ariel Sharon
Colin Powell

>>> lfw_people.data.dtype
dtype('float32')

>>> lfw_people.data.shape
(1288, 1850)

>>> lfw_people.images.shape
(1288, 50, 37)

>>> lfw_people.target.shape
(1288,)

>>> list(lfw_people.target[:10])
[5, 6, 3, 1, 0, 1, 3, 4, 3, 0]


第二个,Face Verification,每一个sample是两张图片(可能属于同一个人、也可能不是)

>>> from sklearn.datasets import <strong>fetch_lfw_pairs</strong>
>>> lfw_pairs_train = fetch_lfw_pairs(subset='train')

>>> list(lfw_pairs_train.target_names)
['Different persons', 'Same person']

>>> lfw_pairs_train.pairs.shape
(2200, 2, 62, 47)

>>> lfw_pairs_train.data.shape
(2200, 5828)

>>> lfw_pairs_train.target.shape
(2200,)






其他数据集如下简单描述:----------------------------------------------


5.2、小数据集:

仅用来展示、尝试不同算法,实际没什么意义。

load_boston() Load and return the boston house-prices dataset (regression).
load_iris() Load and return the iris dataset (classification).
load_diabetes() Load and return the diabetes dataset (regression).
load_digits([n_class]) Load and return the digits dataset (classification).
load_linnerud() Load and return the linnerud dataset (multivariate regression).

5.3、sample image数据集:

load_sample_images() Load sample images for image manipulation.
load_sample_image(image_name) Load the numpy array of a single sample image

5.4、sample generator:

人工产生实验数据,分为单标签数据、多标签数据、二元聚类数据、回归数据、流行数据、可解压缩(分解)数据。

实际没什么意义。


5.5、svmlight/libsvm专用数据类型

该数据类型要求:每行的格式是“ <label> <feature-id>:<feature-value> <feature-id>:<feature-value> .... ”,适合用稀疏矩阵存储。


5.6、Olivetti faces数据集:

40个不同的人,每个人的脸有10张照片,这是张照片会稍微不同(长眼闭眼、笑与不笑、带不带眼镜)。256位灰度图像,每位由8-bit存储。

类别标签是0到39。

数据量小,适合练习无监督学习和半监督学习


5.8、从mldata.org库下载数据集:

使用函数:sklearn.datasets.fetch_mldata.,


5.10、forest covertypes数据集:

一片森林分成了30*30米的小块,每一小块由54个特征描述,预测每一小块主要由7中树木中的哪一种树木组成。最主要的是54个特征有boolean、有离散、有连续,所以很有挑战性,且能练习多元分类问题。

sklearn.datasets.fetch_covtype 加载数据,data和target分别存储特征和标签。


你可能感兴趣的:(数据挖掘,机器学习,加载数据,scikit-learn)