提交 92ab9f11 编写于 作者: L loopyme

更新5-[1,2]到0.21

上级 28a56b94
此差异已折叠。
......@@ -7,9 +7,9 @@
模块 [`sklearn.feature_extraction`](classes.html#module-sklearn.feature_extraction "sklearn.feature_extraction") 可用于提取符合机器学习算法支持的特征,比如文本和图片。
Note
特征特征提取与 特征选择 有很大的不同:前者包括将任意数据(如文本或图像)转换为可用于机器学习的数值特征。后者是将这些特征应用到机器学习中。
>**注意**
>
特征特征提取与[特征选择](docs/14.md?id=_113-特征选择)有很大的不同:前者包括将任意数据(如文本或图像)转换为可用于机器学习的数值特征。后者是将这些特征应用到机器学习中。
## 5.2.1\. 从字典类型加载特征
......@@ -131,12 +131,9 @@ X = hasher.transform(raw_X)
由于使用简单的模数将哈希函数转换为列索引,建议使用2次幂作为 `n_features` 参数; 否则特征不会均匀的分布到列中。
参考文献: Kilian Weinberger,Anirban Dasgupta,John Langford,Alex Smola和Josh Attenberg(2009)。用于大规模多任务学习的特征散列。PROC。ICML。 MurmurHash3。
参考文献:
* Kilian Weinberger, Anirban Dasgupta, John Langford, Alex Smola and Josh Attenberg (2009). [用于大规模多任务学习的特征散列](http://alex.smola.org/papers/2009/Weinbergeretal09.pdf). Proc. ICML.
* [MurmurHash3](https://github.com/aappleby/smhasher).
>参考文献:
>* Kilian Weinberger, Anirban Dasgupta, John Langford, Alex Smola and Josh Attenberg (2009). [用于大规模多任务学习的特征散列](http://alex.smola.org/papers/2009/Weinbergeretal09.pdf). Proc. ICML.
>* [MurmurHash3](https://github.com/aappleby/smhasher).
## 5.2.3\. 文本特征提取
......@@ -282,6 +279,17 @@ array([[0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 1, 0],
array([0, 0, 0, 1]...)
```
#### 5.2.3.3.1 使用停止词
停止词是像“and”、“the”、“him”这样的词,这些词在表示文本内容时被认为是没有信息的,可以删除它们,以避免它们被理解为预测的信号。然而,有时,类似的词对预测很有用,比如在对写作风格或性格进行分类时。
在我们提供的“英w文”停止词列表中有几个已知的问题。详情参见[NQY18]。
请慎重选择停止词列表。流行的停止词列表可能包括对某些任务(如计算机)具有高度信息性的词。
您还应该确保停止单词列表具有与矢量化器中使用的相同的预处理和标记。单词"we’ve"被`CountVectorizer`的默认记号分配器分割成"we"和"ve",所以如果"we’ve"在停止词列表中,但"ve"不在,"ve"会被保留在转换后的文本中。我们的矢量化器将尝试识别和警告某些类型的不一致性。
>参考文献
>* [NQY18] J. Nothman, H. Qin and R. Yurchak (2018). “[Stop Word Lists in Free Open-source Software Packages](https://aclweb.org/anthology/W18-2502)”. In Proc. Workshop for NLP Open Source Software.
### 5.2.3.4\. Tf–idf 项加权
......@@ -335,16 +343,15 @@ TfidfTransformer(norm=...'l2', smooth_idf=False, sublinear_tf=False,
>>> tfidf = transformer.fit_transform(counts)
>>> tfidf
<6x3 sparse matrix of type '<... 'numpy.float64'>'
with 9 stored elements in Compressed Sparse ... format>
with 9 stored elements in Compressed Sparse ... format>
>>> tfidf.toarray()
array([[ 0.81940995, 0\. , 0.57320793],
[ 1\. , 0\. , 0\. ],
[ 1\. , 0\. , 0\. ],
[ 1\. , 0\. , 0\. ],
[ 0.47330339, 0.88089948, 0\. ],
[ 0.58149261, 0\. , 0.81355169]])
array([[0.81940995, 0. , 0.57320793],
[1. , 0. , 0. ],
[1. , 0. , 0. ],
[1. , 0. , 0. ],
[0.47330339, 0.88089948, 0. ],
[0.58149261, 0. , 0.81355169]])
```
每行都被正则化,使其适应欧几里得标准:
......@@ -390,12 +397,12 @@ array([[ 0.81940995, 0\. , 0.57320793],
```py
>>> transformer = TfidfTransformer()
>>> transformer.fit_transform(counts).toarray()
array([[ 0.85151335, 0\. , 0.52433293],
[ 1\. , 0\. , 0\. ],
[ 1\. , 0\. , 0\. ],
[ 1\. , 0\. , 0\. ],
[ 0.55422893, 0.83236428, 0\. ],
[ 0.63035731, 0\. , 0.77630514]])
array([[0.85151335, 0. , 0.52433293],
[1. , 0. , 0. ],
[1. , 0. , 0. ],
[1. , 0. , 0. ],
[0.55422893, 0.83236428, 0. ],
[0.63035731, 0. , 0.77630514]])
```
......@@ -403,7 +410,7 @@ array([[ 0.85151335, 0\. , 0.52433293],
```py
>>> transformer.idf_
array([ 1\. ..., 2.25..., 1.84...])
array([1. ..., 2.25..., 1.84...])
```
......@@ -429,8 +436,8 @@ array([ 1\. ..., 2.25..., 1.84...])
文本由字符组成,但文件由字节组成。字节转化成字符依照一定的编码(encoding)方式。 为了在Python中的使用文本文档,这些字节必须被 _解码_ 为 Unicode 的字符集。 常用的编码方式有 ASCII,Latin-1(西欧),KOI8-R(俄语)和通用编码 UTF-8 和 UTF-16。还有许多其他的编码存在
Note
>**注意**
>
编码也可以称为 ‘字符集’, 但是这个术语不太准确: 单个字符集可能存在多个编码。
scikit-learn 中的文本提取器知道如何解码文本文件, 但只有当您告诉他们文件的编码的情况下才行, [`CountVectorizer`](https://scikit-learn.org/stable/modules/generated/sklearn.feature_extraction.text.CountVectorizer.html#sklearn.feature_extraction.text.CountVectorizer "sklearn.feature_extraction.text.CountVectorizer") 才需要一个 `encoding` 参数。 对于现代文本文件,正确的编码可能是 UTF-8,因此它也是默认解码方式 (`encoding="utf-8"`).
......@@ -451,11 +458,11 @@ scikit-learn 中的文本提取器知道如何解码文本文件, 但只有当
>>> import chardet
>>> text1 = b"Sei mir gegr\xc3\xbc\xc3\x9ft mein Sauerkraut"
>>> text2 = b"holdselig sind deine Ger\xfcche"
>>> text3 = b"\xff\xfeA\x00u\x00f\x00 \x00F\x00l\x00\xfc\x00g\x00e\x00l\x00n\x00 \x00d\x00e\x00s\x00 \x00G\x00e\x00s\x00a\x00n\x00g\x00e\x00s\x00,\x00 \x00H\x00e\x00r\x00z\x00l\x00i\x00e\x00b\x00c\x00h\x00e\x00n\x00,\x00 \x00t\x00r\x00a\x00g\x00 \x00i\x00c\x00h\x00 \x00d\x00i\x00c\x00h\x00 \x00f\x00o\x00r\x00t\x00"
>>> text3 = b"\xff\xfeA\x00u\x00f\x00 \x00F\x00l\x00\xfc\x00g\x00e\x00l\x00n\x00 \x00d\x00e\x00s\x00 \x00G\x00e\x00s\x00a\x00n\x00g\x00e\x00s\x00,\x00 \x00H\x00e\x00r\x00z\x00l\x00i\x00e\x00b\x00c\x00h\x00e\x00n\x00,\x00 \x00t\x00r\x00a\x00g\x00 \x00i\x00c\x00h\x00 \x00d\x00i\x00c\x00h\x00 \x00f\x00o\x00r\x00t\x00"
>>> decoded = [x.decode(chardet.detect(x)['encoding'])
... for x in (text1, text2, text3)]
>>> v = CountVectorizer().fit(decoded).vocabulary_
>>> for term in v: print(v)
>>> for term in v: print(v)
```
......@@ -601,57 +608,52 @@ True
特别是我们命名:
> * `预处理器`: 可以将整个文档作为输入(作为单个字符串)的可调用,并返回文档的可能转换的版本,仍然是整个字符串。这可以用于删除HTML标签,小写整个文档等。
> * `tokenizer`: 一个可从预处理器接收输出并将其分成标记的可调用函数,然后返回这些列表。
> * `分析器`: 一个可替代预处理程序和标记器的可调用程序。默认分析仪都会调用预处理器和刻录机,但是自定义分析仪将会跳过这个。 N-gram提取和停止字过滤在分析器级进行,因此定制分析器可能必须重现这些步骤。
* `预处理器`: 可以将整个文档作为输入(作为单个字符串)的可调用,并返回文档的可能转换的版本,仍然是整个字符串。这可以用于删除HTML标签,小写整个文档等。
* `tokenizer`: 一个可从预处理器接收输出并将其分成标记的可调用函数,然后返回这些列表。
* `分析器`: 一个可替代预处理程序和标记器的可调用程序。默认分析仪都会调用预处理器和刻录机,但是自定义分析仪将会跳过这个。 N-gram提取和停止字过滤在分析器级进行,因此定制分析器可能必须重现这些步骤。
(Lucene 用户可能会识别这些名称,但请注意,scikit-learn 概念可能无法一对一映射到 Lucene 概念上。)
为了使预处理器,标记器和分析器了解模型参数,可以从类派生并覆盖 `build_preprocessor`, `build_tokenizer`` 和 `build_analyzer` 工厂方法,而不是传递自定义函数。
为了使预处理器,标记器和分析器了解模型参数,可以从类派生并覆盖 `build_preprocessor`, `build_tokenizer``build_analyzer` 工厂方法,而不是传递自定义函数。
一些提示和技巧:
> * 如果文档由外部包进行预先标记,则将它们存储在文件(或字符串)中,令牌由空格分隔,并通过 `analyzer=str.split`
> * Fancy 令牌级分析,如词干,词法,复合分割,基于词性的过滤等不包括在 scikit-learn 代码库中,但可以通过定制分词器或分析器来添加。
>
> 这是一个 `CountVectorizer`, 使用 [NLTK](http://www.nltk.org) 的 tokenizer 和 lemmatizer:
>
> ```py
> &gt;&gt;&gt; from nltk import word_tokenize # doctest: +SKIP
> &gt;&gt;&gt; from nltk.stem import WordNetLemmatizer # doctest: +SKIP
> &gt;&gt;&gt; class LemmaTokenizer(object):
> ... def __init__(self):
> ... self.wnl = WordNetLemmatizer()
> ... def __call__(self, doc):
> ... return [self.wnl.lemmatize(t) for t in word_tokenize(doc)]
> ...
> &gt;&gt;&gt; vect = CountVectorizer(tokenizer=LemmaTokenizer()) # doctest: +SKIP
>
> (请注意,这不会过滤标点符号。)
> 例如,以下例子将英国的一些拼写变成美国拼写::
>
> &gt;&gt;&gt; import re
> &gt;&gt;&gt; def to_british(tokens):
> ... for t in tokens:
> ... t = re.sub(r"(...)our$", r"\1or", t)
> ... t = re.sub(r"([bt])re$", r"\1er", t)
> ... t = re.sub(r"([iy])s(e$|ing|ation)", r"\1z\2", t)
> ... t = re.sub(r"ogue$", "og", t)
> ... yield t
> ...
> &gt;&gt;&gt; class CustomVectorizer(CountVectorizer):
> ... def build_tokenizer(self):
> ... tokenize = super(CustomVectorizer, self).build_tokenizer()
> ... return lambda doc: list(to_british(tokenize(doc)))
> ...
> &gt;&gt;&gt; print(CustomVectorizer().build_analyzer()(u"color colour")) # doctest: +NORMALIZE_WHITESPACE +ELLIPSIS
> [...'color', ...'color']
>
> 用于其他样式的预处理; 例子包括 stemming, lemmatization, 或 normalizing numerical tokens, 后者说明如下:
>
> * :ref:`sphx_glr_auto_examples_bicluster_plot_bicluster_newsgroups.py`
>
> ```
* 如果文档由外部包进行预先标记,则将它们存储在文件(或字符串)中,令牌由空格分隔,并通过 `analyzer=str.split`
* Fancy 令牌级分析,如词干,词法,复合分割,基于词性的过滤等不包括在 scikit-learn 代码库中,但可以通过定制分词器或分析器来添加。这是一个 `CountVectorizer`, 使用 [NLTK](http://www.nltk.org) 的 tokenizer 和 lemmatizer:
```py
>>> from nltk import word_tokenize
>>> from nltk.stem import WordNetLemmatizer
>>> class LemmaTokenizer(object):
... def __init__(self):
... self.wnl = WordNetLemmatizer()
... def __call__(self, doc):
... return [self.wnl.lemmatize(t) for t in word_tokenize(doc)]
...
>>> vect = CountVectorizer(tokenizer=LemmaTokenizer())
```
(请注意,这不会过滤标点符号。)
例如,以下例子将英国的一些拼写变成美国拼写::
```py
>>> import re
>>> def to_british(tokens):
... for t in tokens:
... t = re.sub(r"(...)our$", r"\1or", t)
... t = re.sub(r"([bt])re$", r"\1er", t)
... t = re.sub(r"([iy])s(e$|ing|ation)", r"\1z\2", t)
... t = re.sub(r"ogue$", "og", t)
... yield t
...
>>> class CustomVectorizer(CountVectorizer):
... def build_tokenizer(self):
... tokenize = super().build_tokenizer()
... return lambda doc: list(to_british(tokenize(doc)))
...
>>> print(CustomVectorizer().build_analyzer()(u"color colour"))
[...'color', ...'color']
```
用于其他样式的预处理; 例子包括 stemming, lemmatization, 或 normalizing numerical tokens, 后者说明如下:
* [Biclustering documents with the Spectral Co-clustering algorithm
](https://scikit-learn.org/stable/auto_examples/bicluster/plot_bicluster_newsgroups.html#sphx-glr-auto-examples-bicluster-plot-bicluster-newsgroups-py)
在处理不使用显式字分隔符(例如空格)的亚洲语言时,自定义向量化器也是有用的。
......@@ -713,7 +715,7 @@ array([[15, 18],
scikit-learn 中的几个估计可以使用特征或样本之间的连接信息。 例如,Ward聚类(层次聚类 [层次聚类](clustering.html#hierarchical-clustering) )可以聚集在一起,只有图像的相邻像素,从而形成连续的斑块:
[![http://sklearn.apachecn.org/cn/0.19.0/_images/sphx_glr_plot_face_ward_segmentation_0011.png](img/6ade66280d509fe54883d82b315259e1.jpg)](https://scikit-learn.org/stable/auto_examples/cluster/plot_face_ward_segmentation.html)
[![http://sklearn.apachecn.org/cn/0.19.0/_images/sphx_glr_plot_face_ward_segmentation_0011.png](img/sphx_glr_plot_coin_ward_segmentation_0011.png)](https://scikit-learn.org/stable/auto_examples/cluster/plot_coin_ward_segmentation.html)
为此,估计器使用 ‘连接性’ 矩阵,给出连接的样本。
......@@ -721,10 +723,8 @@ scikit-learn 中的几个估计可以使用特征或样本之间的连接信息
这些矩阵可用于在使用连接信息的估计器中强加连接,如 Ward 聚类(层次聚类 [层次聚类](clustering.html#hierarchical-clustering) ),而且还要构建预计算的内核或相似矩阵。
Note
**示例**
* [A demo of structured Ward hierarchical clustering on a raccoon face image](https://scikit-learn.org/stable/auto_examples/cluster/plot_face_ward_segmentation.html#sphx-glr-auto-examples-cluster-plot-face-ward-segmentation-py)
* [Spectral clustering for image segmentation](https://scikit-learn.org/stable/auto_examples/cluster/plot_segmentation_toy.html#sphx-glr-auto-examples-cluster-plot-segmentation-toy-py)
* [Feature agglomeration vs. univariate selection](https://scikit-learn.org/stable/auto_examples/cluster/plot_feature_agglomeration_vs_univariate_selection.html#sphx-glr-auto-examples-cluster-plot-feature-agglomeration-vs-univariate-selection-py)
>**示例**
>
>* [A demo of structured Ward hierarchical clustering on a raccoon face image](https://scikit-learn.org/stable/auto_examples/cluster/plot_face_ward_segmentation.html#sphx-glr-auto-examples-cluster-plot-face-ward-segmentation-py)
>* [Spectral clustering for image segmentation](https://scikit-learn.org/stable/auto_examples/cluster/plot_segmentation_toy.html#sphx-glr-auto-examples-cluster-plot-segmentation-toy-py)
>* [Feature agglomeration vs. univariate selection](https://scikit-learn.org/stable/auto_examples/cluster/plot_feature_agglomeration_vs_univariate_selection.html#sphx-glr-auto-examples-cluster-plot-feature-agglomeration-vs-univariate-selection-py)
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册