提交 2968e61d 编写于 作者: L loopyme

根据0.21.x调整目录结构,修改出口链接

上级 29695cfb
......@@ -25,31 +25,30 @@
+ [2.4\. 双聚类](docs/23.md)
+ [2.5\. 分解成分中的信号(矩阵分解问题)](docs/24.md)
+ [2.6\. 协方差估计](docs/25.md)
+ [2.7\. 经验协方差](docs/26.md)
+ [2.8\. 收敛协方差](docs/27.md)
+ [2.9\. 稀疏逆协方差](docs/28.md)
+ [2.10\. Robust 协方差估计](docs/29.md)
+ [2.11\. 新奇和异常值检测](docs/30.md)
+ [2.12\. 密度估计](docs/31.md)
+ [2.13\. 神经网络模型(无监督)](docs/32.md)
+ [2.7\. 新奇和异常值检测](docs/26.md)
+ [2.8\. 密度估计](docs/27.md)
+ [2.9\. 神经网络模型(无监督)](docs/28.md)
+ [3\. 模型选择和评估](docs/33.md)
+ [3.1\. 交叉验证:评估估算器的表现](docs/34.md)
+ [3.2\. 调整估计器的超参数](docs/35.md)
+ [3.3\. 模型评估: 量化预测的质量](docs/53.md)
+ [3.4\. 模型持久化](docs/54.md)
+ [3.5\. 验证曲线: 绘制分数以评估模型](docs/55.md)
+ [4\. 数据集转换](docs/56.md)
+ [4.1\. Pipeline(管道)和 FeatureUnion(特征联合): 合并的评估器](docs/57.md)
+ [4.2\. 特征提取](docs/58.md)
+ [4.3\. 预处理数据](docs/59.md)
+ [4.4\. 无监督降维](docs/60.md)
+ [4.5\. 随机投影](docs/61.md)
+ [4.6\. 内核近似](docs/62.md)
+ [4.7\. 成对的矩阵, 类别和核函数](docs/63.md)
+ [4.8\. 预测目标 (`y`.md) 的转换](docs/64.md)
+ [5\. 数据集加载工具](docs/65.md)
+ [6\. 大规模计算的策略: 更大量的数据](docs/66.md)
+ [7\. 计算性能](docs/67.md)
+ [5\. 数据集转换](docs/56.md)
+ [5.1\. Pipeline(管道)和 FeatureUnion(特征联合): 合并的评估器](docs/57.md)
+ [5.2\. 特征提取](docs/58.md)
+ [5.3\. 预处理数据](docs/59.md)
+ [5.4\. 缺失值填充]()
+ [5.5\. 无监督降维](docs/60.md)
+ [5.6\. 随机投影](docs/61.md)
+ [5.7\. 内核近似](docs/62.md)
+ [5.8\. 成对的矩阵, 类别和核函数](docs/63.md)
+ [5.9\. 预测目标 (`y`.md) 的转换](docs/64.md)
+ [6\. 数据集加载工具](docs/65.md)
+ [7\. 使用scikit-learn计算](docs/66.md)
+ [7.1\. 大规模计算的策略: 更大量的数据](docs/66?id=_71-大规模计算的策略-更大量的数据)
+ [7.2\. 计算性能](docs/66?id=_72-计算性能)
+ [7.3\. 并行,资源管理和配置]()
+ 教程
+ [使用 scikit-learn 介绍机器学习](docs/68.md)
+ [关于科学数据处理的统计学习教程](docs/69.md)
......@@ -64,4 +63,4 @@
+ [外部资源,视频和谈话](docs/78.md)
+ [API 参考](https://scikit-learn.org/stable/modules/classes.html)
+ [常见问题](docs/80.md)
+ [时光轴](docs/81.md)
\ No newline at end of file
+ [时光轴](docs/81.md)
......@@ -16,7 +16,7 @@
![P(x_i | y, x_1, \dots, x_{i-1}, x_{i+1}, \dots, x_n) = P(x_i | y) ,](img/9fd83615429a9be9e5698d35bec8642a.jpg)
对于所有的 :math: <cite>i</cite> ,这个关系式可以简化为
对于所有的 :math: *i* ,这个关系式可以简化为
![P(y \mid x_1, \dots, x_n) = \frac{P(y) \prod_{i=1}^{n} P(x_i \mid y)}
......@@ -47,7 +47,7 @@
## 1.9.1\. 高斯朴素贝叶斯
[`GaussianNB`](generated/sklearn.naive_bayes.GaussianNB.html#sklearn.naive_bayes.GaussianNB "sklearn.naive_bayes.GaussianNB") 实现了运用于分类的高斯朴素贝叶斯算法。特征的可能性(即概率)假设为高斯分布:
[`GaussianNB`](https://scikit-learn.org/stable/modules/generated/sklearn.naive_bayes.GaussianNB.html#sklearn.naive_bayes.GaussianNB "sklearn.naive_bayes.GaussianNB") 实现了运用于分类的高斯朴素贝叶斯算法。特征的可能性(即概率)假设为高斯分布:
![P(x_i \mid y) &= \frac{1}{\sqrt{2\pi\sigma^2_y}} \exp\left(-\frac{(x_i - \mu_y)^2}{2\sigma^2_y}\right)](img/92c470d884176abfcc29a5218ccf0aef.jpg)
......@@ -67,7 +67,7 @@ Number of mislabeled points out of a total 150 points : 6
## 1.9.2\. 多项分布朴素贝叶斯
[`MultinomialNB`](generated/sklearn.naive_bayes.MultinomialNB.html#sklearn.naive_bayes.MultinomialNB "sklearn.naive_bayes.MultinomialNB") 实现了服从多项分布数据的朴素贝叶斯算法,也是用于文本分类(这个领域中数据往往以词向量表示,尽管在实践中 tf-idf 向量在预测时表现良好)的两大经典朴素贝叶斯算法之一。 分布参数由每类 ![y](img/0775c03fc710a24df297dedcec515aaf.jpg) 的 ![\theta_y = (\theta_{y1},\ldots,\theta_{yn})](img/a25ab5d7faac5be22a6d242286376161.jpg) 向量决定, 式中 ![n](img/c87d9110f3d32ffa5fa08671e4af11fb.jpg) 是特征的数量(对于文本分类,是词汇量的大小) ![\theta_{yi}](img/860af894555b3d5f52e893bab6cafc11.jpg) 是样本中属于类 ![y](img/0775c03fc710a24df297dedcec515aaf.jpg) 中特征 ![i](img/43e13b580daefe5ba754b790dfbd216c.jpg) 概率 ![P(x_i \mid y)](img/db23fadfab6b660dbfa2934c4536beb1.jpg)
[`MultinomialNB`](https://scikit-learn.org/stable/modules/generated/sklearn.naive_bayes.MultinomialNB.html#sklearn.naive_bayes.MultinomialNB "sklearn.naive_bayes.MultinomialNB") 实现了服从多项分布数据的朴素贝叶斯算法,也是用于文本分类(这个领域中数据往往以词向量表示,尽管在实践中 tf-idf 向量在预测时表现良好)的两大经典朴素贝叶斯算法之一。 分布参数由每类 ![y](img/0775c03fc710a24df297dedcec515aaf.jpg) 的 ![\theta_y = (\theta_{y1},\ldots,\theta_{yn})](img/a25ab5d7faac5be22a6d242286376161.jpg) 向量决定, 式中 ![n](img/c87d9110f3d32ffa5fa08671e4af11fb.jpg) 是特征的数量(对于文本分类,是词汇量的大小) ![\theta_{yi}](img/860af894555b3d5f52e893bab6cafc11.jpg) 是样本中属于类 ![y](img/0775c03fc710a24df297dedcec515aaf.jpg) 中特征 ![i](img/43e13b580daefe5ba754b790dfbd216c.jpg) 概率 ![P(x_i \mid y)](img/db23fadfab6b660dbfa2934c4536beb1.jpg)
参数 ![\theta_y](img/bbdbd3f496f907578ff01cc798adde74.jpg) 使用平滑过的最大似然估计法来估计,即相对频率计数:
......@@ -83,7 +83,7 @@ Number of mislabeled points out of a total 150 points : 6
## 1.9.3\. 伯努利朴素贝叶斯
[`BernoulliNB`](generated/sklearn.naive_bayes.BernoulliNB.html#sklearn.naive_bayes.BernoulliNB "sklearn.naive_bayes.BernoulliNB") 实现了用于多重伯努利分布数据的朴素贝叶斯训练和分类算法,即有多个特征,但每个特征 都假设是一个二元 (Bernoulli, boolean) 变量。 因此,这类算法要求样本以二元值特征向量表示;如果样本含有其他类型的数据, 一个 `BernoulliNB` 实例会将其二值化(取决于 `binarize` 参数)。
[`BernoulliNB`](https://scikit-learn.org/stable/modules/generated/sklearn.naive_bayes.BernoulliNB.html#sklearn.naive_bayes.BernoulliNB "sklearn.naive_bayes.BernoulliNB") 实现了用于多重伯努利分布数据的朴素贝叶斯训练和分类算法,即有多个特征,但每个特征 都假设是一个二元 (Bernoulli, boolean) 变量。 因此,这类算法要求样本以二元值特征向量表示;如果样本含有其他类型的数据, 一个 `BernoulliNB` 实例会将其二值化(取决于 `binarize` 参数)。
伯努利朴素贝叶斯的决策规则基于
......@@ -101,7 +101,7 @@ Number of mislabeled points out of a total 150 points : 6
## 1.9.4\. 堆外朴素贝叶斯模型拟合
朴素贝叶斯模型可以解决整个训练集不能导入内存的大规模分类问题。 为了解决这个问题, [`MultinomialNB`](generated/sklearn.naive_bayes.MultinomialNB.html#sklearn.naive_bayes.MultinomialNB "sklearn.naive_bayes.MultinomialNB"), [`BernoulliNB`](generated/sklearn.naive_bayes.BernoulliNB.html#sklearn.naive_bayes.BernoulliNB "sklearn.naive_bayes.BernoulliNB"), 和 [`GaussianNB`](generated/sklearn.naive_bayes.GaussianNB.html#sklearn.naive_bayes.GaussianNB "sklearn.naive_bayes.GaussianNB") 实现了 `partial_fit` 方法,可以动态的增加数据,使用方法与其他分类器的一样,使用示例见 [Out-of-core classification of text documents](../auto_examples/applications/plot_out_of_core_classification.html#sphx-glr-auto-examples-applications-plot-out-of-core-classification-py) 。所有的朴素贝叶斯分类器都支持样本权重。
朴素贝叶斯模型可以解决整个训练集不能导入内存的大规模分类问题。 为了解决这个问题, [`MultinomialNB`](https://scikit-learn.org/stable/modules/generated/sklearn.naive_bayes.MultinomialNB.html#sklearn.naive_bayes.MultinomialNB "sklearn.naive_bayes.MultinomialNB"), [`BernoulliNB`](https://scikit-learn.org/stable/modules/generated/sklearn.naive_bayes.BernoulliNB.html#sklearn.naive_bayes.BernoulliNB "sklearn.naive_bayes.BernoulliNB"), 和 [`GaussianNB`](https://scikit-learn.org/stable/modules/generated/sklearn.naive_bayes.GaussianNB.html#sklearn.naive_bayes.GaussianNB "sklearn.naive_bayes.GaussianNB") 实现了 `partial_fit` 方法,可以动态的增加数据,使用方法与其他分类器的一样,使用示例见 [Out-of-core classification of text documents](https://scikit-learn.org/stable/auto_examples/applications/plot_out_of_core_classification.html#sphx-glr-auto-examples-applications-plot-out-of-core-classification-py) 。所有的朴素贝叶斯分类器都支持样本权重。
`fit` 方法不同,首次调用 `partial_fit` 方法需要传递一个所有期望的类标签的列表。
......
......@@ -10,7 +10,7 @@
例如,在下面的图片中,决策树通过if-then-else的决策规则来学习数据从而估测数一个正弦图像。决策树越深入,决策规则就越复杂并且对数据的拟合越好。
[![http://sklearn.apachecn.org/cn/0.19.0/_images/sphx_glr_plot_tree_regression_0011.png](img/f0b72920659961ba27aec1da59f3019c.jpg)](../auto_examples/tree/plot_tree_regression.html)
[![http://sklearn.apachecn.org/cn/0.19.0/_images/sphx_glr_plot_tree_regression_0011.png](img/f0b72920659961ba27aec1da59f3019c.jpg)](https://scikit-learn.org/stable/auto_examples/tree/plot_tree_regression.html)
决策树的优势:
......@@ -35,7 +35,7 @@
## 1.10.1\. 分类
[`DecisionTreeClassifier`](generated/sklearn.tree.DecisionTreeClassifier.html#sklearn.tree.DecisionTreeClassifier "sklearn.tree.DecisionTreeClassifier") 是能够在数据集上执行多分类的类,与其他分类器一样,[`DecisionTreeClassifier`](generated/sklearn.tree.DecisionTreeClassifier.html#sklearn.tree.DecisionTreeClassifier "sklearn.tree.DecisionTreeClassifier") 采用输入两个数组:数组X,用 `[n_samples, n_features]` 的方式来存放训练样本。整数值数组Y,用 `[n_samples]` 来保存训练样本的类标签:
[`DecisionTreeClassifier`](https://scikit-learn.org/stable/modules/generated/sklearn.tree.DecisionTreeClassifier.html#sklearn.tree.DecisionTreeClassifier "sklearn.tree.DecisionTreeClassifier") 是能够在数据集上执行多分类的类,与其他分类器一样,[`DecisionTreeClassifier`](https://scikit-learn.org/stable/modules/generated/sklearn.tree.DecisionTreeClassifier.html#sklearn.tree.DecisionTreeClassifier "sklearn.tree.DecisionTreeClassifier") 采用输入两个数组:数组X,用 `[n_samples, n_features]` 的方式来存放训练样本。整数值数组Y,用 `[n_samples]` 来保存训练样本的类标签:
```py
>>> from sklearn import tree
......@@ -62,7 +62,7 @@ array([[ 0., 1.]])
```
[`DecisionTreeClassifier`](generated/sklearn.tree.DecisionTreeClassifier.html#sklearn.tree.DecisionTreeClassifier "sklearn.tree.DecisionTreeClassifier") 既能用于二分类(其中标签为[-1,1])也能用于多分类(其中标签为[0,…,k-1])。使用Lris数据集,我们可以构造一个决策树,如下所示:
[`DecisionTreeClassifier`](https://scikit-learn.org/stable/modules/generated/sklearn.tree.DecisionTreeClassifier.html#sklearn.tree.DecisionTreeClassifier "sklearn.tree.DecisionTreeClassifier") 既能用于二分类(其中标签为[-1,1])也能用于多分类(其中标签为[0,…,k-1])。使用Lris数据集,我们可以构造一个决策树,如下所示:
```py
>>> from sklearn.datasets import load_iris
......@@ -73,11 +73,11 @@ array([[ 0., 1.]])
```
经过训练,我们可以使用 [`export_graphviz`](generated/sklearn.tree.export_graphviz.html#sklearn.tree.export_graphviz "sklearn.tree.export_graphviz") 导出器以 [Graphviz](http://www.graphviz.org/) 格式导出决策树. 如果你是用 [conda](http://conda.io) 来管理包,那么安装 graphviz 二进制文件和 python 包可以用以下指令安装
经过训练,我们可以使用 [`export_graphviz`](https://scikit-learn.org/stable/modules/generated/sklearn.tree.export_graphviz.html#sklearn.tree.export_graphviz "sklearn.tree.export_graphviz") 导出器以 [Graphviz](http://www.graphviz.org/) 格式导出决策树. 如果你是用 [conda](http://conda.io) 来管理包,那么安装 graphviz 二进制文件和 python 包可以用以下指令安装
> conda install python-graphviz
或者,可以从 graphviz 项目主页下载 graphviz 的二进制文件,并从 pypi 安装 Python 包装器,并安装 <cite>pip install graphviz</cite> .以下是在整个 iris 数据集上训练的上述树的 graphviz 导出示例; 其结果被保存在 <cite>iris.pdf</cite> 中:
或者,可以从 graphviz 项目主页下载 graphviz 的二进制文件,并从 pypi 安装 Python 包装器,并安装 *pip install graphviz* .以下是在整个 iris 数据集上训练的上述树的 graphviz 导出示例; 其结果被保存在 *iris.pdf* 中:
```py
>>> import graphviz # doctest: +SKIP
......@@ -115,17 +115,17 @@ array([[ 1., 0., 0.]])
```
[![http://sklearn.apachecn.org/cn/0.19.0/_images/sphx_glr_plot_iris_0013.png](img/cba233fc4178da6d3fe0b177cbbb6318.jpg)](../auto_examples/tree/plot_iris.html)
[![http://sklearn.apachecn.org/cn/0.19.0/_images/sphx_glr_plot_iris_0013.png](img/cba233fc4178da6d3fe0b177cbbb6318.jpg)](https://scikit-learn.org/stable/auto_examples/tree/plot_iris.html)
示例:
* [Plot the decision surface of a decision tree on the iris dataset](../auto_examples/tree/plot_iris.html#sphx-glr-auto-examples-tree-plot-iris-py)
* [Plot the decision surface of a decision tree on the iris dataset](https://scikit-learn.org/stable/auto_examples/tree/plot_iris.html#sphx-glr-auto-examples-tree-plot-iris-py)
## 1.10.2\. 回归
[![http://sklearn.apachecn.org/cn/0.19.0/_images/sphx_glr_plot_tree_regression_0011.png](img/f0b72920659961ba27aec1da59f3019c.jpg)](../auto_examples/tree/plot_tree_regression.html)
[![http://sklearn.apachecn.org/cn/0.19.0/_images/sphx_glr_plot_tree_regression_0011.png](img/f0b72920659961ba27aec1da59f3019c.jpg)](https://scikit-learn.org/stable/auto_examples/tree/plot_tree_regression.html)
决策树通过使用 [`DecisionTreeRegressor`](generated/sklearn.tree.DecisionTreeRegressor.html#sklearn.tree.DecisionTreeRegressor "sklearn.tree.DecisionTreeRegressor") 类也可以用来解决回归问题。如在分类设置中,拟合方法将数组X和数组y作为参数,只有在这种情况下,y数组预期才是浮点值:
决策树通过使用 [`DecisionTreeRegressor`](https://scikit-learn.org/stable/modules/generated/sklearn.tree.DecisionTreeRegressor.html#sklearn.tree.DecisionTreeRegressor "sklearn.tree.DecisionTreeRegressor") 类也可以用来解决回归问题。如在分类设置中,拟合方法将数组X和数组y作为参数,只有在这种情况下,y数组预期才是浮点值:
```py
>>> from sklearn import tree
......@@ -140,7 +140,7 @@ array([ 0.5])
示例:
* [Decision Tree Regression](../auto_examples/tree/plot_tree_regression.html#sphx-glr-auto-examples-tree-plot-tree-regression-py)
* [Decision Tree Regression](https://scikit-learn.org/stable/auto_examples/tree/plot_tree_regression.html#sphx-glr-auto-examples-tree-plot-tree-regression-py)
## 1.10.3\. 多值输出问题
......@@ -160,18 +160,18 @@ array([ 0.5])
```
用多输出决策树进行回归分析 [Multi-output Decision Tree Regression](../auto_examples/tree/plot_tree_regression_multioutput.html#sphx-glr-auto-examples-tree-plot-tree-regression-multioutput-py) 。 在该示例中,输入X是单个实数值,并且输出Y是X的正弦和余弦。
用多输出决策树进行回归分析 [Multi-output Decision Tree Regression](https://scikit-learn.org/stable/auto_examples/tree/plot_tree_regression_multioutput.html#sphx-glr-auto-examples-tree-plot-tree-regression-multioutput-py) 。 在该示例中,输入X是单个实数值,并且输出Y是X的正弦和余弦。
[![http://sklearn.apachecn.org/cn/0.19.0/_images/sphx_glr_plot_tree_regression_multioutput_0011.png](img/c6b27df44672e7fa50d1d81ffbbebfbd.jpg)](../auto_examples/tree/plot_tree_regression_multioutput.html)
[![http://sklearn.apachecn.org/cn/0.19.0/_images/sphx_glr_plot_tree_regression_multioutput_0011.png](img/c6b27df44672e7fa50d1d81ffbbebfbd.jpg)](https://scikit-learn.org/stable/auto_examples/tree/plot_tree_regression_multioutput.html)
使用多输出树进行分类,在 [Face completion with a multi-output estimators](../auto_examples/plot_multioutput_face_completion.html#sphx-glr-auto-examples-plot-multioutput-face-completion-py) 中进行了演示。 在该示例中,输入X是面的上半部分的像素,并且输出Y是这些面的下半部分的像素。
使用多输出树进行分类,在 [Face completion with a multi-output estimators](https://scikit-learn.org/stable/auto_examples/plot_multioutput_face_completion.html#sphx-glr-auto-examples-plot-multioutput-face-completion-py) 中进行了演示。 在该示例中,输入X是面的上半部分的像素,并且输出Y是这些面的下半部分的像素。
[](../auto_examples/plot_multioutput_face_completion.html)
[](https://scikit-learn.org/stable/auto_examples/plot_multioutput_face_completion.html)
示例:
* [Multi-output Decision Tree Regression](../auto_examples/tree/plot_tree_regression_multioutput.html#sphx-glr-auto-examples-tree-plot-tree-regression-multioutput-py)
* [Face completion with a multi-output estimators](../auto_examples/plot_multioutput_face_completion.html#sphx-glr-auto-examples-plot-multioutput-face-completion-py)
* [Multi-output Decision Tree Regression](https://scikit-learn.org/stable/auto_examples/tree/plot_tree_regression_multioutput.html#sphx-glr-auto-examples-tree-plot-tree-regression-multioutput-py)
* [Face completion with a multi-output estimators](https://scikit-learn.org/stable/auto_examples/plot_multioutput_face_completion.html#sphx-glr-auto-examples-plot-multioutput-face-completion-py)
参考:
......
此差异已折叠。
此差异已折叠。
此差异已折叠。
......@@ -7,7 +7,7 @@
[半监督学习](https://en.wikipedia.org/wiki/Semi-supervised_learning) 适用于在训练数据上的一些样本数据没有贴上标签的情况。 [`sklearn.semi_supervised`](classes.html#module-sklearn.semi_supervised "sklearn.semi_supervised") 中的半监督估计, 能够利用这些附加的未标记数据来更好地捕获底层数据分布的形状,并将其更好地类推到新的样本。 当我们有非常少量的已标签化的点和大量的未标签化的点时,这些算法表现均良好。
<cite>y</cite> 中含有未标记的数据
*y* 中含有未标记的数据
在使用 `fit` 方法训练数据时, 将标识符与已标签化的数据一起分配给未标签化的点是尤其重要的. 实现该标记的方法是使用整数值 ![-1](img/8b3be66a25e0c33787b341667b261360.jpg).
......@@ -22,15 +22,15 @@
* 可用于分类和回归任务
* Kernel methods to project data into alternate dimensional spaces
<cite>scikit-learn</cite> 提供了两种标签传播模型: [`LabelPropagation`](generated/sklearn.semi_supervised.LabelPropagation.html#sklearn.semi_supervised.LabelPropagation "sklearn.semi_supervised.LabelPropagation")[`LabelSpreading`](generated/sklearn.semi_supervised.LabelSpreading.html#sklearn.semi_supervised.LabelSpreading "sklearn.semi_supervised.LabelSpreading") 。 两者都通过在输入的 dataset(数据集)中的所有 items(项)上构建 similarity graph (相似图)来进行工作。
*scikit-learn* 提供了两种标签传播模型: [`LabelPropagation`](https://scikit-learn.org/stable/modules/generated/sklearn.semi_supervised.LabelPropagation.html#sklearn.semi_supervised.LabelPropagation "sklearn.semi_supervised.LabelPropagation")[`LabelSpreading`](https://scikit-learn.org/stable/modules/generated/sklearn.semi_supervised.LabelSpreading.html#sklearn.semi_supervised.LabelSpreading "sklearn.semi_supervised.LabelSpreading") 。 两者都通过在输入的 dataset(数据集)中的所有 items(项)上构建 similarity graph (相似图)来进行工作。
[![http://sklearn.apachecn.org/cn/0.19.0/_images/sphx_glr_plot_label_propagation_structure_0011.png](img/f38f84b73c7da2884c38bdfca1a38b1c.jpg)](../auto_examples/semi_supervised/plot_label_propagation_structure.html)
[![http://sklearn.apachecn.org/cn/0.19.0/_images/sphx_glr_plot_label_propagation_structure_0011.png](img/f38f84b73c7da2884c38bdfca1a38b1c.jpg)](https://scikit-learn.org/stable/auto_examples/semi_supervised/plot_label_propagation_structure.html)
**标签传播说明:** _未标签化的观察值结构与 class(类)结构一致, 因此可以将 class(类)标签传播到训练集的未标签化的观察值_
[`LabelPropagation`](generated/sklearn.semi_supervised.LabelPropagation.html#sklearn.semi_supervised.LabelPropagation "sklearn.semi_supervised.LabelPropagation")[`LabelSpreading`](generated/sklearn.semi_supervised.LabelSpreading.html#sklearn.semi_supervised.LabelSpreading "sklearn.semi_supervised.LabelSpreading") 在对图形的相似性矩阵, 以及对标签分布的 clamping effect(夹持效应)的修改方面不太一样。 Clamping 允许算法在一定程度上改变真实标签化数据的权重。 该 [`LabelPropagation`](generated/sklearn.semi_supervised.LabelPropagation.html#sklearn.semi_supervised.LabelPropagation "sklearn.semi_supervised.LabelPropagation") 算法执行输入标签的 hard clamping, 这意味着 ![\alpha=0](img/1ff751c4de3bbad5543f0dbbad73dd35.jpg) 。 这些 clamping factor 可以不是很严格的, 例如 ![\alpha=0.2](img/db30d43fd890d5f28b84a667ddfbb39d.jpg) , 这意味着我们将始终保留原始标签分配的 80%, 但该算法可以将其分布的置信度改变在 20% 以内。
[`LabelPropagation`](https://scikit-learn.org/stable/modules/generated/sklearn.semi_supervised.LabelPropagation.html#sklearn.semi_supervised.LabelPropagation "sklearn.semi_supervised.LabelPropagation")[`LabelSpreading`](https://scikit-learn.org/stable/modules/generated/sklearn.semi_supervised.LabelSpreading.html#sklearn.semi_supervised.LabelSpreading "sklearn.semi_supervised.LabelSpreading") 在对图形的相似性矩阵, 以及对标签分布的 clamping effect(夹持效应)的修改方面不太一样。 Clamping 允许算法在一定程度上改变真实标签化数据的权重。 该 [`LabelPropagation`](https://scikit-learn.org/stable/modules/generated/sklearn.semi_supervised.LabelPropagation.html#sklearn.semi_supervised.LabelPropagation "sklearn.semi_supervised.LabelPropagation") 算法执行输入标签的 hard clamping, 这意味着 ![\alpha=0](img/1ff751c4de3bbad5543f0dbbad73dd35.jpg) 。 这些 clamping factor 可以不是很严格的, 例如 ![\alpha=0.2](img/db30d43fd890d5f28b84a667ddfbb39d.jpg) , 这意味着我们将始终保留原始标签分配的 80%, 但该算法可以将其分布的置信度改变在 20% 以内。
[`LabelPropagation`](generated/sklearn.semi_supervised.LabelPropagation.html#sklearn.semi_supervised.LabelPropagation "sklearn.semi_supervised.LabelPropagation") 使用原始相似性矩阵从未修改的数据来构建。 [`LabelSpreading`](generated/sklearn.semi_supervised.LabelSpreading.html#sklearn.semi_supervised.LabelSpreading "sklearn.semi_supervised.LabelSpreading") 最小化具有正则化属性的损耗函数, 因此它通常更适用于噪声数据。 该算法在原始图形的修改版本上进行迭代, 并通过计算 normalized graph Laplacian matrix (归一化图拉普拉斯矩阵)来对边缘的权重进行归一化。 此过程也用于 [Spectral clustering](clustering.html#spectral-clustering)
[`LabelPropagation`](https://scikit-learn.org/stable/modules/generated/sklearn.semi_supervised.LabelPropagation.html#sklearn.semi_supervised.LabelPropagation "sklearn.semi_supervised.LabelPropagation") 使用原始相似性矩阵从未修改的数据来构建。 [`LabelSpreading`](https://scikit-learn.org/stable/modules/generated/sklearn.semi_supervised.LabelSpreading.html#sklearn.semi_supervised.LabelSpreading "sklearn.semi_supervised.LabelSpreading") 最小化具有正则化属性的损耗函数, 因此它通常更适用于噪声数据。 该算法在原始图形的修改版本上进行迭代, 并通过计算 normalized graph Laplacian matrix (归一化图拉普拉斯矩阵)来对边缘的权重进行归一化。 此过程也用于 [Spectral clustering](clustering.html#spectral-clustering)
标签传播模型有两种内置的 kernel methods(核函数)。 kernel (核)的选择会影响算法的可扩展性和性能。 以下是可用的:
......@@ -41,9 +41,9 @@ RBF 核将产生一个完全连接的图形, 它通过密集矩阵在内存中
例子
* [Decision boundary of label propagation versus SVM on the Iris dataset](../auto_examples/semi_supervised/plot_label_propagation_versus_svm_iris.html#sphx-glr-auto-examples-semi-supervised-plot-label-propagation-versus-svm-iris-py)
* [Label Propagation learning a complex structure](../auto_examples/semi_supervised/plot_label_propagation_structure.html#sphx-glr-auto-examples-semi-supervised-plot-label-propagation-structure-py)
* [Label Propagation digits active learning](../auto_examples/semi_supervised/plot_label_propagation_digits_active_learning.html#sphx-glr-auto-examples-semi-supervised-plot-label-propagation-digits-active-learning-py)
* [Decision boundary of label propagation versus SVM on the Iris dataset](https://scikit-learn.org/stable/auto_examples/semi_supervised/plot_label_propagation_versus_svm_iris.html#sphx-glr-auto-examples-semi-supervised-plot-label-propagation-versus-svm-iris-py)
* [Label Propagation learning a complex structure](https://scikit-learn.org/stable/auto_examples/semi_supervised/plot_label_propagation_structure.html#sphx-glr-auto-examples-semi-supervised-plot-label-propagation-structure-py)
* [Label Propagation digits active learning](https://scikit-learn.org/stable/auto_examples/semi_supervised/plot_label_propagation_digits_active_learning.html#sphx-glr-auto-examples-semi-supervised-plot-label-propagation-digits-active-learning-py)
参考
......
......@@ -5,7 +5,7 @@
翻译者:
        [@Damon](https://github.com/apachecn/scikit-learn-doc-zh)
[`IsotonicRegression`](generated/sklearn.isotonic.IsotonicRegression.html#sklearn.isotonic.IsotonicRegression "sklearn.isotonic.IsotonicRegression") 类对数据进行非降函数拟合. 它解决了如下的问题:
[`IsotonicRegression`](https://scikit-learn.org/stable/modules/generated/sklearn.isotonic.IsotonicRegression.html#sklearn.isotonic.IsotonicRegression "sklearn.isotonic.IsotonicRegression") 类对数据进行非降函数拟合. 它解决了如下的问题:
> 最小化 ![\sum_i w_i (y_i - \hat{y}_i)^2](img/156554c81cfe5f0230627ac0487fd07f.jpg)
>
......@@ -13,4 +13,4 @@
其中每一个 ![w_i](img/6689aa593e8e42bb5c2caa474e642b5f.jpg) 是 strictly 正数而且每个 ![y_i](img/4a22ca544916918b2358e5fc7c71b8e6.jpg) 是任意实 数. 它生成一个由平方误差接近的不减元素组成的向量.实际上这一些元素形成 一个分段线性的函数.
[![http://sklearn.apachecn.org/cn/0.19.0/_images/sphx_glr_plot_isotonic_regression_0011.png](img/87ae31ef4f2e7b4385b9a25aa8fed533.jpg)](../auto_examples/plot_isotonic_regression.html)
\ No newline at end of file
[![http://sklearn.apachecn.org/cn/0.19.0/_images/sphx_glr_plot_isotonic_regression_0011.png](img/87ae31ef4f2e7b4385b9a25aa8fed533.jpg)](https://scikit-learn.org/stable/auto_examples/plot_isotonic_regression.html)
\ No newline at end of file
......@@ -10,47 +10,47 @@
精确校准的分类器是概率分类器, 其可以将 predict_proba 方法的输出直接解释为 confidence level(置信度级别). 例如,一个经过良好校准的(二元的)分类器应该对样本进行分类, 使得在给出一个接近 0.8 的 prediction_proba 值的样本中, 大约 80% 实际上属于正类. 以下图表比较了校准不同分类器的概率预测的良好程度:
[![http://sklearn.apachecn.org/cn/0.19.0/_images/sphx_glr_plot_compare_calibration_0011.png](img/2490002eaeb5d875d22825fc267ef925.jpg)](../auto_examples/calibration/plot_compare_calibration.html)
[![http://sklearn.apachecn.org/cn/0.19.0/_images/sphx_glr_plot_compare_calibration_0011.png](img/2490002eaeb5d875d22825fc267ef925.jpg)](https://scikit-learn.org/stable/auto_examples/calibration/plot_compare_calibration.html)
[`LogisticRegression`](generated/sklearn.linear_model.LogisticRegression.html#sklearn.linear_model.LogisticRegression "sklearn.linear_model.LogisticRegression") 默认情况下返回良好的校准预测, 因为它直接优化了 log-loss(对数损失)情况. 相反,其他方法返回 biased probabilities(偏倚概率); 每种方法有不同的偏差:
[`LogisticRegression`](https://scikit-learn.org/stable/modules/generated/sklearn.linear_model.LogisticRegression.html#sklearn.linear_model.LogisticRegression "sklearn.linear_model.LogisticRegression") 默认情况下返回良好的校准预测, 因为它直接优化了 log-loss(对数损失)情况. 相反,其他方法返回 biased probabilities(偏倚概率); 每种方法有不同的偏差:
* [`GaussianNB`](generated/sklearn.naive_bayes.GaussianNB.html#sklearn.naive_bayes.GaussianNB "sklearn.naive_bayes.GaussianNB") 往往将概率推到 0 或 1(注意直方图中的计数). 这主要是因为它假设特征在给定类别的条件下是独立的, 在该数据集中不包含 2 个冗余特征.
* [`GaussianNB`](https://scikit-learn.org/stable/modules/generated/sklearn.naive_bayes.GaussianNB.html#sklearn.naive_bayes.GaussianNB "sklearn.naive_bayes.GaussianNB") 往往将概率推到 0 或 1(注意直方图中的计数). 这主要是因为它假设特征在给定类别的条件下是独立的, 在该数据集中不包含 2 个冗余特征.
* [`RandomForestClassifier`](generated/sklearn.ensemble.RandomForestClassifier.html#sklearn.ensemble.RandomForestClassifier "sklearn.ensemble.RandomForestClassifier") 解释了相反的行为:直方图在约 0.2 和 0.9 的概率时显示峰值, 而接近 0 或 1 的概率非常罕见. Niculescu-Mizil 和 Caruana [4] 给出了一个解释:”诸如 bagging 和 random forests(随机森林)的方法, 从基本模型的平均预测中可能难以将预测置于 0 和 1 附近, 因为基础模型的变化会偏离预测值, 它们应该接近于零或偏离这些值, 因为预测被限制在 [0,1] 的间隔, 由方差引起的误差往往是靠近 0 和 1 的一边, 例如,如果一个模型应该对于一个案例,预测 p = 0,bagging 可以实现的唯一方法是假设所有的 bagging 树预测为零. 如果我们在 bagging 树上增加噪声, 这种噪声将导致一些树预测大于 0 的值, 因此将 bagging 的平均预测从 0 移开. 我们用随机森林最强烈地观察到这种效应, 因为用随机森林训练的 base-level 树由于特征划分而具有相对较高的方差. 因此,校准曲线也被称为可靠性图 (Wilks 1995 [5] _) 显示了一种典型的 sigmoid 形状, 表明分类器可以更多地信任其 “直觉”, 并通常将接近 0 或 1 的概率返回.
* [`RandomForestClassifier`](https://scikit-learn.org/stable/modules/generated/sklearn.ensemble.RandomForestClassifier.html#sklearn.ensemble.RandomForestClassifier "sklearn.ensemble.RandomForestClassifier") 解释了相反的行为:直方图在约 0.2 和 0.9 的概率时显示峰值, 而接近 0 或 1 的概率非常罕见. Niculescu-Mizil 和 Caruana [4] 给出了一个解释:”诸如 bagging 和 random forests(随机森林)的方法, 从基本模型的平均预测中可能难以将预测置于 0 和 1 附近, 因为基础模型的变化会偏离预测值, 它们应该接近于零或偏离这些值, 因为预测被限制在 [0,1] 的间隔, 由方差引起的误差往往是靠近 0 和 1 的一边, 例如,如果一个模型应该对于一个案例,预测 p = 0,bagging 可以实现的唯一方法是假设所有的 bagging 树预测为零. 如果我们在 bagging 树上增加噪声, 这种噪声将导致一些树预测大于 0 的值, 因此将 bagging 的平均预测从 0 移开. 我们用随机森林最强烈地观察到这种效应, 因为用随机森林训练的 base-level 树由于特征划分而具有相对较高的方差. 因此,校准曲线也被称为可靠性图 (Wilks 1995 [5] _) 显示了一种典型的 sigmoid 形状, 表明分类器可以更多地信任其 “直觉”, 并通常将接近 0 或 1 的概率返回.
* 线性支持向量分类 ([`LinearSVC`](generated/sklearn.svm.LinearSVC.html#sklearn.svm.LinearSVC "sklearn.svm.LinearSVC")) 显示了作为 RandomForestClassifier 更多的 Sigmoid 曲线, 这是经典的最大边距方法 (compare Niculescu-Mizil and Caruana [[4]](#id3)), 其重点是靠近决策边界的 hard samples(支持向量).
* 线性支持向量分类 ([`LinearSVC`](https://scikit-learn.org/stable/modules/generated/sklearn.svm.LinearSVC.html#sklearn.svm.LinearSVC "sklearn.svm.LinearSVC")) 显示了作为 RandomForestClassifier 更多的 Sigmoid 曲线, 这是经典的最大边距方法 (compare Niculescu-Mizil and Caruana [[4]](#id3)), 其重点是靠近决策边界的 hard samples(支持向量).
提供了执行概率预测校准的两种方法: 基于 Platt 的 Sigmoid 模型的参数化方法和基于 isotonic regression(保序回归)的非参数方法 ([`sklearn.isotonic`](classes.html#module-sklearn.isotonic "sklearn.isotonic")). 对于不用于模型拟合的新数据, 应进行概率校准. 类 [`CalibratedClassifierCV`](generated/sklearn.calibration.CalibratedClassifierCV.html#sklearn.calibration.CalibratedClassifierCV "sklearn.calibration.CalibratedClassifierCV") 使用交叉验证生成器, 并对每个拆分模型参数对训练样本和测试样本的校准进行估计. 然后对折叠预测的概率进行平均. 已经安装的分类器可以通过:class:<cite>CalibratedClassifierCV</cite> 传递参数 cv =”prefit” 这种方式进行校准. 在这种情况下, 用户必须手动注意模型拟合和校准的数据是不相交的.
提供了执行概率预测校准的两种方法: 基于 Platt 的 Sigmoid 模型的参数化方法和基于 isotonic regression(保序回归)的非参数方法 ([`sklearn.isotonic`](classes.html#module-sklearn.isotonic "sklearn.isotonic")). 对于不用于模型拟合的新数据, 应进行概率校准. 类 [`CalibratedClassifierCV`](https://scikit-learn.org/stable/modules/generated/sklearn.calibration.CalibratedClassifierCV.html#sklearn.calibration.CalibratedClassifierCV "sklearn.calibration.CalibratedClassifierCV") 使用交叉验证生成器, 并对每个拆分模型参数对训练样本和测试样本的校准进行估计. 然后对折叠预测的概率进行平均. 已经安装的分类器可以通过:class:*CalibratedClassifierCV* 传递参数 cv =”prefit” 这种方式进行校准. 在这种情况下, 用户必须手动注意模型拟合和校准的数据是不相交的.
以下图像展示了概率校准的好处. 第一个图像显示一个具有 2 个类和 3 个数据块的数据集. 中间的数据块包含每个类的随机样本. 此数据块中样本的概率应为 0.5.
[![http://sklearn.apachecn.org/cn/0.19.0/_images/sphx_glr_plot_calibration_0011.png](img/6a0ac3a1f45ae7072f58ae85160eca33.jpg)](../auto_examples/calibration/plot_calibration.html)
[![http://sklearn.apachecn.org/cn/0.19.0/_images/sphx_glr_plot_calibration_0011.png](img/6a0ac3a1f45ae7072f58ae85160eca33.jpg)](https://scikit-learn.org/stable/auto_examples/calibration/plot_calibration.html)
以下图像使用没有校准的高斯朴素贝叶斯分类器, 使用 sigmoid 校准和非参数的等渗校准来显示上述估计概率的数据. 可以观察到, 非参数模型为中间样本提供最准确的概率估计, 即0.5.
[![http://sklearn.apachecn.org/cn/0.19.0/_images/sphx_glr_plot_calibration_0021.png](img/b7a68272ceb9cb09f48103e44f469470.jpg)](../auto_examples/calibration/plot_calibration.html)
[![http://sklearn.apachecn.org/cn/0.19.0/_images/sphx_glr_plot_calibration_0021.png](img/b7a68272ceb9cb09f48103e44f469470.jpg)](https://scikit-learn.org/stable/auto_examples/calibration/plot_calibration.html)
对具有20个特征的100.000个样本(其中一个用于模型拟合)进行二元分类的人造数据集进行以下实验. 在 20个 特征中,只有 2 个是信息量, 10 个是冗余的. 该图显示了使用逻辑回归获得的估计概率, 线性支持向量分类器(SVC)和具有 sigmoid 校准和 sigmoid 校准的线性 SVC. 校准性能使用 Brier score [`brier_score_loss`](generated/sklearn.metrics.brier_score_loss.html#sklearn.metrics.brier_score_loss "sklearn.metrics.brier_score_loss") 来计算, 请看下面的图例(越销越好).
对具有20个特征的100.000个样本(其中一个用于模型拟合)进行二元分类的人造数据集进行以下实验. 在 20个 特征中,只有 2 个是信息量, 10 个是冗余的. 该图显示了使用逻辑回归获得的估计概率, 线性支持向量分类器(SVC)和具有 sigmoid 校准和 sigmoid 校准的线性 SVC. 校准性能使用 Brier score [`brier_score_loss`](https://scikit-learn.org/stable/modules/generated/sklearn.metrics.brier_score_loss.html#sklearn.metrics.brier_score_loss "sklearn.metrics.brier_score_loss") 来计算, 请看下面的图例(越销越好).
[![http://sklearn.apachecn.org/cn/0.19.0/_images/sphx_glr_plot_calibration_curve_0021.png](img/0563dd4e7c322dfdafde0770d8a9dc4a.jpg)](../auto_examples/calibration/plot_calibration_curve.html)
[![http://sklearn.apachecn.org/cn/0.19.0/_images/sphx_glr_plot_calibration_curve_0021.png](img/0563dd4e7c322dfdafde0770d8a9dc4a.jpg)](https://scikit-learn.org/stable/auto_examples/calibration/plot_calibration_curve.html)
这里可以观察到, 逻辑回归被很好地校准, 因为其曲线几乎是对角线. 线性 SVC 的校准曲线或可靠性图具有 sigmoid 曲线, 这是一个典型的不够自信的分类器. 在 LinearSVC 的情况下, 这是 hinge loss 的边缘属性引起的, 这使得模型集中在靠近决策边界(支持向量)的 hard samples(硬样本)上. 这两种校准都可以解决这个问题, 并产生几乎相同的结果. 下图显示了高斯朴素贝叶斯在相同数据上的校准曲线, 具有两种校准, 也没有校准.
[![http://sklearn.apachecn.org/cn/0.19.0/_images/sphx_glr_plot_calibration_curve_0011.png](img/dd9b7456f8406b6fac80cfd3ca11ccfc.jpg)](../auto_examples/calibration/plot_calibration_curve.html)
[![http://sklearn.apachecn.org/cn/0.19.0/_images/sphx_glr_plot_calibration_curve_0011.png](img/dd9b7456f8406b6fac80cfd3ca11ccfc.jpg)](https://scikit-learn.org/stable/auto_examples/calibration/plot_calibration_curve.html)
可以看出, 高斯朴素贝叶斯的表现非常差, 但是以线性 SVC 的方式也是如此. 尽管线性 SVC 显示了 sigmoid 校准曲线, 但高斯朴素贝叶斯校准曲线具有转置的 sigmoid 结构. 这对于过分自信的分类器来说是非常经典的. 在这种情况下,分类器的过度自信是由违反朴素贝叶斯特征独立假设的冗余特征引起的.
用等渗回归法对高斯朴素贝叶斯概率的校准可以解决这个问题, 从几乎对角线校准曲线可以看出. Sigmoid 校准也略微改善了 brier 评分, 尽管不如非参数等渗校准那样强烈. 这是 sigmoid 校准的固有限制,其参数形式假定为 sigmoid ,而不是转置的 sigmoid 曲线. 然而, 非参数等渗校准模型没有这样强大的假设, 并且可以处理任何形状, 只要有足够的校准数据. 通常,在校准曲线为 sigmoid 且校准数据有限的情况下, sigmoid 校准是优选的, 而对于非 sigmoid 校准曲线和大量数据可用于校准的情况,等渗校准是优选的.
[`CalibratedClassifierCV`](generated/sklearn.calibration.CalibratedClassifierCV.html#sklearn.calibration.CalibratedClassifierCV "sklearn.calibration.CalibratedClassifierCV") 也可以处理涉及两个以上类的分类任务, 如果基本估计器可以这样做的话. 在这种情况下, 分类器是以一对一的方式分别对每个类进行校准. 当预测未知数据的概率时, 分别预测每个类的校准概率. 由于这些概率并不总是一致, 因此执行后处理以使它们归一化.
[`CalibratedClassifierCV`](https://scikit-learn.org/stable/modules/generated/sklearn.calibration.CalibratedClassifierCV.html#sklearn.calibration.CalibratedClassifierCV "sklearn.calibration.CalibratedClassifierCV") 也可以处理涉及两个以上类的分类任务, 如果基本估计器可以这样做的话. 在这种情况下, 分类器是以一对一的方式分别对每个类进行校准. 当预测未知数据的概率时, 分别预测每个类的校准概率. 由于这些概率并不总是一致, 因此执行后处理以使它们归一化.
下一个图像说明了 Sigmoid 校准如何改变 3 类分类问题的预测概率. 说明是标准的 2-simplex,其中三个角对应于三个类. 箭头从未校准分类器预测的概率向量指向在保持验证集上的 sigmoid 校准之后由同一分类器预测的概率向量. 颜色表示实例的真实类(red: class 1, green: class 2, blue: class 3).
[![http://sklearn.apachecn.org/cn/0.19.0/_images/sphx_glr_plot_calibration_multiclass_0001.png](img/7ea4ef13cdad8ec1dea90fc195d058b1.jpg)](../auto_examples/calibration/plot_calibration_multiclass.html)
[![http://sklearn.apachecn.org/cn/0.19.0/_images/sphx_glr_plot_calibration_multiclass_0001.png](img/7ea4ef13cdad8ec1dea90fc195d058b1.jpg)](https://scikit-learn.org/stable/auto_examples/calibration/plot_calibration_multiclass.html)
基础分类器是具有 25 个基本估计器(树)的随机森林分类器. 如果这个分类器对所有 800 个训练数据点进行了训练, 那么它的预测过于自信, 从而导致了大量的对数损失. 校准在 600 个数据点上训练的相同分类器, 其余 200 个数据点上的 method =’sigmoid’ 减少了预测的置信度, 即将概率向量从单面的边缘向中心移动:
[![http://sklearn.apachecn.org/cn/0.19.0/_images/sphx_glr_plot_calibration_multiclass_0011.png](img/ef59d065ac32862cd7527b0c3fcf7956.jpg)](../auto_examples/calibration/plot_calibration_multiclass.html)
[![http://sklearn.apachecn.org/cn/0.19.0/_images/sphx_glr_plot_calibration_multiclass_0011.png](img/ef59d065ac32862cd7527b0c3fcf7956.jpg)](https://scikit-learn.org/stable/auto_examples/calibration/plot_calibration_multiclass.html)
该校准导致较低的 log-loss(对数损失). 请注意,替代方案是增加基准估计量的数量, 这将导致对数损失类似的减少.
......
......@@ -37,7 +37,7 @@ Warning
## 1.17.2\. 分类
> [`MLPClassifier`](generated/sklearn.neural_network.MLPClassifier.html#sklearn.neural_network.MLPClassifier "sklearn.neural_network.MLPClassifier") 类实现了通过 [Backpropagation](http://ufldl.stanford.edu/wiki/index.php/Backpropagation_Algorithm) 进行训练的多层感知器(MLP)算法。
> [`MLPClassifier`](https://scikit-learn.org/stable/modules/generated/sklearn.neural_network.MLPClassifier.html#sklearn.neural_network.MLPClassifier "sklearn.neural_network.MLPClassifier") 类实现了通过 [Backpropagation](http://ufldl.stanford.edu/wiki/index.php/Backpropagation_Algorithm) 进行训练的多层感知器(MLP)算法。
MLP 在两个 array 上进行训练:大小为 (n_samples, n_features) 的 array X 储存表示训练样本的浮点型特征向量; 大小为 (n_samples,) 的 array y 储存训练样本的目标值(类别标签):
......@@ -75,7 +75,7 @@ MLP 可以为训练数据拟合一个非线性模型。 `clf.coefs_` 包含了
```
目前, [`MLPClassifier`](generated/sklearn.neural_network.MLPClassifier.html#sklearn.neural_network.MLPClassifier "sklearn.neural_network.MLPClassifier") 只支持交叉熵损失函数,通过运行 `predict_proba` 方法进行概率估计。
目前, [`MLPClassifier`](https://scikit-learn.org/stable/modules/generated/sklearn.neural_network.MLPClassifier.html#sklearn.neural_network.MLPClassifier "sklearn.neural_network.MLPClassifier") 只支持交叉熵损失函数,通过运行 `predict_proba` 方法进行概率估计。
MLP 算法使用的是反向传播的方式。 更准确地说,它使用了通过反向传播计算得到的梯度和某种形式的梯度下降来进行训练。 对于分类来说,它最小化交叉熵损失函数,为每个样本 ![x](img/5c82dbae35dc43d2f556f9f284d9d184.jpg) 给出一个向量形式的概率估计 ![P(y|x)](img/3cca81fd08a4732dc7061cd246b323ed.jpg)
......@@ -86,7 +86,7 @@ array([[ 1.967...e-04, 9.998...-01],
```
[`MLPClassifier`](generated/sklearn.neural_network.MLPClassifier.html#sklearn.neural_network.MLPClassifier "sklearn.neural_network.MLPClassifier") 通过应用 [Softmax](https://en.wikipedia.org/wiki/Softmax_activation_function) 作为输出函数来支持多分类。
[`MLPClassifier`](https://scikit-learn.org/stable/modules/generated/sklearn.neural_network.MLPClassifier.html#sklearn.neural_network.MLPClassifier "sklearn.neural_network.MLPClassifier") 通过应用 [Softmax](https://en.wikipedia.org/wiki/Softmax_activation_function) 作为输出函数来支持多分类。
此外,该模型支持 [多标签分类](multiclass.html#multiclass) ,一个样本可能属于多个类别。 对于每个类,原始输出经过 logistic 函数变换后,大于或等于 0.5 的值将进为 1,否则为 0。 对于样本的预测输出,值为 1 的索引位置表示该样本的分类类别:
......@@ -111,30 +111,30 @@ array([[0, 1]])
```
更多内容请参阅下面的示例和文档 [`MLPClassifier.fit`](generated/sklearn.neural_network.MLPClassifier.html#sklearn.neural_network.MLPClassifier.fit "sklearn.neural_network.MLPClassifier.fit")
更多内容请参阅下面的示例和文档 [`MLPClassifier.fit`](https://scikit-learn.org/stable/modules/generated/sklearn.neural_network.MLPClassifier.html#sklearn.neural_network.MLPClassifier.fit "sklearn.neural_network.MLPClassifier.fit")
示例:
* [Compare Stochastic learning strategies for MLPClassifier](../auto_examples/neural_networks/plot_mlp_training_curves.html#sphx-glr-auto-examples-neural-networks-plot-mlp-training-curves-py)
* [Visualization of MLP weights on MNIST](../auto_examples/neural_networks/plot_mnist_filters.html#sphx-glr-auto-examples-neural-networks-plot-mnist-filters-py)
* [Compare Stochastic learning strategies for MLPClassifier](https://scikit-learn.org/stable/auto_examples/neural_networks/plot_mlp_training_curves.html#sphx-glr-auto-examples-neural-networks-plot-mlp-training-curves-py)
* [Visualization of MLP weights on MNIST](https://scikit-learn.org/stable/auto_examples/neural_networks/plot_mnist_filters.html#sphx-glr-auto-examples-neural-networks-plot-mnist-filters-py)
## 1.17.3\. 回归
[`MLPRegressor`](generated/sklearn.neural_network.MLPRegressor.html#sklearn.neural_network.MLPRegressor "sklearn.neural_network.MLPRegressor") 类多层感知器(MLP)的实现,在使用反向传播进行训练时的输出层没有使用激活函数,也可以看作是使用恒等函数(identity function)作为激活函数。 因此,它使用平方误差作为损失函数,输出是一组连续值。
[`MLPRegressor`](https://scikit-learn.org/stable/modules/generated/sklearn.neural_network.MLPRegressor.html#sklearn.neural_network.MLPRegressor "sklearn.neural_network.MLPRegressor") 类多层感知器(MLP)的实现,在使用反向传播进行训练时的输出层没有使用激活函数,也可以看作是使用恒等函数(identity function)作为激活函数。 因此,它使用平方误差作为损失函数,输出是一组连续值。
[`MLPRegressor`](generated/sklearn.neural_network.MLPRegressor.html#sklearn.neural_network.MLPRegressor "sklearn.neural_network.MLPRegressor") 还支持多输出回归,其中一个样本可以有多个目标值。
[`MLPRegressor`](https://scikit-learn.org/stable/modules/generated/sklearn.neural_network.MLPRegressor.html#sklearn.neural_network.MLPRegressor "sklearn.neural_network.MLPRegressor") 还支持多输出回归,其中一个样本可以有多个目标值。
## 1.17.4\. 正则化
[`MLPRegressor`](generated/sklearn.neural_network.MLPRegressor.html#sklearn.neural_network.MLPRegressor "sklearn.neural_network.MLPRegressor") 类和 [`MLPClassifier`](generated/sklearn.neural_network.MLPClassifier.html#sklearn.neural_network.MLPClassifier "sklearn.neural_network.MLPClassifier") 类都使用参数 `alpha` 作为正则化( L2 正则化)系数,正则化通过惩罚大数量级的权重值以避免过拟合问题。 下面的图表展示了不同的 alpha 值下的决策函数的变化。
[`MLPRegressor`](https://scikit-learn.org/stable/modules/generated/sklearn.neural_network.MLPRegressor.html#sklearn.neural_network.MLPRegressor "sklearn.neural_network.MLPRegressor") 类和 [`MLPClassifier`](https://scikit-learn.org/stable/modules/generated/sklearn.neural_network.MLPClassifier.html#sklearn.neural_network.MLPClassifier "sklearn.neural_network.MLPClassifier") 类都使用参数 `alpha` 作为正则化( L2 正则化)系数,正则化通过惩罚大数量级的权重值以避免过拟合问题。 下面的图表展示了不同的 alpha 值下的决策函数的变化。
[![http://sklearn.apachecn.org/cn/0.19.0/_images/sphx_glr_plot_mlp_alpha_0011.png](img/cfe45a2d171ae9c5933cd6d48cd48cb0.jpg)](../auto_examples/neural_networks/plot_mlp_alpha.html)
[![http://sklearn.apachecn.org/cn/0.19.0/_images/sphx_glr_plot_mlp_alpha_0011.png](img/cfe45a2d171ae9c5933cd6d48cd48cb0.jpg)](https://scikit-learn.org/stable/auto_examples/neural_networks/plot_mlp_alpha.html)
详细信息,请参阅下面的示例。
示例:
* [Varying regularization in Multi-layer Perceptron](../auto_examples/neural_networks/plot_mlp_alpha.html#sphx-glr-auto-examples-neural-networks-plot-mlp-alpha-py)
* [Varying regularization in Multi-layer Perceptron](https://scikit-learn.org/stable/auto_examples/neural_networks/plot_mlp_alpha.html#sphx-glr-auto-examples-neural-networks-plot-mlp-alpha-py)
## 1.17.5\. 算法
......@@ -217,9 +217,9 @@ MLP 根据特定问题使用不同的损失函数。 二分类问题的损失函
> > 另一个推荐的方法是在 `Pipeline` 中使用的 `StandardScaler` 。
>
> * 最好使用 `GridSearchCV` 找到一个合理的正则化参数 ![\alpha](img/d8b3d5242d513369a44f8bf0c6112744.jpg) ,通常范围是在 `10.0 ** -np.arange(1, 7)` 。
> * 据经验可知,我们观察到 <cite>L-BFGS</cite> 收敛速度是更快的并且是小数据集上更好的解决方案。对于规模相对比较大的数据集,<cite>Adam</cite> 是非常鲁棒的。 它通常会迅速收敛,并得到相当不错的表现。
> * 据经验可知,我们观察到 *L-BFGS* 收敛速度是更快的并且是小数据集上更好的解决方案。对于规模相对比较大的数据集,*Adam* 是非常鲁棒的。 它通常会迅速收敛,并得到相当不错的表现。
>
> 另一方面,如果学习率调整得正确, 使用 momentum 或 nesterov’s momentum 的 <cite>SGD</cite> 可以比这两种算法更好。
> 另一方面,如果学习率调整得正确, 使用 momentum 或 nesterov’s momentum 的 *SGD* 可以比这两种算法更好。
## 1.17.9\. 使用 warm_start 的更多控制
......
......@@ -93,27 +93,28 @@
* [2.5.6.1\. NMF 与 Frobenius 范数](/docs/24?id=_2561-nmf-与-frobenius-范数)
* [2.5.6.2\. 具有 beta-divergence 的 NMF](/docs/24?id=_2562-具有-beta-divergence-的-nmf)
* [2.5.7\. 隐 Dirichlet 分配(LDA)](/docs/24?id=_257-隐-dirichlet-分配(lda))
* [2.6\. 协方差估计](/docs/25?id=_26-协方差估计)
* [2.7\. 经验协方差](/docs/26?id=_27-经验协方差)
* [2.8\. 收敛协方差](/docs/27?id=_28-收敛协方差)
* [2.8.1\. 基本收敛](/docs/27?id=_281-基本收敛)
* [2.8.2\. Ledoit-Wolf 收敛](/docs/27?id=_282-ledoit-wolf-收敛)
* [2.8.3\. Oracle 近似收缩](/docs/27?id=_283-oracle-近似收缩)
* [2.9\. 稀疏逆协方差](/docs/28?id=_29-稀疏逆协方差)
* [2.10\. Robust 协方差估计](/docs/29?id=_210-robust-协方差估计)
* [2.10.1\. 最小协方差决定](/docs/29?id=_2101-最小协方差决定)
* [2.11\. 新奇和异常值检测](/docs/30?id=_211-新奇和异常值检测)
* [2.11.1\. Novelty Detection(新奇检测)](/docs/30?id=_2111-novelty-detection(新奇检测))
* [2.11.2\. Outlier Detection(异常值检测)](/docs/30?id=_2112-outlier-detection(异常值检测))
* [2.11.2.1\. Fitting an elliptic envelope(椭圆模型拟合)](/docs/30?id=_21121-fitting-an-elliptic-envelope(椭圆模型拟合))
* [2.11.2.2\. Isolation Forest(隔离森林)](/docs/30?id=_21122-isolation-forest(隔离森林))
* [2.11.2.3\. Local Outlier Factor(局部异常系数)](/docs/30?id=_21123-local-outlier-factor(局部异常系数))
* [2.11.2.4\. 一类支持向量机与椭圆模型与隔离森林与局部异常系数](/docs/30?id=_21124-一类支持向量机与椭圆模型与隔离森林与局部异常系数)
* [2.12\. 密度估计](/docs/31?id=_212-密度估计)
* [2.12.1\. 密度估计: 直方图](/docs/31?id=_2121-密度估计:-直方图)
* [2.12.2\. 核密度估计](/docs/31?id=_2122-核密度估计)
* [2.13\. 神经网络模型(无监督)](/docs/32?id=_213-神经网络模型(无监督))
* [2.13.1\. 限制波尔兹曼机](/docs/32?id=_2131-限制波尔兹曼机)
* [2.13.1.1\. 图形模型和参数化](/docs/32?id=_21311-图形模型和参数化)
* [2.13.1.2\. 伯努利限制玻尔兹曼机](/docs/32?id=_21312-伯努利限制玻尔兹曼机)
* [2.13.1.3\. 随机最大似然学习](/docs/32?id=_21313-随机最大似然学习)
\ No newline at end of file
* [2.6.1\. 经验协方差](/docs/25?id=_261-经验协方差)
* [2.6.2\. 收敛协方差](/docs/25?id=_262-收敛协方差)
* [2.6.2.1\. 基本收敛](/docs/25?id=_2621-基本收敛)
* [2.6.2.2\. Ledoit-Wolf 收敛](/docs/25?id=_2622-ledoit-wolf-收敛)
* [2.6.2.3\. Oracle 近似收缩](/docs/25?id=_2623-oracle-近似收缩)
* [2.6.3\. 稀疏逆协方差](/docs/25?id=_263-稀疏逆协方差)
* [2.6.4\. Robust 协方差估计](/docs/25?id=_264-robust-协方差估计)
* [2.6.4.1\. 最小协方差决定](/docs/25?id=_2641-最小协方差决定)
* [2.11\. 新奇和异常值检测](/docs/26?id=_211-新奇和异常值检测)
* [2.11.1\. Novelty Detection(新奇检测)](/docs/26?id=_2111-novelty-detection(新奇检测))
* [2.11.2\. Outlier Detection(异常值检测)](/docs/26?id=_2112-outlier-detection(异常值检测))
* [2.11.2.1\. Fitting an elliptic envelope(椭圆模型拟合)](/docs/26?id=_21121-fitting-an-elliptic-envelope(椭圆模型拟合))
* [2.11.2.2\. Isolation Forest(隔离森林)](/docs/26?id=_21122-isolation-forest(隔离森林))
* [2.11.2.3\. Local Outlier Factor(局部异常系数)](/docs/26?id=_21123-local-outlier-factor(局部异常系数))
* [2.11.2.4\. 一类支持向量机与椭圆模型与隔离森林与局部异常系数](/docs/26?id=_21124-一类支持向量机与椭圆模型与隔离森林与局部异常系数)
* [2.12\. 密度估计](/docs/27?id=_212-密度估计)
* [2.12.1\. 密度估计: 直方图](/docs/27?id=_2121-密度估计:-直方图)
* [2.12.2\. 核密度估计](/docs/27?id=_2122-核密度估计)
* [2.13\. 神经网络模型(无监督)](/docs/28?id=_213-神经网络模型(无监督))
* [2.13.1\. 限制波尔兹曼机](/docs/28?id=_2131-限制波尔兹曼机)
* [2.13.1.1\. 图形模型和参数化](/docs/28?id=_21311-图形模型和参数化)
* [2.13.1.2\. 伯努利限制玻尔兹曼机](/docs/28?id=_21312-伯努利限制玻尔兹曼机)
* [2.13.1.3\. 随机最大似然学习](/docs/28?id=_21313-随机最大似然学习)
......@@ -361,7 +361,7 @@ Alpha 在这里也是作为一个变量,通过数据中估计得到。
默认 ![\alpha_1 = \alpha_2 = \lambda_1 = \lambda_2 = 10^{-6}](img/76530e85b09bd8385fad05337b968caf.jpg).
[![http://sklearn.apachecn.org/cn/0.19.0/_images/sphx_glr_plot_bayesian_ridge_0011.png](img/947ae691edda29c53c3b962665b052c6.jpg)](../auto_examples/linear_model/plot_bayesian_ridge.html)
[![http://sklearn.apachecn.org/cn/0.19.0/_images/sphx_glr_plot_bayesian_ridge_0011.png](img/947ae691edda29c53c3b962665b052c6.jpg)](https://scikit-learn.org/stable/auto_examples/linear_model/plot_bayesian_ridge.html)
贝叶斯岭回归用来解决回归问题:
......@@ -421,13 +421,13 @@ array([ 0.49999993, 0.49999993])
[`Bayesian Ridge Regression`_](#id53) 不同, 每个 ![w_{i}](img/848835d5b40c5bd74a6e592a65eed5d6.jpg) 都有一个标准差 ![\lambda_i](img/fc333385a9012524b39bc23303de30d4.jpg) 。所有 ![\lambda_i](img/fc333385a9012524b39bc23303de30d4.jpg) 的先验分布 由超参数 ![\lambda_1](img/a6334506478d6feb4025038294ccfa00.jpg) 、 ![\lambda_2](img/535f86af715e90b9c394e3cbf53d99eb.jpg) 确定的相同的 gamma 分布确定。
[![http://sklearn.apachecn.org/cn/0.19.0/_images/sphx_glr_plot_ard_0011.png](img/8aca5aa85ff13bf8e8687220b137f9d3.jpg)](../auto_examples/linear_model/plot_ard.html)
[![http://sklearn.apachecn.org/cn/0.19.0/_images/sphx_glr_plot_ard_0011.png](img/8aca5aa85ff13bf8e8687220b137f9d3.jpg)](https://scikit-learn.org/stable/auto_examples/linear_model/plot_ard.html)
ARD 也被称为 **稀疏贝叶斯学习****相关向量机** [3][4]
>示例:
>
>* [Automatic Relevance Determination Regression (ARD)](../auto_examples/linear_model/plot_ard.html#sphx-glr-auto-examples-linear-model-plot-ard-py)
>* [Automatic Relevance Determination Regression (ARD)](https://scikit-learn.org/stable/auto_examples/linear_model/plot_ard.html#sphx-glr-auto-examples-linear-model-plot-ard-py)
>参考:
>* [1] Christopher M. Bishop: Pattern Recognition and Machine Learning, Chapter 7.2.1
......@@ -525,7 +525,7 @@ Elastic-Net正则化是L1 和 L2的组合,来使如下代价函数最小:
## 1.1.13\. Perceptron(感知器)
[`Perceptron`](generated/sklearn.linear_model.Perceptron.html#sklearn.linear_model.Perceptron "sklearn.linear_model.Perceptron") 是适用于大规模学习的一种简单算法。默认情况下:
[`Perceptron`](https://scikit-learn.org/stable/modules/generated/sklearn.linear_model.Perceptron.html#sklearn.linear_model.Perceptron "sklearn.linear_model.Perceptron") 是适用于大规模学习的一种简单算法。默认情况下:
* 不需要设置学习率(learning rate)。
* 不需要正则化处理。
......@@ -547,7 +547,7 @@ Elastic-Net正则化是L1 和 L2的组合,来使如下代价函数最小:
稳健回归(robust regression)特别适用于回归模型包含损坏数据(corrupt data)的情况,如离群点或模型中的错误。
[![http://sklearn.apachecn.org/cn/0.19.0/_images/sphx_glr_plot_theilsen_0011.png](img/9d2e3befcfa08a4b6a7cfed8dadbd5c0.jpg)](../auto_examples/linear_model/plot_theilsen.html)
[![http://sklearn.apachecn.org/cn/0.19.0/_images/sphx_glr_plot_theilsen_0011.png](img/9d2e3befcfa08a4b6a7cfed8dadbd5c0.jpg)](https://scikit-learn.org/stable/auto_examples/linear_model/plot_theilsen.html)
### 1.1.15.1\. 各种使用场景与相关概念
......@@ -595,7 +595,7 @@ RANSAC 是一种非确定性算法,以一定概率输出一个可能的合理
算法从全体样本输入中分出一个局内点集合,全体样本可能由于测量错误或对数据的假设错误而含有噪点、离群点。最终的模型仅从这个局内点集合中得出。
[![http://sklearn.apachecn.org/cn/0.19.0/_images/sphx_glr_plot_ransac_0011.png](img/658840c7508dc5a73ca6180323904862.jpg)](../auto_examples/linear_model/plot_ransac.html)
[![http://sklearn.apachecn.org/cn/0.19.0/_images/sphx_glr_plot_ransac_0011.png](img/658840c7508dc5a73ca6180323904862.jpg)](https://scikit-learn.org/stable/auto_examples/linear_model/plot_ransac.html)
#### 1.1.15.2.1\. 算法细节
......@@ -641,7 +641,7 @@ RANSAC 是一种非确定性算法,以一定概率输出一个可能的合理
[`TheilSenRegressor`](https://scikit-learn.org/stable/modules/generated/sklearn.linear_model.TheilSenRegressor.html#sklearn.linear_model.TheilSenRegressor) 在渐近效率和无偏估计方面足以媲美 [Ordinary Least Squares (OLS)](#_1111-普通最小二乘法复杂度) (普通最小二乘法(OLS))。与 OLS 不同的是, Theil-Sen 是一种非参数方法,这意味着它没有对底层数据的分布假设。由于 Theil-Sen 是基于中值的估计,它更适合于损坏的数据即离群值。 在单变量的设置中,Theil-Sen 在简单的线性回归的情况下,其崩溃点大约 29.3% ,这意味着它可以容忍任意损坏的数据高达 29.3% 。
[![http://sklearn.apachecn.org/cn/0.19.0/_images/sphx_glr_plot_theilsen_0011.png](img/9d2e3befcfa08a4b6a7cfed8dadbd5c0.jpg)](../auto_examples/linear_model/plot_theilsen.html)
[![http://sklearn.apachecn.org/cn/0.19.0/_images/sphx_glr_plot_theilsen_0011.png](img/9d2e3befcfa08a4b6a7cfed8dadbd5c0.jpg)](https://scikit-learn.org/stable/auto_examples/linear_model/plot_theilsen.html)
scikit-learn 中实现的 [`TheilSenRegressor`](https://scikit-learn.org/stable/modules/generated/sklearn.linear_model.TheilSenRegressor.html#sklearn.linear_model.TheilSenRegressor) 是多元线性回归模型的推广 [[8]](#f1) ,利用了空间中值方法,它是多维中值的推广 [[9]](#f2)
......@@ -662,7 +662,7 @@ scikit-learn 中实现的 [`TheilSenRegressor`](https://scikit-learn.org/stable/
[`HuberRegressor`](https://scikit-learn.org/stable/modules/generated/sklearn.linear_model.HuberRegressor.html#sklearn.linear_model.HuberRegressor)[`Ridge`](https://scikit-learn.org/stable/modules/generated/sklearn.linear_model.Ridge.html#sklearn.linear_model.Ridge) 不同,因为它对于被分为异常值的样本应用了一个线性损失。如果这个样品的绝对误差小于某一阈值,样品就被分为内围值。 它不同于 [`TheilSenRegressor`](https://scikit-learn.org/stable/modules/generated/sklearn.linear_model.TheilSenRegressor.html#sklearn.linear_model.TheilSenRegressor)[`RANSACRegressor`](https://scikit-learn.org/stable/modules/generated/sklearn.linear_model.RANSACRegressor.html#sklearn.linear_model.RANSACRegressor) ,因为它没有忽略异常值的影响,并分配给它们较小的权重。
[![http://sklearn.apachecn.org/cn/0.19.0/_images/sphx_glr_plot_huber_vs_ridge_001.png](img/d06a11a8d6ed2efac238ab0bdbd33326.jpg)](../auto_examples/linear_model/plot_huber_vs_ridge.html)
[![http://sklearn.apachecn.org/cn/0.19.0/_images/sphx_glr_plot_huber_vs_ridge_001.png](img/d06a11a8d6ed2efac238ab0bdbd33326.jpg)](https://scikit-learn.org/stable/auto_examples/linear_model/plot_huber_vs_ridge.html)
这个 [`HuberRegressor`](https://scikit-learn.org/stable/modules/generated/sklearn.linear_model.HuberRegressor.html#sklearn.linear_model.HuberRegressor) 最小化的损失函数是:
......@@ -715,7 +715,7 @@ scikit-learn 中实现的 [`TheilSenRegressor`](https://scikit-learn.org/stable/
这里是一个例子,使用不同程度的多项式特征将这个想法应用于一维数据:
[![http://sklearn.apachecn.org/cn/0.19.0/_images/sphx_glr_plot_polynomial_interpolation_0011.png](img/cda529a3abe8af421f1f218b1a390091.jpg)](../auto_examples/linear_model/plot_polynomial_interpolation.html)
[![http://sklearn.apachecn.org/cn/0.19.0/_images/sphx_glr_plot_polynomial_interpolation_0011.png](img/cda529a3abe8af421f1f218b1a390091.jpg)](https://scikit-learn.org/stable/auto_examples/linear_model/plot_polynomial_interpolation.html)
这个图是使用 [`PolynomialFeatures`](https://scikit-learn.org/stable/modules/generated/sklearn.preprocessing.PolynomialFeatures.html#sklearn.preprocessing.PolynomialFeatures)  预创建。该预处理器将输入数据矩阵转换为给定度的新数据矩阵。使用方法如下:
......
......@@ -8,7 +8,7 @@
`sklearn.mixture` 是一个应用高斯混合模型进行非监督学习的包,支持 diagonal,spherical,tied,full 四种协方差矩阵 (注:diagonal 指每个分量有各自不同对角协方差矩阵, spherical 指每个分量有各自不同的简单协方差矩阵, tied 指所有分量有相同的标准协方差矩阵, full 指每个分量有各自不同的标准协方差矩阵),它对数据进行抽样,并且根据数据估计模型。同时包也提供了相关支持,来帮助用户决定合适的分量数(分量个数)。 _(译注:在高斯混合模型中,我们将每一个高斯分布称为一个分量,即 component )_
> [![http://sklearn.apachecn.org/cn/0.19.0/_images/sphx_glr_plot_gmm_pdf_0011.png](img/ac23b915eb44e4fa668b8d56785e2c20.jpg)](../auto_examples/mixture/plot_gmm_pdf.html)
> [![http://sklearn.apachecn.org/cn/0.19.0/_images/sphx_glr_plot_gmm_pdf_0011.png](img/ac23b915eb44e4fa668b8d56785e2c20.jpg)](https://scikit-learn.org/stable/auto_examples/mixture/plot_gmm_pdf.html)
>
> **二分量高斯混合模型:** _数据点,以及模型的等概率线。_
......@@ -18,20 +18,20 @@
## 2.1.1\. 高斯混合
> [`GaussianMixture`](generated/sklearn.mixture.GaussianMixture.html#sklearn.mixture.GaussianMixture "sklearn.mixture.GaussianMixture") 对象实现了用来拟合高斯混合模型的 [期望最大化](#expectation-maximization) (EM) 算法。它还可以为多变量模型绘制置信区间,同时计算 BIC(Bayesian Information Criterion,贝叶斯信息准则)来评估数据中聚类的数量。 [`GaussianMixture.fit`](generated/sklearn.mixture.GaussianMixture.html#sklearn.mixture.GaussianMixture.fit "sklearn.mixture.GaussianMixture.fit") 提供了从训练数据中学习高斯混合模型的方法。
> [`GaussianMixture`](https://scikit-learn.org/stable/modules/generated/sklearn.mixture.GaussianMixture.html#sklearn.mixture.GaussianMixture "sklearn.mixture.GaussianMixture") 对象实现了用来拟合高斯混合模型的 [期望最大化](#expectation-maximization) (EM) 算法。它还可以为多变量模型绘制置信区间,同时计算 BIC(Bayesian Information Criterion,贝叶斯信息准则)来评估数据中聚类的数量。 [`GaussianMixture.fit`](https://scikit-learn.org/stable/modules/generated/sklearn.mixture.GaussianMixture.html#sklearn.mixture.GaussianMixture.fit "sklearn.mixture.GaussianMixture.fit") 提供了从训练数据中学习高斯混合模型的方法。
给定测试数据,通过使用 [`GaussianMixture.predict`](generated/sklearn.mixture.GaussianMixture.html#sklearn.mixture.GaussianMixture.predict "sklearn.mixture.GaussianMixture.predict") 方法,可以为每个样本分配最有可能对应的高斯分布。
给定测试数据,通过使用 [`GaussianMixture.predict`](https://scikit-learn.org/stable/modules/generated/sklearn.mixture.GaussianMixture.html#sklearn.mixture.GaussianMixture.predict "sklearn.mixture.GaussianMixture.predict") 方法,可以为每个样本分配最有可能对应的高斯分布。
[`GaussianMixture`](generated/sklearn.mixture.GaussianMixture.html#sklearn.mixture.GaussianMixture "sklearn.mixture.GaussianMixture") 方法中自带了不同的选项来约束不同估类的协方差:spherical,diagonal,tied 或 full 协方差。
[`GaussianMixture`](https://scikit-learn.org/stable/modules/generated/sklearn.mixture.GaussianMixture.html#sklearn.mixture.GaussianMixture "sklearn.mixture.GaussianMixture") 方法中自带了不同的选项来约束不同估类的协方差:spherical,diagonal,tied 或 full 协方差。
[![http://sklearn.apachecn.org/cn/0.19.0/_images/sphx_glr_plot_gmm_covariances_0011.png](img/8cc09b4045e2fece3222e52d7333c1e5.jpg)](../auto_examples/mixture/plot_gmm_covariances.html)
[![http://sklearn.apachecn.org/cn/0.19.0/_images/sphx_glr_plot_gmm_covariances_0011.png](img/8cc09b4045e2fece3222e52d7333c1e5.jpg)](https://scikit-learn.org/stable/auto_examples/mixture/plot_gmm_covariances.html)
示例:
* 一个利用高斯混合模型在鸢尾花卉数据集(IRIS 数据集)上做聚类的协方差实例,请查阅 [GMM covariances](../auto_examples/mixture/plot_gmm_covariances.html#sphx-glr-auto-examples-mixture-plot-gmm-covariances-py)
* 一个绘制密度估计的例子,请查阅 [Density Estimation for a Gaussian mixture](../auto_examples/mixture/plot_gmm_pdf.html#sphx-glr-auto-examples-mixture-plot-gmm-pdf-py)
* 一个利用高斯混合模型在鸢尾花卉数据集(IRIS 数据集)上做聚类的协方差实例,请查阅 [GMM covariances](https://scikit-learn.org/stable/auto_examples/mixture/plot_gmm_covariances.html#sphx-glr-auto-examples-mixture-plot-gmm-covariances-py)
* 一个绘制密度估计的例子,请查阅 [Density Estimation for a Gaussian mixture](https://scikit-learn.org/stable/auto_examples/mixture/plot_gmm_pdf.html#sphx-glr-auto-examples-mixture-plot-gmm-pdf-py)
### 2.1.1.1\. 优缺点 [`GaussianMixture`](generated/sklearn.mixture.GaussianMixture.html#sklearn.mixture.GaussianMixture "sklearn.mixture.GaussianMixture")
### 2.1.1.1\. 优缺点 [`GaussianMixture`](https://scikit-learn.org/stable/modules/generated/sklearn.mixture.GaussianMixture.html#sklearn.mixture.GaussianMixture "sklearn.mixture.GaussianMixture")
#### 2.1.1.1.1\. 优点
......@@ -53,11 +53,11 @@
一种高效的方法是利用 BIC(贝叶斯信息准则)来选择高斯混合的分量数。 理论上,它仅当在近似状态下可以恢复正确的分量数(即如果有大量数据可用,并且假设这些数据实际上是一个混合高斯模型独立同分布生成的)。注意:使用 [变分贝叶斯高斯混合](#bgmm) 可以避免高斯混合模型中分量数的选择。
[![http://sklearn.apachecn.org/cn/0.19.0/_images/sphx_glr_plot_gmm_selection_0011.png](img/8615ac5c8ea1fb8ae6e726f7659a29e9.jpg)](../auto_examples/mixture/plot_gmm_selection.html)
[![http://sklearn.apachecn.org/cn/0.19.0/_images/sphx_glr_plot_gmm_selection_0011.png](img/8615ac5c8ea1fb8ae6e726f7659a29e9.jpg)](https://scikit-learn.org/stable/auto_examples/mixture/plot_gmm_selection.html)
示例:
* 一个用典型的高斯混合进行的模型选择的例子,请查阅 [Gaussian Mixture Model Selection](../auto_examples/mixture/plot_gmm_selection.html#sphx-glr-auto-examples-mixture-plot-gmm-selection-py)
* 一个用典型的高斯混合进行的模型选择的例子,请查阅 [Gaussian Mixture Model Selection](https://scikit-learn.org/stable/auto_examples/mixture/plot_gmm_selection.html#sphx-glr-auto-examples-mixture-plot-gmm-selection-py)
### 2.1.1.3\. 估计算法期望最大化(EM)
......@@ -65,33 +65,33 @@
## 2.1.2\. 变分贝叶斯高斯混合
> [`BayesianGaussianMixture`](generated/sklearn.mixture.BayesianGaussianMixture.html#sklearn.mixture.BayesianGaussianMixture "sklearn.mixture.BayesianGaussianMixture") 对象实现了具有变分的高斯混合模型的变体推理算法。 这个API和 [`GaussianMixture`](generated/sklearn.mixture.GaussianMixture.html#sklearn.mixture.GaussianMixture "sklearn.mixture.GaussianMixture") 相似。
> [`BayesianGaussianMixture`](https://scikit-learn.org/stable/modules/generated/sklearn.mixture.BayesianGaussianMixture.html#sklearn.mixture.BayesianGaussianMixture "sklearn.mixture.BayesianGaussianMixture") 对象实现了具有变分的高斯混合模型的变体推理算法。 这个API和 [`GaussianMixture`](https://scikit-learn.org/stable/modules/generated/sklearn.mixture.GaussianMixture.html#sklearn.mixture.GaussianMixture "sklearn.mixture.GaussianMixture") 相似。
### 2.1.2.1\. 估计算法: 变分推断(variational inference)
变分推断是期望最大化(EM)的扩展,它最大化模型证据(包括先验)的下界,而不是数据似然函数。 变分方法的原理与期望最大化相同(二者都是迭代算法,在寻找由混合产生的每个点的概率和根据所分配的点拟合之间两步交替),但是变分方法通过整合先验分布信息来增加正则化限制。 这避免了期望最大化解决方案中常出现的奇异性,但是也对模型带来了微小的偏差。 变分方法计算过程通常明显较慢,但通常不会慢到无法使用。
由于它的贝叶斯特性,变分算法比预期最大化(EM)需要更多的超参数(即先验分布中的参数),其中最重要的就是 浓度参数 `weight_concentration_prior` 。指定一个低浓度先验,将会使模型将大部分的权重放在少数分量上, 其余分量的权重则趋近 0。而高浓度先验将使混合模型中的大部分分量都有一定的权重。 [`BayesianGaussianMixture`](generated/sklearn.mixture.BayesianGaussianMixture.html#sklearn.mixture.BayesianGaussianMixture "sklearn.mixture.BayesianGaussianMixture") 类的参数实现提出了两种权重分布先验: 一种是利用 Dirichlet distribution(狄利克雷分布)的有限混合模型,另一种是利用 Dirichlet Process(狄利克雷过程)的无限混合模型。 在实际应用上,狄利克雷过程推理算法是近似的,并且使用具有固定最大分量数的截尾分布(称之为 Stick-breaking representation)。使用的分量数实际上几乎总是取决于数据。
由于它的贝叶斯特性,变分算法比预期最大化(EM)需要更多的超参数(即先验分布中的参数),其中最重要的就是 浓度参数 `weight_concentration_prior` 。指定一个低浓度先验,将会使模型将大部分的权重放在少数分量上, 其余分量的权重则趋近 0。而高浓度先验将使混合模型中的大部分分量都有一定的权重。 [`BayesianGaussianMixture`](https://scikit-learn.org/stable/modules/generated/sklearn.mixture.BayesianGaussianMixture.html#sklearn.mixture.BayesianGaussianMixture "sklearn.mixture.BayesianGaussianMixture") 类的参数实现提出了两种权重分布先验: 一种是利用 Dirichlet distribution(狄利克雷分布)的有限混合模型,另一种是利用 Dirichlet Process(狄利克雷过程)的无限混合模型。 在实际应用上,狄利克雷过程推理算法是近似的,并且使用具有固定最大分量数的截尾分布(称之为 Stick-breaking representation)。使用的分量数实际上几乎总是取决于数据。
下图比较了不同类型的权重浓度先验(参数 `weight_concentration_prior_type` ) 不同的 `weight_concentration_prior` 的值获得的结果。 在这里,我们可以发现 `weight_concentration_prior` 参数的值对获得的有效的激活分量数(即权重较大的分量的数量)有很大影响。 我们也能注意到当先验是 ‘dirichlet_distribution’ 类型时,大的浓度权重先验会导致更均匀的权重,然而 ‘dirichlet_process’ 类型(默认类型)却不是这样。
**[![plot_bgmm](img/d7151d4911c077ded512eba1c6cd2bb0.jpg)](../auto_examples/mixture/plot_concentration_prior.html) [![plot_dpgmm](img/ed352c0fca166fdaa12d7da93676df50.jpg)](../auto_examples/mixture/plot_concentration_prior.html)**
**[![plot_bgmm](img/d7151d4911c077ded512eba1c6cd2bb0.jpg)](https://scikit-learn.org/stable/auto_examples/mixture/plot_concentration_prior.html) [![plot_dpgmm](img/ed352c0fca166fdaa12d7da93676df50.jpg)](https://scikit-learn.org/stable/auto_examples/mixture/plot_concentration_prior.html)**
下面的例子将具有固定数量分量的高斯混合模型与 Dirichlet process prior(狄利克雷过程先验)的变分高斯混合模型进行比较。 这里,典型高斯混合模型被指定由 2 个聚类组成的有 5 个分量的数据集。 我们可以看到,具有狄利克雷过程的变分高斯混合可以将自身限制在 2 个分量,而高斯混合必须按照用户先验设置的固定数量的分量来拟合数据。 在例子中,用户选择了 `n_components=5` ,这与真正的试用数据集(toy dataset)的生成分量不符。 稍微观察就能注意到,狄利克雷过程先验的变分高斯混合模型可以采取保守的立场,并且只适合一个分量。
[![http://sklearn.apachecn.org/cn/0.19.0/_images/sphx_glr_plot_gmm_0011.png](img/c009aed89f1926875e5637127af6000c.jpg)](../auto_examples/mixture/plot_gmm.html)
[![http://sklearn.apachecn.org/cn/0.19.0/_images/sphx_glr_plot_gmm_0011.png](img/c009aed89f1926875e5637127af6000c.jpg)](https://scikit-learn.org/stable/auto_examples/mixture/plot_gmm.html)
在下图中,我们将拟合一个并不能被高斯混合模型很好描述的数据集。 调整 [`BayesianGaussianMixture`](generated/sklearn.mixture.BayesianGaussianMixture.html#sklearn.mixture.BayesianGaussianMixture "sklearn.mixture.BayesianGaussianMixture") 类的参数 `weight_concentration_prior` ,这个参数决定了用来拟合数据的分量数。我们在最后两个图上展示了从两个混合结果产生的随机抽样。
在下图中,我们将拟合一个并不能被高斯混合模型很好描述的数据集。 调整 [`BayesianGaussianMixture`](https://scikit-learn.org/stable/modules/generated/sklearn.mixture.BayesianGaussianMixture.html#sklearn.mixture.BayesianGaussianMixture "sklearn.mixture.BayesianGaussianMixture") 类的参数 `weight_concentration_prior` ,这个参数决定了用来拟合数据的分量数。我们在最后两个图上展示了从两个混合结果产生的随机抽样。
[![http://sklearn.apachecn.org/cn/0.19.0/_images/sphx_glr_plot_gmm_sin_0011.png](img/d2b34f499ce1bbd28a276bc0d7afdde1.jpg)](../auto_examples/mixture/plot_gmm_sin.html)
[![http://sklearn.apachecn.org/cn/0.19.0/_images/sphx_glr_plot_gmm_sin_0011.png](img/d2b34f499ce1bbd28a276bc0d7afdde1.jpg)](https://scikit-learn.org/stable/auto_examples/mixture/plot_gmm_sin.html)
示例:
> * 一个用 [`GaussianMixture`](generated/sklearn.mixture.GaussianMixture.html#sklearn.mixture.GaussianMixture "sklearn.mixture.GaussianMixture") 和 [`BayesianGaussianMixture`](generated/sklearn.mixture.BayesianGaussianMixture.html#sklearn.mixture.BayesianGaussianMixture "sklearn.mixture.BayesianGaussianMixture") 绘制置信椭圆体的例子, 请查阅 [Gaussian Mixture Model Ellipsoids](../auto_examples/mixture/plot_gmm.html#sphx-glr-auto-examples-mixture-plot-gmm-py)
> * [Gaussian Mixture Model Sine Curve](../auto_examples/mixture/plot_gmm_sin.html#sphx-glr-auto-examples-mixture-plot-gmm-sin-py) 这个例子展示了用 [`GaussianMixture`](generated/sklearn.mixture.GaussianMixture.html#sklearn.mixture.GaussianMixture "sklearn.mixture.GaussianMixture") 和 [`BayesianGaussianMixture`](generated/sklearn.mixture.BayesianGaussianMixture.html#sklearn.mixture.BayesianGaussianMixture "sklearn.mixture.BayesianGaussianMixture") 来拟合正弦波。
> * 一个使用不同的 `weight_concentration_prior_type` 用以不同的 `weight_concentration_prior` 参数值的:class:<cite>BayesianGaussianMixture</cite> 来绘制置信椭圆体的例子。 请查阅 [Concentration Prior Type Analysis of Variation Bayesian Gaussian Mixture](../auto_examples/mixture/plot_concentration_prior.html#sphx-glr-auto-examples-mixture-plot-concentration-prior-py)
> * 一个用 [`GaussianMixture`](https://scikit-learn.org/stable/modules/generated/sklearn.mixture.GaussianMixture.html#sklearn.mixture.GaussianMixture "sklearn.mixture.GaussianMixture") 和 [`BayesianGaussianMixture`](https://scikit-learn.org/stable/modules/generated/sklearn.mixture.BayesianGaussianMixture.html#sklearn.mixture.BayesianGaussianMixture "sklearn.mixture.BayesianGaussianMixture") 绘制置信椭圆体的例子, 请查阅 [Gaussian Mixture Model Ellipsoids](https://scikit-learn.org/stable/auto_examples/mixture/plot_gmm.html#sphx-glr-auto-examples-mixture-plot-gmm-py)
> * [Gaussian Mixture Model Sine Curve](https://scikit-learn.org/stable/auto_examples/mixture/plot_gmm_sin.html#sphx-glr-auto-examples-mixture-plot-gmm-sin-py) 这个例子展示了用 [`GaussianMixture`](https://scikit-learn.org/stable/modules/generated/sklearn.mixture.GaussianMixture.html#sklearn.mixture.GaussianMixture "sklearn.mixture.GaussianMixture") 和 [`BayesianGaussianMixture`](https://scikit-learn.org/stable/modules/generated/sklearn.mixture.BayesianGaussianMixture.html#sklearn.mixture.BayesianGaussianMixture "sklearn.mixture.BayesianGaussianMixture") 来拟合正弦波。
> * 一个使用不同的 `weight_concentration_prior_type` 用以不同的 `weight_concentration_prior` 参数值的:class:*BayesianGaussianMixture* 来绘制置信椭圆体的例子。 请查阅 [Concentration Prior Type Analysis of Variation Bayesian Gaussian Mixture](https://scikit-learn.org/stable/auto_examples/mixture/plot_concentration_prior.html#sphx-glr-auto-examples-mixture-plot-concentration-prior-py)
[`BayesianGaussianMixture`](generated/sklearn.mixture.BayesianGaussianMixture.html#sklearn.mixture.BayesianGaussianMixture "sklearn.mixture.BayesianGaussianMixture") 下的变分推理的优缺点
[`BayesianGaussianMixture`](https://scikit-learn.org/stable/modules/generated/sklearn.mixture.BayesianGaussianMixture.html#sklearn.mixture.BayesianGaussianMixture "sklearn.mixture.BayesianGaussianMixture") 下的变分推理的优缺点
* * *
......
此差异已折叠。
此差异已折叠。
......@@ -34,11 +34,11 @@ array([[ 1, 2],
如果每一行和每一列同属于一种 bicluster ,就重新排列数据矩阵的行和列,会使得 bicluster 呈现对角线。 下面是一个例子,此结构的biclusters 具有比其他行列更高的平均值:
[![http://sklearn.apachecn.org/cn/0.19.0/_images/sphx_glr_plot_spectral_coclustering_0031.png](img/9812effbd6ddac1053fd0b63ebe8c2fb.jpg)](../auto_examples/bicluster/images/sphx_glr_plot_spectral_coclustering_003.png)
[![http://sklearn.apachecn.org/cn/0.19.0/_images/sphx_glr_plot_spectral_coclustering_0031.png](img/9812effbd6ddac1053fd0b63ebe8c2fb.jpg)](https://scikit-learn.org/stable/auto_examples/bicluster/images/sphx_glr_plot_spectral_coclustering_003.png)
在棋盘结构的例子中, 每一行属于所有的列类别, 每一列属于所有的行类别。 下面是一个例子,每个 bicluster 中的值差异较小:
[![http://sklearn.apachecn.org/cn/0.19.0/_images/sphx_glr_plot_spectral_biclustering_0031.png](img/4e0d8935ff82f26fc3a46a3202bd1fa3.jpg)](../auto_examples/bicluster/images/sphx_glr_plot_spectral_biclustering_003.png)
[![http://sklearn.apachecn.org/cn/0.19.0/_images/sphx_glr_plot_spectral_biclustering_0031.png](img/4e0d8935ff82f26fc3a46a3202bd1fa3.jpg)](https://scikit-learn.org/stable/auto_examples/bicluster/images/sphx_glr_plot_spectral_biclustering_003.png)
在拟合模型之后, 可以在 `rows_``columns_` 属性中找到行列 cluster membership 。 `rows_[i]` 是一个二进制的向量, 就是属于 bicluster `i` 的一行。 同样的, `columns_[i]` 就表示属于 bicluster `i` 的列。
......@@ -50,7 +50,7 @@ Biclustering 在不同的领域有很多其他名称,包括 co-clustering, two
## 2.4.1\. Spectral Co-Clustering
> [`SpectralCoclustering`](generated/sklearn.cluster.bicluster.SpectralCoclustering.html#sklearn.cluster.bicluster.SpectralCoclustering "sklearn.cluster.bicluster.SpectralCoclustering") 算法找到的 bicluster 的值比相应的其他行和列更高。
> [`SpectralCoclustering`](https://scikit-learn.org/stable/modules/generated/sklearn.cluster.bicluster.SpectralCoclustering.html#sklearn.cluster.bicluster.SpectralCoclustering "sklearn.cluster.bicluster.SpectralCoclustering") 算法找到的 bicluster 的值比相应的其他行和列更高。
每一个行和列都只属于一个 bicluster, 所以重新分配行和列,使得分区连续显示对角线上的 high value:
......@@ -71,7 +71,7 @@ Note
奇异值分解, ![A_n = U \Sigma V^\top](img/93074566222e67121a8ab55e90d8e1af.jpg) , 提供了 ![A](img/eeaf066f8cca5064b706ccfc4728323d.jpg) 行列的分区. 左边的奇异值向量给予行分区,右边的奇异值向量给予列分区。
![\ell = \lceil \log_2 k \rceil](img/5e45807b4775fcfaca64f6363102dc5e.jpg) 奇异值向量从第二个开始, 提供所需的分区信息。 这些用于形成矩阵 :<cite>Z</cite>:
![\ell = \lceil \log_2 k \rceil](img/5e45807b4775fcfaca64f6363102dc5e.jpg) 奇异值向量从第二个开始, 提供所需的分区信息。 这些用于形成矩阵 :*Z*:
![Z = \begin{bmatrix} R^{-1/2} U \\\\
......@@ -85,8 +85,8 @@ Note
例子:
* [A demo of the Spectral Co-Clustering algorithm](../auto_examples/bicluster/plot_spectral_coclustering.html#sphx-glr-auto-examples-bicluster-plot-spectral-coclustering-py): 如何用 bicluster 数据矩阵并应用。
* [Biclustering documents with the Spectral Co-clustering algorithm](../auto_examples/bicluster/plot_bicluster_newsgroups.html#sphx-glr-auto-examples-bicluster-plot-bicluster-newsgroups-py):一个在 20 个新闻组数据集中发现 biclusters 的例子
* [A demo of the Spectral Co-Clustering algorithm](https://scikit-learn.org/stable/auto_examples/bicluster/plot_spectral_coclustering.html#sphx-glr-auto-examples-bicluster-plot-spectral-coclustering-py): 如何用 bicluster 数据矩阵并应用。
* [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):一个在 20 个新闻组数据集中发现 biclusters 的例子
参考文献:
......@@ -94,7 +94,7 @@ Note
## 2.4.2\. Spectral Biclustering
> [`SpectralBiclustering`](generated/sklearn.cluster.bicluster.SpectralBiclustering.html#sklearn.cluster.bicluster.SpectralBiclustering "sklearn.cluster.bicluster.SpectralBiclustering") 算法假设输入的数据矩阵具有隐藏的棋盘结构。 具有这种结构的矩阵的行列 可能被分区,使得在笛卡尔积中的 大部分 biclusters 的 row clusters 和 column cluster 是近似恒定的。
> [`SpectralBiclustering`](https://scikit-learn.org/stable/modules/generated/sklearn.cluster.bicluster.SpectralBiclustering.html#sklearn.cluster.bicluster.SpectralBiclustering "sklearn.cluster.bicluster.SpectralBiclustering") 算法假设输入的数据矩阵具有隐藏的棋盘结构。 具有这种结构的矩阵的行列 可能被分区,使得在笛卡尔积中的 大部分 biclusters 的 row clusters 和 column cluster 是近似恒定的。
例如,如果有两个row 分区和三个列分区,每一行属于三个 bicluster ,每一列属于两个 bicluster。
......@@ -128,7 +128,7 @@ j}} + \overline{L_{\cdot \cdot}}](img/d670eea3215462f64d74d9366622a490.jpg)
示例:
* [A demo of the Spectral Biclustering algorithm](../auto_examples/bicluster/plot_spectral_biclustering.html#sphx-glr-auto-examples-bicluster-plot-spectral-biclustering-py): 一个简单的例子 显示如何生成棋盘矩阵和 bicluster
* [A demo of the Spectral Biclustering algorithm](https://scikit-learn.org/stable/auto_examples/bicluster/plot_spectral_biclustering.html#sphx-glr-auto-examples-bicluster-plot-spectral-biclustering-py): 一个简单的例子 显示如何生成棋盘矩阵和 bicluster
.
......@@ -152,7 +152,7 @@ j}} + \overline{L_{\cdot \cdot}}](img/d670eea3215462f64d74d9366622a490.jpg)
Jaccard 索引 达到最小值0,当 biclusters 不重叠的时候,并且当他们相同干的时候,最大值为1。
有些方法已经开发出来,用来比较两个 biclusters 的数据集。 从现在开始 之后 [`consensus_score`](generated/sklearn.metrics.consensus_score.html#sklearn.metrics.consensus_score "sklearn.metrics.consensus_score") (Hochreiter et. al., 2010) 是可以用:
有些方法已经开发出来,用来比较两个 biclusters 的数据集。 从现在开始 之后 [`consensus_score`](https://scikit-learn.org/stable/modules/generated/sklearn.metrics.consensus_score.html#sklearn.metrics.consensus_score "sklearn.metrics.consensus_score") (Hochreiter et. al., 2010) 是可以用:
1. 使用 Jaccard 索引或类似措施,计算 biclusters 的 bicluster 相似性。
2. 以一对一的方式将 bicluster 分从一组分配给另一组,以最大化其相似性的总和。该步骤使用匈牙利算法执行。
......
此差异已折叠。
......@@ -6,6 +6,139 @@
翻译者:
        [@柠檬](https://github.com/apachecn/scikit-learn-doc-zh)
许多统计问题在某一时刻需要估计一个总体的协方差矩阵,这可以看作是对数据集散点图形状的估计。 大多数情况下,基于样本的估计(基于其属性,如尺寸,结构,均匀性), 对估计质量有很大影响。 <cite>sklearn.covariance</cite> 方法的目的是 提供一个能在各种设置下准确估计总体协方差矩阵的工具。
许多统计问题在某一时刻需要估计一个总体的协方差矩阵,这可以看作是对数据集散点图形状的估计。 大多数情况下,基于样本的估计(基于其属性,如尺寸,结构,均匀性), 对估计质量有很大影响。 *sklearn.covariance* 方法的目的是 提供一个能在各种设置下准确估计总体协方差矩阵的工具。
我们假设观察是独立的,相同分布的 (i.i.d.)。
\ No newline at end of file
我们假设观察是独立的,相同分布的 (i.i.d.)。
# 2.6.1\. 经验协方差
已知数据集的协方差矩阵与经典 _maximum likelihood estimator(最大似然估计)_ (或 “经验协方差”) 很好地近似,条件是与特征数量(描述观测值的变量)相比,观测数量足够大。 更准确地说,样本的最大似然估计是相应的总体协方差矩阵的无偏估计。
样本的经验协方差矩阵可以使用 [`empirical_covariance`](https://scikit-learn.org/stable/modules/generated/sklearn.covariance.empirical_covariance.html#sklearn.covariance.empirical_covariance "sklearn.covariance.empirical_covariance") 包的函数计算 , 或者通过 [`EmpiricalCovariance`](https://scikit-learn.org/stable/modules/generated/sklearn.covariance.EmpiricalCovariance.html#sklearn.covariance.EmpiricalCovariance "sklearn.covariance.EmpiricalCovariance") 使用 [`EmpiricalCovariance.fit`](https://scikit-learn.org/stable/modules/generated/sklearn.covariance.EmpiricalCovariance.html#sklearn.covariance.EmpiricalCovariance.fit "sklearn.covariance.EmpiricalCovariance.fit") 方法将对象与数据样本拟合 。 要注意,取决于数据是否居中,结果会有所不同,所以可能需要准确使用参数 `assume_centered`。 如果使用 `assume_centered=False` ,则结果更准确。且测试集应该具有与训练集相同的均值向量。 如果不是这样,两者都应该使用中心值, `assume_centered=True` 应该使用。
例子:
* See [Shrinkage covariance estimation: LedoitWolf vs OAS and max-likelihood](https://scikit-learn.org/stable/auto_examples/covariance/plot_covariance_estimation.html#sphx-glr-auto-examples-covariance-plot-covariance-estimation-py) for an example on how to fit an [`EmpiricalCovariance`](https://scikit-learn.org/stable/modules/generated/sklearn.covariance.EmpiricalCovariance.html#sklearn.covariance.EmpiricalCovariance "sklearn.covariance.EmpiricalCovariance") object to data.
# 2.6.2\. 收敛协方差
## 2.6.2.1\. 基本收敛
尽管是协方差矩阵的无偏估计, 最大似然估计不是协方差矩阵的特征值的一个很好的估计, 所以从反演得到的精度矩阵是不准确的。 有时,甚至出现数学原因,经验协方差矩阵不能反转。 为了避免这样的反演问题,引入了经验协方差矩阵的一种变换方式:`shrinkage`
在 scikit-learn 中,该变换(具有用户定义的收缩系数) 可以直接应用于使用 [`shrunk_covariance`](https://scikit-learn.org/stable/modules/generated/sklearn.covariance.shrunk_covariance.html#sklearn.covariance.shrunk_covariance "sklearn.covariance.shrunk_covariance") 方法预先计算协方差。 此外,协方差的收缩估计可以用 [`ShrunkCovariance`](https://scikit-learn.org/stable/modules/generated/sklearn.covariance.ShrunkCovariance.html#sklearn.covariance.ShrunkCovariance "sklearn.covariance.ShrunkCovariance") 对象 及其 [`ShrunkCovariance.fit`](https://scikit-learn.org/stable/modules/generated/sklearn.covariance.ShrunkCovariance.html#sklearn.covariance.ShrunkCovariance.fit "sklearn.covariance.ShrunkCovariance.fit") 方法拟合到数据中。 再次,根据数据是否居中,结果会不同,所以可能要准确使用参数 `assume_centered`
在数学上,这种收缩在于减少经验协方差矩阵的最小和最大特征值之间的比率。 可以通过简单地根据给定的偏移量移动每个特征值来完成, 这相当于找到协方差矩阵的l2惩罚的最大似然估计器(l2-penalized Maximum Likelihood Estimator)。在实践中,收缩归结为简单的凸变换: ![\Sigma_{\rm
shrunk} = (1-\alpha)\hat{\Sigma} + \alpha\frac{{\rm
Tr}\hat{\Sigma}}{p}\rm Id](img/8f62a01deaf67a7f9960d853abd9dcca.jpg).
选择收缩量, ![\alpha](img/d8b3d5242d513369a44f8bf0c6112744.jpg) 相当于设置偏差/方差权衡,下面将讨论。
示例:
* See [Shrinkage covariance estimation: LedoitWolf vs OAS and max-likelihood](https://scikit-learn.org/stable/auto_examples/covariance/plot_covariance_estimation.html#sphx-glr-auto-examples-covariance-plot-covariance-estimation-py) for an example on how to fit a [`ShrunkCovariance`](https://scikit-learn.org/stable/modules/generated/sklearn.covariance.ShrunkCovariance.html#sklearn.covariance.ShrunkCovariance "sklearn.covariance.ShrunkCovariance") object to data.
## 2.6.2.2\. Ledoit-Wolf 收敛
在他们的 2004 年的论文 [[1]](#id6) 中, O.Ledoit 和 M.Wolf 提出了一个公式, 用来计算优化的收敛系数 ![\alpha](img/d8b3d5242d513369a44f8bf0c6112744.jpg) , 它使得估计协方差和实际协方差矩阵之间的均方差进行最小化。
*sklearn.covariance* 包中,可以使用 [`ledoit_wolf`](https://scikit-learn.org/stable/modules/generated/sklearn.covariance.ledoit_wolf.html#sklearn.covariance.ledoit_wolf "sklearn.covariance.ledoit_wolf") 函数来计算样本的 基于 Ledoit-Wolf estimator 的协方差, 或者可以针对同样的样本 通过拟合 [`LedoitWolf`](https://scikit-learn.org/stable/modules/generated/sklearn.covariance.LedoitWolf.html#sklearn.covariance.LedoitWolf "sklearn.covariance.LedoitWolf") 对象来获得。
例子:
* See [Shrinkage covariance estimation: LedoitWolf vs OAS and max-likelihood](https://scikit-learn.org/stable/auto_examples/covariance/plot_covariance_estimation.html#sphx-glr-auto-examples-covariance-plot-covariance-estimation-py) 关于如何将 [`LedoitWolf`](https://scikit-learn.org/stable/modules/generated/sklearn.covariance.LedoitWolf.html#sklearn.covariance.LedoitWolf "sklearn.covariance.LedoitWolf") 对象与数据拟合, 并将 Ledoit-Wolf 估计器的性能进行可视化的示例。
参考文献:
| [[1]](#id5) | O. Ledoit and M. Wolf, “A Well-Conditioned Estimator for Large-Dimensional Covariance Matrices”, Journal of Multivariate Analysis, Volume 88, Issue 2, February 2004, pages 365-411. |
## 2.6.2.3\. Oracle 近似收缩
在数据为高斯分布的假设下,Chen et al. 等 [[2]](#id8) 推导出了一个公式,旨在 产生比 Ledoit 和 Wolf 公式具有更小均方差的收敛系数。 所得到的估计器被称为协方差的 Oracle 收缩近似估计器。
*sklearn.covariance* 包中, OAS 估计的协方差可以使用函数 [`oas`](https://scikit-learn.org/stable/modules/generated/sklearn.covariance.oas.html#sklearn.covariance.oas "sklearn.covariance.oas") 对样本进行计算,或者可以通过将 [`OAS`](https://scikit-learn.org/stable/modules/generated/sklearn.covariance.OAS.html#sklearn.covariance.OAS "sklearn.covariance.OAS") 对象拟合到相同的样本来获得。
[![http://sklearn.apachecn.org/cn/0.19.0/_images/sphx_glr_plot_covariance_estimation_0011.png](img/7f37b8cd4340f3d0981f518f14670c26.jpg)](https://scikit-learn.org/stable/auto_examples/covariance/plot_covariance_estimation.html)
设定收缩时的偏差方差权衡:比较 Ledoit-Wolf 和 OAS 估计量的选择
参考文献:
| [[2]](#id7) | Chen et al., “Shrinkage Algorithms for MMSE Covariance Estimation”, IEEE Trans. on Sign. Proc., Volume 58, Issue 10, October 2010. |
示例:
* See [Shrinkage covariance estimation: LedoitWolf vs OAS and max-likelihood](https://scikit-learn.org/stable/auto_examples/covariance/plot_covariance_estimation.html#sphx-glr-auto-examples-covariance-plot-covariance-estimation-py) for an example on how to fit an [`OAS`](https://scikit-learn.org/stable/modules/generated/sklearn.covariance.OAS.html#sklearn.covariance.OAS "sklearn.covariance.OAS") object to data.
* See [Ledoit-Wolf vs OAS estimation](https://scikit-learn.org/stable/auto_examples/covariance/plot_lw_vs_oas.html#sphx-glr-auto-examples-covariance-plot-lw-vs-oas-py) to visualize the Mean Squared Error difference between a [`LedoitWolf`](https://scikit-learn.org/stable/modules/generated/sklearn.covariance.LedoitWolf.html#sklearn.covariance.LedoitWolf "sklearn.covariance.LedoitWolf") and an [`OAS`](https://scikit-learn.org/stable/modules/generated/sklearn.covariance.OAS.html#sklearn.covariance.OAS "sklearn.covariance.OAS") estimator of the covariance.
[![http://sklearn.apachecn.org/cn/0.19.0/_images/sphx_glr_plot_lw_vs_oas_0011.png](img/bc35316fd422689a9bc65845d63c1433.jpg)](https://scikit-learn.org/stable/auto_examples/covariance/plot_lw_vs_oas.html)
# 2.6.3\. 稀疏逆协方差
协方差矩阵的逆矩阵,通常称为精度矩阵(precision matrix),它与部分相关矩阵(partial correlation matrix)成正比。 它给出部分独立性关系。换句话说,如果两个特征在其他特征上有条件地独立, 则精度矩阵中的对应系数将为零。这就是为什么估计一个稀疏精度矩阵是有道理的: 通过从数据中学习独立关系,协方差矩阵的估计能更好处理。这被称为协方差选择。
在小样本的情况,即 `n_samples` 是数量级 `n_features` 或更小, 稀疏的逆协方差估计往往比收敛的协方差估计更好。 然而,在相反的情况下,或者对于非常相关的数据,它们可能在数值上不稳定。 此外,与收敛估算不同,稀疏估计器能够恢复非对角线结构 (off-diagonal structure)。
[`GraphLasso`](https://scikit-learn.org/stable/modules/generated/sklearn.covariance.GraphLasso.html#sklearn.covariance.GraphLasso "sklearn.covariance.GraphLasso") 估计器使用 L1 惩罚执行关于精度矩阵的稀疏性: `alpha` 参数越高,精度矩阵的稀疏性越大。 相应的 [`GraphLassoCV`](https://scikit-learn.org/stable/modules/generated/sklearn.covariance.GraphLassoCV.html#sklearn.covariance.GraphLassoCV "sklearn.covariance.GraphLassoCV") 对象使用交叉验证来自动设置 `alpha` 参数。
[![http://sklearn.apachecn.org/cn/0.19.0/_images/sphx_glr_plot_sparse_cov_0011.png](img/7c9ce57d4e2ba5270352f4e118af7fac.jpg)](https://scikit-learn.org/stable/auto_examples/covariance/plot_sparse_cov.html)
Note
**结构恢复**
从数据中的相关性恢复图形结构是一个具有挑战性的事情。如果您对这种恢复感兴趣,请记住:
* 相关矩阵的恢复比协方差矩阵更容易:在运行 [`GraphLasso`](https://scikit-learn.org/stable/modules/generated/sklearn.covariance.GraphLasso.html#sklearn.covariance.GraphLasso "sklearn.covariance.GraphLasso") 前先标准化观察值
* 如果底层图具有比平均节点更多的连接节点,则算法将错过其中一些连接。
* 如果您的观察次数与底层图形中的边数相比不大,则不会恢复。
* 即使您具有良好的恢复条件,通过交叉验证(例如使用GraphLassoCV对象)选择的 Alpha 参数将导致选择太多边。 然而,相关边缘将具有比不相关边缘更重的权重。
数学公式如下:
![\hat{K} = \mathrm{argmin}_K \big(\mathrm{tr} S K - \mathrm{log} \mathrm{det} K+ \alpha \|K\|_1\big)](img/43996aff9311511e6e2f81912a249c7e.jpg)
其中:![K](img/e279b8169ddd6581c5606c868ba52fae.jpg) 是要估计的精度矩阵(precision matrix), ![S](img/12ecd862769bee1e71c75c134b6423bb.jpg) 是样本的协方差矩阵。 ![\|K\|_1](img/6122e23454910f4f076c71a84c068291.jpg) 是非对角系数 ![K](img/e279b8169ddd6581c5606c868ba52fae.jpg) (off-diagonal coefficients)的绝对值之和。 用于解决这个问题的算法是来自 Friedman 2008 Biostatistics 论文的 GLasso 算法。 它与 R 语言 `glasso` 包中的算法相同。
例子:
* [Sparse inverse covariance estimation](https://scikit-learn.org/stable/auto_examples/covariance/plot_sparse_cov.html#sphx-glr-auto-examples-covariance-plot-sparse-cov-py):
合成数据示例,显示结构的一些恢复,并与其他协方差估计器进行比较。
* [Visualizing the stock market structure](https://scikit-learn.org/stable/auto_examples/applications/plot_stock_market.html#sphx-glr-auto-examples-applications-plot-stock-market-py): 真实股票市场数据示例,查找哪些信号最相关。
参考文献:
* Friedman et al, [“Sparse inverse covariance estimation with the graphical lasso”](http://biostatistics.oxfordjournals.org/content/9/3/432.short), Biostatistics 9, pp 432, 2008
# 2.6.4\. Robust 协方差估计
实际数据集通常是会有测量或记录错误。合格但不常见的观察也可能出于各种原因。 每个不常见的观察称为异常值。 上面提出的经验协方差估计器和收缩协方差估计器对数据中异常观察值非常敏感。 因此,应该使用更好的协方差估计(robust covariance estimators)来估算其真实数据集的协方差。 或者,可以使用更好的协方差估计器(robust covariance estimators)来执行异常值检测, 并根据数据的进一步处理,丢弃/降低某些观察值。
`sklearn.covariance` 包实现了 robust estimator of covariance, 即 Minimum Covariance Determinant [[3]](#id14)
## 2.6.4.1\. 最小协方差决定
最小协方差决定(Minimum Covariance Determinant)估计器是 由 P.J. Rousseeuw 在 [[3]](#id14) 中引入的数据集协方差的鲁棒估计 (robust estimator)。 这个想法是找出一个给定比例(h)的 “好” 观察值,它们不是离群值, 且可以计算其经验协方差矩阵。 然后将该经验协方差矩阵重新缩放以补偿所执行的观察选择(”consistency step(一致性步骤)”)。 计算最小协方差决定估计器后,可以根据其马氏距离(Mahalanobis distance)给出观测值的权重, 这导致数据集的协方差矩阵的重新加权估计(”reweighting step(重新加权步骤)”)。
Rousseeuw 和 Van Driessen [[4]](#id15) 开发了 FastMCD 算法,以计算最小协方差决定因子(Minimum Covariance Determinant)。 在 scikit-learn 中,该算法在将 MCD 对象拟合到数据时应用。FastMCD 算法同时计算数据集位置的鲁棒估计。
Raw估计可通过 [`MinCovDet`](https://scikit-learn.org/stable/modules/generated/sklearn.covariance.MinCovDet.html#sklearn.covariance.MinCovDet "sklearn.covariance.MinCovDet") 对象的 `raw_location_``raw_covariance_` 属性获得。
参考文献:
| [3] | _([1](#id10), [2](#id12))_ P. J. Rousseeuw. Least median of squares regression. J. Am Stat Ass, 79:871, 1984. |
| [[4]](#id13) | A Fast Algorithm for the Minimum Covariance Determinant Estimator, 1999, American Statistical Association and the American Society for Quality, TECHNOMETRICS. |
例子:
* See [Robust vs Empirical covariance estimate](https://scikit-learn.org/stable/auto_examples/covariance/plot_robust_vs_empirical_covariance.html#sphx-glr-auto-examples-covariance-plot-robust-vs-empirical-covariance-py) 关于如何将对象 [`MinCovDet`](https://scikit-learn.org/stable/modules/generated/sklearn.covariance.MinCovDet.html#sklearn.covariance.MinCovDet "sklearn.covariance.MinCovDet") 与数据拟合的示例, 尽管存在异常值,但估计结果仍然比较准确。
* See [Robust covariance estimation and Mahalanobis distances relevance](https://scikit-learn.org/stable/auto_examples/covariance/plot_mahalanobis_distances.html#sphx-glr-auto-examples-covariance-plot-mahalanobis-distances-py) 马氏距离(Mahalanobis distance),针对协方差估计器 [`EmpiricalCovariance`](https://scikit-learn.org/stable/modules/generated/sklearn.covariance.EmpiricalCovariance.html#sklearn.covariance.EmpiricalCovariance "sklearn.covariance.EmpiricalCovariance")[`MinCovDet`](https://scikit-learn.org/stable/modules/generated/sklearn.covariance.MinCovDet.html#sklearn.covariance.MinCovDet "sklearn.covariance.MinCovDet") 之间的差异进行可视化。(所以我们得到了精度矩阵的更好估计)
| Influence of outliers on location and covariance estimates | Separating inliers from outliers using a Mahalanobis distance |
| --- | --- |
| [![robust_vs_emp](img/e1b4d08b93d1d222e96d99475a766281.jpg)](https://scikit-learn.org/stable/auto_examples/covariance/plot_robust_vs_empirical_covariance.html) | [![mahalanobis](img/89807b9fa9f5924b027486f280324b1a.jpg)](https://scikit-learn.org/stable/auto_examples/covariance/plot_mahalanobis_distances.html) |
# 2.7\. 经验协方差
# 2.11\. 新奇和异常值检测
已知数据集的协方差矩阵与经典 _maximum likelihood estimator(最大似然估计)_ (或 “经验协方差”) 很好地近似,条件是与特征数量(描述观测值的变量)相比,观测数量足够大。 更准确地说,样本的最大似然估计是相应的总体协方差矩阵的无偏估计。
校验者:
        [@RyanZhiNie](https://github.com/RyanZhiNie)
        [@羊三](https://github.com/apachecn/scikit-learn-doc-zh)
翻译者:
        [@羊三](https://github.com/apachecn/scikit-learn-doc-zh)
样本的经验协方差矩阵可以使用 [`empirical_covariance`](generated/sklearn.covariance.empirical_covariance.html#sklearn.covariance.empirical_covariance "sklearn.covariance.empirical_covariance") 包的函数计算 , 或者通过 [`EmpiricalCovariance`](generated/sklearn.covariance.EmpiricalCovariance.html#sklearn.covariance.EmpiricalCovariance "sklearn.covariance.EmpiricalCovariance") 使用 [`EmpiricalCovariance.fit`](generated/sklearn.covariance.EmpiricalCovariance.html#sklearn.covariance.EmpiricalCovariance.fit "sklearn.covariance.EmpiricalCovariance.fit") 方法将对象与数据样本拟合 。 要注意,取决于数据是否居中,结果会有所不同,所以可能需要准确使用参数 `assume_centered`。 如果使用 `assume_centered=False` ,则结果更准确。且测试集应该具有与训练集相同的均值向量。 如果不是这样,两者都应该使用中心值, `assume_centered=True` 应该使用。
许多应用需要能够对新观测进行判断,判断其是否与现有观测服从同一分布(即新观测为内围值),相反则被认为不服从同一分布(即新观测为异常值)。 通常,这种能力被用于清理实际的数据集。必须做出两种重要区分:
| 新奇检测: | 训练数据未被异常值污染,我们对于新观测中的异常情况有兴趣检测。 |
| --- | --- |
| 异常值检测: | 训练数据包含异常值,我们需要拟合出训练数据的中心模式,以忽略有偏差的观测。 |
| --- | --- |
scikit-learn项目提供了一套可用于新奇或异常值检测的机器学习工具。 该策略是以无监督的方式学习数据中的对象来实现的:
```py
estimator.fit(X_train)
```
然后可以使用 *predict* 方法将新观测归为内围值或异常值:
```py
estimator.predict(X_test)
```
内围值被标记为1,而异常值被标记为-1。
## 2.11.1\. Novelty Detection(新奇检测)
考虑一个来自同一分布的数据集,以 ![p](img/e2f9b08680b30cfb80102f69264fdd5c.jpg) 个特征描述、有 ![n](img/c87d9110f3d32ffa5fa08671e4af11fb.jpg) 个观测。 现在考虑我们再往该数据集中添加一个观测。 如果新观测与原有观测有很大差异,我们就可以怀疑它是否是内围值吗? (即是否来自同一分布?)或者相反,如果新观测与原有观测很相似,我们就无法将其与原有观测区分开吗? 这就是新奇检测工具和方法所解决的问题。
一般来说,它将要学习出一个粗略且紧密的边界,界定出初始观测分布的轮廓,绘制在相互嵌入的 ![p](img/e2f9b08680b30cfb80102f69264fdd5c.jpg) 维空间中。 那么,如果后续的观测在边界划分的子空间内,则它们被认为来自与初始观测相同的总体。 否则,如果它们在边界之外,我们可以说就我们评估中给定的置信度而言,它们是异常值。
One-Class SVM(一类支持向量机)已经由 Schölkopf 等人采用以实现新奇检测,并在 [支持向量机](svm.html#svm) 模块的 [`svm.OneClassSVM`](https://scikit-learn.org/stable/modules/generated/sklearn.svm.OneClassSVM.html#sklearn.svm.OneClassSVM "sklearn.svm.OneClassSVM") 对象中实现。 需要选择 kernel 和 scalar 参数来定义边界。 通常选择 RBF kernel,即使没有确切的公式或算法来设置其带宽参数。 这是 scikit-learn 实现中的默认值。 ![\nu](img/f996477bc9806499e6b6a1ea4d9ae8eb.jpg) 参数,也称为一类支持向量机的边沿,对应于在边界之外找到新的但内围的观测的概率。
参考文献:
* [Estimating the support of a high-dimensional distribution](http://dl.acm.org/citation.cfm?id=1119749) Schölkopf, Bernhard, et al. Neural computation 13.7 (2001): 1443-1471.
例子:
* 参见 [One-class SVM with non-linear kernel (RBF)](https://scikit-learn.org/stable/auto_examples/svm/plot_oneclass.html#sphx-glr-auto-examples-svm-plot-oneclass-py) ,通过 [`svm.OneClassSVM`](https://scikit-learn.org/stable/modules/generated/sklearn.svm.OneClassSVM.html#sklearn.svm.OneClassSVM "sklearn.svm.OneClassSVM") 对象学习一些数据来将边界可视化。
[![http://sklearn.apachecn.org/cn/0.19.0/_images/sphx_glr_plot_oneclass_0011.png](img/b29b59eca5c581c3f54d92c1671f2b2e.jpg)](https://scikit-learn.org/stable/auto_examples/svm/plot_oneclass.html)
## 2.11.2\. Outlier Detection(异常值检测)
异常值检测类似于新奇检测,其目的是将内围观测的中心与一些被称为 “异常值” 的污染数据进行分离。 然而,在异常值检测的情况下,我们没有干净且适用于训练任何工具的数据集来代表内围观测的总体。
### 2.11.2.1\. Fitting an elliptic envelope(椭圆模型拟合)
实现异常值检测的一种常见方式是假设内围数据来自已知分布(例如,数据服从高斯分布)。 从这个假设来看,我们通常试图定义数据的 “形状”,并且可以将异常观测定义为足够远离拟合形状的观测。
scikit-learn 提供了 [`covariance.EllipticEnvelope`](https://scikit-learn.org/stable/modules/generated/sklearn.covariance.EllipticEnvelope.html#sklearn.covariance.EllipticEnvelope "sklearn.covariance.EllipticEnvelope") 对象,它能拟合出数据的稳健协方差估计,从而为中心数据点拟合出一个椭圆,忽略中心模式之外的点。
例如,假设内围数据服从高斯分布,它将稳健地(即不受异常值的影响)估计内围位置和协方差。 从该估计得到的马氏距离用于得出异常度量。 该策略如下图所示。
[![http://sklearn.apachecn.org/cn/0.19.0/_images/sphx_glr_plot_mahalanobis_distances_0011.png](img/89807b9fa9f5924b027486f280324b1a.jpg)](https://scikit-learn.org/stable/auto_examples/covariance/plot_mahalanobis_distances.html)
示例:
* 参见 [Robust covariance estimation and Mahalanobis distances relevance](https://scikit-learn.org/stable/auto_examples/covariance/plot_mahalanobis_distances.html#sphx-glr-auto-examples-covariance-plot-mahalanobis-distances-py) 说明对位置和协方差使用标准估计 ([`covariance.EmpiricalCovariance`](https://scikit-learn.org/stable/modules/generated/sklearn.covariance.EmpiricalCovariance.html#sklearn.covariance.EmpiricalCovariance "sklearn.covariance.EmpiricalCovariance")) 或稳健估计 ([`covariance.MinCovDet`](https://scikit-learn.org/stable/modules/generated/sklearn.covariance.MinCovDet.html#sklearn.covariance.MinCovDet "sklearn.covariance.MinCovDet")) 来评估观测的异常程度的差异。
参考文献:
* Rousseeuw, P.J., Van Driessen, K. “A fast algorithm for the minimum covariance determinant estimator” Technometrics 41(3), 212 (1999)
### 2.11.2.2\. Isolation Forest(隔离森林)
```py
在高维数据集中实现异常值检测的一种有效方法是使用随机森林
```
[`ensemble.IsolationForest`](https://scikit-learn.org/stable/modules/generated/sklearn.ensemble.IsolationForest.html#sklearn.ensemble.IsolationForest "sklearn.ensemble.IsolationForest") 通过随机选择特征然后随机选择所选特征的最大值和最小值之间的分割值来隔离观测。
由于递归划分可以由树形结构表示,因此隔离样本所需的分割次数等同于从根节点到终止节点的路径长度。
在这样的随机树的森林中取平均的路径长度是数据正态性和我们的决策功能的量度。
随机划分能为异常观测产生明显的较短路径。 因此,当随机树的森林共同为特定样本产生较短的路径长度时,这些样本就很有可能是异常观测。
该策略如下图所示。
[![http://sklearn.apachecn.org/cn/0.19.0/_images/sphx_glr_plot_isolation_forest_0011.png](img/769ae28b43c9c1f02556a7d6d878c7e0.jpg)](https://scikit-learn.org/stable/auto_examples/ensemble/plot_isolation_forest.html)
例子:
* See [Shrinkage covariance estimation: LedoitWolf vs OAS and max-likelihood](../auto_examples/covariance/plot_covariance_estimation.html#sphx-glr-auto-examples-covariance-plot-covariance-estimation-py) for an example on how to fit an [`EmpiricalCovariance`](generated/sklearn.covariance.EmpiricalCovariance.html#sklearn.covariance.EmpiricalCovariance "sklearn.covariance.EmpiricalCovariance") object to data.
\ No newline at end of file
* 参见 [IsolationForest example](https://scikit-learn.org/stable/auto_examples/ensemble/plot_isolation_forest.html#sphx-glr-auto-examples-ensemble-plot-isolation-forest-py) 说明隔离森林的用法。
* 参见 [Outlier detection with several methods.](https://scikit-learn.org/stable/auto_examples/covariance/plot_outlier_detection.html#sphx-glr-auto-examples-covariance-plot-outlier-detection-py) 比较 [`ensemble.IsolationForest`](https://scikit-learn.org/stable/modules/generated/sklearn.ensemble.IsolationForest.html#sklearn.ensemble.IsolationForest "sklearn.ensemble.IsolationForest")[`neighbors.LocalOutlierFactor`](https://scikit-learn.org/stable/modules/generated/sklearn.neighbors.LocalOutlierFactor.html#sklearn.neighbors.LocalOutlierFactor "sklearn.neighbors.LocalOutlierFactor"), [`svm.OneClassSVM`](https://scikit-learn.org/stable/modules/generated/sklearn.svm.OneClassSVM.html#sklearn.svm.OneClassSVM "sklearn.svm.OneClassSVM") (调整为执行类似异常值检测的方法)和基于协方差使用 [`covariance.EllipticEnvelope`](https://scikit-learn.org/stable/modules/generated/sklearn.covariance.EllipticEnvelope.html#sklearn.covariance.EllipticEnvelope "sklearn.covariance.EllipticEnvelope") 进行异常值检测。
参考文献:
* Liu, Fei Tony, Ting, Kai Ming and Zhou, Zhi-Hua. “Isolation forest.” Data Mining, 2008\. ICDM‘08\. Eighth IEEE International Conference on.
### 2.11.2.3\. Local Outlier Factor(局部异常系数)
对中等高维数据集实现异常值检测的另一种有效方法是使用局部异常系数(LOF)算法。
> [`neighbors.LocalOutlierFactor`](https://scikit-learn.org/stable/modules/generated/sklearn.neighbors.LocalOutlierFactor.html#sklearn.neighbors.LocalOutlierFactor "sklearn.neighbors.LocalOutlierFactor") (LOF)算法计算出反映观测异常程度的得分(称为局部异常系数)。 它测量给定数据点相对于其邻近点的局部密度偏差。 算法思想是检测出具有比其邻近点明显更低密度的样本。
实际上,局部密度从 k 个最近邻得到。 观测数据的 LOF 得分等于其 k 个最近邻的平均局部密度与其本身密度的比值:正常情况预期具有与其近邻类似的局部密度,而异常数据 则预计比局部密度要小得多。
考虑的k个近邻数(别名参数 n_neighbors )通常选择 1) 大于聚类必须包含的对象最小数量,以便其它对象相对于该聚类成为局部异常值,并且 2) 小于可能成为局部异常值对象的最大数量。 在实践中,这样的信息通常不可用,并且使 n_neighbors = 20 似乎通常都能很好地工作。 当异常值的比例较高时(即大于 10% 时,如下面的例子),n_neighbors 应该较大(在下面的例子中,n_neighbors = 35)。
LOF 算法的优点是考虑到数据集的局部和全局属性:即使在异常样本具有不同潜在密度的数据集中,它也能够表现得很好。 问题不在于样本是如何被分离的,而是样本与周围近邻的分离程度有多大。
该策略如下图所示。
[![http://sklearn.apachecn.org/cn/0.19.0/_images/sphx_glr_plot_lof_0011.png](img/7a958d3bae3db42cc0811b186bbc4e00.jpg)](https://scikit-learn.org/stable/auto_examples/neighbors/plot_lof.html)
示例:
* 参见 [Anomaly detection with Local Outlier Factor (LOF)](https://scikit-learn.org/stable/auto_examples/neighbors/plot_lof.html#sphx-glr-auto-examples-neighbors-plot-lof-py)[`neighbors.LocalOutlierFactor`](https://scikit-learn.org/stable/modules/generated/sklearn.neighbors.LocalOutlierFactor.html#sklearn.neighbors.LocalOutlierFactor "sklearn.neighbors.LocalOutlierFactor") 使用说明。
* 参见 [Outlier detection with several methods.](https://scikit-learn.org/stable/auto_examples/covariance/plot_outlier_detection.html#sphx-glr-auto-examples-covariance-plot-outlier-detection-py) 与其它异常检测方法进行比较。
参考文献:
* Breunig, Kriegel, Ng, and Sander (2000) [LOF: identifying density-based local outliers.](http://www.dbs.ifi.lmu.de/Publikationen/Papers/LOF.pdf) Proc. ACM SIGMOD
### 2.11.2.4\. 一类支持向量机与椭圆模型与隔离森林与局部异常系数
严格来说, One-class SVM (一类支持向量机)不是异常值检测方法,而是一种新奇检测方法:其训练集不应该被异常值污染,因为算法可能将它们拟合。 也就是说,高维度的异常值检测或对数据分布不做任何假设是非常具有挑战性的, 而一类支持向量机在这些情况下产生出有用的结果。
下面的例子说明了当数据越来越不单峰时, [`covariance.EllipticEnvelope`](https://scikit-learn.org/stable/modules/generated/sklearn.covariance.EllipticEnvelope.html#sklearn.covariance.EllipticEnvelope "sklearn.covariance.EllipticEnvelope") 的表现越来越差。 [`svm.OneClassSVM`](https://scikit-learn.org/stable/modules/generated/sklearn.svm.OneClassSVM.html#sklearn.svm.OneClassSVM "sklearn.svm.OneClassSVM") 在具有多种模式的数据上表现得更好, [`ensemble.IsolationForest`](https://scikit-learn.org/stable/modules/generated/sklearn.ensemble.IsolationForest.html#sklearn.ensemble.IsolationForest "sklearn.ensemble.IsolationForest")[`neighbors.LocalOutlierFactor`](https://scikit-learn.org/stable/modules/generated/sklearn.neighbors.LocalOutlierFactor.html#sklearn.neighbors.LocalOutlierFactor "sklearn.neighbors.LocalOutlierFactor") 在每种情况下都表现良好。
<caption>**Comparing One-class SVM, Isolation Forest, LOF, and Elliptic Envelope**</caption>
| 对于中心化和椭圆形的内围模式, [`svm.OneClassSVM`](https://scikit-learn.org/stable/modules/generated/sklearn.svm.OneClassSVM.html#sklearn.svm.OneClassSVM "sklearn.svm.OneClassSVM") 不适用于内围总体的旋转对称性。 此外,它拟合了一小部分存在于训练集中的异常值。 相反,基于拟合 [`covariance.EllipticEnvelope`](https://scikit-learn.org/stable/modules/generated/sklearn.covariance.EllipticEnvelope.html#sklearn.covariance.EllipticEnvelope "sklearn.covariance.EllipticEnvelope") 的决策规则学习出一个椭圆,对于内围分布拟合良好。 [`ensemble.IsolationForest`](https://scikit-learn.org/stable/modules/generated/sklearn.ensemble.IsolationForest.html#sklearn.ensemble.IsolationForest "sklearn.ensemble.IsolationForest")[`neighbors.LocalOutlierFactor`](https://scikit-learn.org/stable/modules/generated/sklearn.neighbors.LocalOutlierFactor.html#sklearn.neighbors.LocalOutlierFactor "sklearn.neighbors.LocalOutlierFactor") 也表现良好。 | [![outlier1](img/84298218bd1f4087ce7f8d55a4de1843.jpg)](https://scikit-learn.org/stable/auto_examples/covariance/plot_outlier_detection.html) |
| 由于内围分布变为双峰,所以 [`covariance.EllipticEnvelope`](https://scikit-learn.org/stable/modules/generated/sklearn.covariance.EllipticEnvelope.html#sklearn.covariance.EllipticEnvelope "sklearn.covariance.EllipticEnvelope") 对内围数据拟合得不好。 但是,我们可以看到 [`ensemble.IsolationForest`](https://scikit-learn.org/stable/modules/generated/sklearn.ensemble.IsolationForest.html#sklearn.ensemble.IsolationForest "sklearn.ensemble.IsolationForest"), [`svm.OneClassSVM`](https://scikit-learn.org/stable/modules/generated/sklearn.svm.OneClassSVM.html#sklearn.svm.OneClassSVM "sklearn.svm.OneClassSVM")[`neighbors.LocalOutlierFactor`](https://scikit-learn.org/stable/modules/generated/sklearn.neighbors.LocalOutlierFactor.html#sklearn.neighbors.LocalOutlierFactor "sklearn.neighbors.LocalOutlierFactor") 难以检测双峰,而且 [`svm.OneClassSVM`](https://scikit-learn.org/stable/modules/generated/sklearn.svm.OneClassSVM.html#sklearn.svm.OneClassSVM "sklearn.svm.OneClassSVM") 往往会过拟合:因为它没有内围模型, 便随机地把一些异常值当做内围值聚类在某个区域。 | [![outlier2](img/d7e5ca264ffbd335db57ba194e2a5d0f.jpg)](https://scikit-learn.org/stable/auto_examples/covariance/plot_outlier_detection.html) |
| 如果内围分布极度不正态,则 [`svm.OneClassSVM`](https://scikit-learn.org/stable/modules/generated/sklearn.svm.OneClassSVM.html#sklearn.svm.OneClassSVM "sklearn.svm.OneClassSVM"), [`ensemble.IsolationForest`](https://scikit-learn.org/stable/modules/generated/sklearn.ensemble.IsolationForest.html#sklearn.ensemble.IsolationForest "sklearn.ensemble.IsolationForest")[`neighbors.LocalOutlierFactor`](https://scikit-learn.org/stable/modules/generated/sklearn.neighbors.LocalOutlierFactor.html#sklearn.neighbors.LocalOutlierFactor "sklearn.neighbors.LocalOutlierFactor") 能构造出合理的近似结果, 而 [`covariance.EllipticEnvelope`](https://scikit-learn.org/stable/modules/generated/sklearn.covariance.EllipticEnvelope.html#sklearn.covariance.EllipticEnvelope "sklearn.covariance.EllipticEnvelope") 完全失败。 | [![outlier3](img/9e49f9c5d629711b032b027324d531b9.jpg)](https://scikit-learn.org/stable/auto_examples/covariance/plot_outlier_detection.html) |
示例:
* 参见 [Outlier detection with several methods.](https://scikit-learn.org/stable/auto_examples/covariance/plot_outlier_detection.html#sphx-glr-auto-examples-covariance-plot-outlier-detection-py) 比较 [`svm.OneClassSVM`](https://scikit-learn.org/stable/modules/generated/sklearn.svm.OneClassSVM.html#sklearn.svm.OneClassSVM "sklearn.svm.OneClassSVM") (调整为执行类似异常值检测的方法), [`ensemble.IsolationForest`](https://scikit-learn.org/stable/modules/generated/sklearn.ensemble.IsolationForest.html#sklearn.ensemble.IsolationForest "sklearn.ensemble.IsolationForest"), [`neighbors.LocalOutlierFactor`](https://scikit-learn.org/stable/modules/generated/sklearn.neighbors.LocalOutlierFactor.html#sklearn.neighbors.LocalOutlierFactor "sklearn.neighbors.LocalOutlierFactor") 和基于协方差使用 [`covariance.EllipticEnvelope`](https://scikit-learn.org/stable/modules/generated/sklearn.covariance.EllipticEnvelope.html#sklearn.covariance.EllipticEnvelope "sklearn.covariance.EllipticEnvelope") 进行异常值检测。
\ No newline at end of file
# 2.8\. 收敛协方差
# 2.12\. 密度估计
## 2.8.1\. 基本收敛
校验者:
        [@不将就](https://github.com/apachecn/scikit-learn-doc-zh)
翻译者:
        [@Xi](https://github.com/apachecn/scikit-learn-doc-zh)
尽管是协方差矩阵的无偏估计, 最大似然估计不是协方差矩阵的特征值的一个很好的估计, 所以从反演得到的精度矩阵是不准确的。 有时,甚至出现数学原因,经验协方差矩阵不能反转。 为了避免这样的反演问题,引入了经验协方差矩阵的一种变换方式:`shrinkage`
密度估计在无监督学习,特征工程和数据建模之间划分了界线。一些最流行和最有用的密度估计方法是混合模型,如高斯混合( [`sklearn.mixture.GaussianMixture`](https://scikit-learn.org/stable/modules/generated/sklearn.mixture.GaussianMixture.html#sklearn.mixture.GaussianMixture "sklearn.mixture.GaussianMixture") ), 和基于邻近的方法( [`sklearn.neighbors.KernelDensity`](https://scikit-learn.org/stable/modules/generated/sklearn.neighbors.KernelDensity.html#sklearn.neighbors.KernelDensity "sklearn.neighbors.KernelDensity") ),如核密度估计。 [clustering](clustering.html#clustering) 一节中更充分地讨论了高斯混合,因为此方法也用作为一种无监督聚类方案
在 scikit-learn 中,该变换(具有用户定义的收缩系数) 可以直接应用于使用 [`shrunk_covariance`](generated/sklearn.covariance.shrunk_covariance.html#sklearn.covariance.shrunk_covariance "sklearn.covariance.shrunk_covariance") 方法预先计算协方差。 此外,协方差的收缩估计可以用 [`ShrunkCovariance`](generated/sklearn.covariance.ShrunkCovariance.html#sklearn.covariance.ShrunkCovariance "sklearn.covariance.ShrunkCovariance") 对象 及其 [`ShrunkCovariance.fit`](generated/sklearn.covariance.ShrunkCovariance.html#sklearn.covariance.ShrunkCovariance.fit "sklearn.covariance.ShrunkCovariance.fit") 方法拟合到数据中。 再次,根据数据是否居中,结果会不同,所以可能要准确使用参数 `assume_centered`
密度估计是一个非常简单的概念,大多数人已经熟悉了其中一种常用的密度估计技术:直方图
在数学上,这种收缩在于减少经验协方差矩阵的最小和最大特征值之间的比率。 可以通过简单地根据给定的偏移量移动每个特征值来完成, 这相当于找到协方差矩阵的l2惩罚的最大似然估计器(l2-penalized Maximum Likelihood Estimator)。在实践中,收缩归结为简单的凸变换: ![\Sigma_{\rm
shrunk} = (1-\alpha)\hat{\Sigma} + \alpha\frac{{\rm
Tr}\hat{\Sigma}}{p}\rm Id](img/8f62a01deaf67a7f9960d853abd9dcca.jpg).
## 2.12.1\. 密度估计: 直方图
选择收缩量, ![\alpha](img/d8b3d5242d513369a44f8bf0c6112744.jpg) 相当于设置偏差/方差权衡,下面将讨论。
直方图是一种简单的数据可视化方法,其中定义了组( bins ),并且统计了每个组( bin )中的数据点的数量。在下图的左上角中可以看到一个直方图的例子:
示例:
**[![hist_to_kde](img/10a8344b866fca53744b728ef788a668.jpg)](https://scikit-learn.org/stable/auto_examples/neighbors/plot_kde_1d.html)**
* See [Shrinkage covariance estimation: LedoitWolf vs OAS and max-likelihood](../auto_examples/covariance/plot_covariance_estimation.html#sphx-glr-auto-examples-covariance-plot-covariance-estimation-py) for an example on how to fit a [`ShrunkCovariance`](generated/sklearn.covariance.ShrunkCovariance.html#sklearn.covariance.ShrunkCovariance "sklearn.covariance.ShrunkCovariance") object to data.
然而,直方图的一个主要问题是组( binning )的选择可能会对得到的可视化结果造成不相称的影响。考虑上图中右上角的图, 它显示了相同数据下组( bins )向右移动后的直方图。这两个可视化的结果看起来完全不同,可能会导致对数据作出不同的解释。
## 2.8.2\. Ledoit-Wolf 收敛
直观地说,你也可以把一个直方图看成由一堆块组成,每个点上放一个块,通过在合适的网格空间中堆积这些块,我们就可以得到直方图。但是,如果不是把这些块堆叠在一个规则的网格上,而是把每个块的中心定位在它所代表的点上,然后把每个位置的总高度相加呢?这样可以得到如上图左 下角所示的可视化.它可能不像直方图那样整洁,但是由数据决定块的位置意味着它能更好地表示基本的数据。
在他们的 2004 年的论文 [[1]](#id6) 中, O.Ledoit 和 M.Wolf 提出了一个公式, 用来计算优化的收敛系数 ![\alpha](img/d8b3d5242d513369a44f8bf0c6112744.jpg) , 它使得估计协方差和实际协方差矩阵之间的均方差进行最小化
这个可视化是核密度估计的一个例子,该例中用的是一种”顶帽”核(即每个点上的方块)。我们可以通过使用一种更平滑的核来得到一个更平滑的分布。上图右下角展示了一个高斯核密度估计,其中每个点都给总的分布贡献一条高斯曲线。结果是从数据中得到了一个平滑的密度估计,并且可作为一个强大的非参数模型用来估计这些点的分布
&lt;cite&gt;sklearn.covariance&lt;/cite&gt; 包中,可以使用 [`ledoit_wolf`](generated/sklearn.covariance.ledoit_wolf.html#sklearn.covariance.ledoit_wolf "sklearn.covariance.ledoit_wolf") 函数来计算样本的 基于 Ledoit-Wolf estimator 的协方差, 或者可以针对同样的样本 通过拟合 [`LedoitWolf`](generated/sklearn.covariance.LedoitWolf.html#sklearn.covariance.LedoitWolf "sklearn.covariance.LedoitWolf") 对象来获得。
## 2.12.2\. 核密度估计
例子:
[`sklearn.neighbors.KernelDensity`](https://scikit-learn.org/stable/modules/generated/sklearn.neighbors.KernelDensity.html#sklearn.neighbors.KernelDensity "sklearn.neighbors.KernelDensity") 实现了 scikit-learn 中的核密度估计,它使用 Ball Tree 或 KD Tree 来进行高效查询(有关这些讨论请参见 [最近邻](neighbors.html#neighbors) )。尽管为了简单起见上述示例采用的是一维数据集,但实际上核密度估计能够用在任意维度上, 不过在实际应用中,维数灾难会导致其在高维上的性能降低。
* See [Shrinkage covariance estimation: LedoitWolf vs OAS and max-likelihood](../auto_examples/covariance/plot_covariance_estimation.html#sphx-glr-auto-examples-covariance-plot-covariance-estimation-py) 关于如何将 [`LedoitWolf`](generated/sklearn.covariance.LedoitWolf.html#sklearn.covariance.LedoitWolf "sklearn.covariance.LedoitWolf") 对象与数据拟合, 并将 Ledoit-Wolf 估计器的性能进行可视化的示例。
如下图所示, 从双峰分布中绘制了100个点,并展示了选用三个不同核的核密度估计:
参考文献:
**[![kde_1d_distribution](img/dda3ed622f104bd7e6abffbcb1691998.jpg)](https://scikit-learn.org/stable/auto_examples/neighbors/plot_kde_1d.html)**
| [[1]](#id5) | O. Ledoit and M. Wolf, “A Well-Conditioned Estimator for Large-Dimensional Covariance Matrices”, Journal of Multivariate Analysis, Volume 88, Issue 2, February 2004, pages 365-411. |
图中可以很明显地看到核的形状如何影响结果分布的平滑度. 使用 scikit-learn 核密度估计的方法如下所示:
## 2.8.3\. Oracle 近似收缩
```py
>>> from sklearn.neighbors.kde import KernelDensity
>>> import numpy as np
>>> X = np.array([[-1, -1], [-2, -1], [-3, -2], [1, 1], [2, 1], [3, 2]])
>>> kde = KernelDensity(kernel='gaussian', bandwidth=0.2).fit(X)
>>> kde.score_samples(X)
array([-0.41075698, -0.41075698, -0.41076071, -0.41075698, -0.41075698,
-0.41076071])
在数据为高斯分布的假设下,Chen et al. 等 [[2]](#id8) 推导出了一个公式,旨在 产生比 Ledoit 和 Wolf 公式具有更小均方差的收敛系数。 所得到的估计器被称为协方差的 Oracle 收缩近似估计器。
```
&lt;cite&gt;sklearn.covariance&lt;/cite&gt; 包中, OAS 估计的协方差可以使用函数 [`oas`](generated/sklearn.covariance.oas.html#sklearn.covariance.oas "sklearn.covariance.oas") 对样本进行计算,或者可以通过将 [`OAS`](generated/sklearn.covariance.OAS.html#sklearn.covariance.OAS "sklearn.covariance.OAS") 对象拟合到相同的样本来获得。
如上所示,这里我们选用的是高斯核 `kernel='gaussian'` .在数学上,核是由带宽参数 ![h](img/c5f49595b56010ad04fce358940848e5.jpg) 控制的正值函数 ![K(x;h)](img/3dae1c97513f643047c2e33ee90ca8b5.jpg) . 给定核的形状后,在一组点 ![x_i; i=1\cdots N](img/fae30a190cd9e5f5d06f534d956df5f5.jpg) 内的 ![y](img/0775c03fc710a24df297dedcec515aaf.jpg) 点处的密度估计由下式给出:
[![http://sklearn.apachecn.org/cn/0.19.0/_images/sphx_glr_plot_covariance_estimation_0011.png](img/7f37b8cd4340f3d0981f518f14670c26.jpg)](../auto_examples/covariance/plot_covariance_estimation.html)
![\rho_K(y) = \sum_{i=1}^{N} K((y - x_i) / h)](img/8c8211edce4dbaeb44032f8d71a12135.jpg)
设定收缩时的偏差方差权衡:比较 Ledoit-Wolf 和 OAS 估计量的选择
这里的带宽作为平滑参数,用来平衡结果中偏差和方差的值。 大的带宽会导致非常平滑(即高偏差)密度分布,而小的带宽则导致不平滑(即高方差)密度分布。
参考文献:
[`sklearn.neighbors.KernelDensity`](https://scikit-learn.org/stable/modules/generated/sklearn.neighbors.KernelDensity.html#sklearn.neighbors.KernelDensity "sklearn.neighbors.KernelDensity") 实现了一些常见形状的核, 如下图所示:
| [[2]](#id7) | Chen et al., “Shrinkage Algorithms for MMSE Covariance Estimation”, IEEE Trans. on Sign. Proc., Volume 58, Issue 10, October 2010. |
**[![kde_kernels](img/f1fc9ca63c663059f76c2af6729189d1.jpg)](https://scikit-learn.org/stable/auto_examples/neighbors/plot_kde_1d.html)**
示例:
这些核的形式如下所示:
* Gaussian kernel (`kernel = 'gaussian'`)
![K(x; h) \propto \exp(- \frac{x^2}{2h^2} )](img/a960dd9452e5e5da7b211f6fdc120a0d.jpg)
* Tophat kernel (`kernel = 'tophat'`)
![K(x; h) \propto 1](img/2a58cf81e4ccf9022d6033557d49aed5.jpg) if ![x &lt; h](img/7d1d547b1a46ad827caeb7f0e85b213d.jpg)
* Epanechnikov kernel (`kernel = 'epanechnikov'`)
![K(x; h) \propto 1 - \frac{x^2}{h^2}](img/088266f72839f4195c9058dfd17d778b.jpg)
* Exponential kernel (`kernel = 'exponential'`)
![K(x; h) \propto \exp(-x/h)](img/3944d32654b4bf939d248b496f950b10.jpg)
* See [Shrinkage covariance estimation: LedoitWolf vs OAS and max-likelihood](../auto_examples/covariance/plot_covariance_estimation.html#sphx-glr-auto-examples-covariance-plot-covariance-estimation-py) for an example on how to fit an [`OAS`](generated/sklearn.covariance.OAS.html#sklearn.covariance.OAS "sklearn.covariance.OAS") object to data.
* See [Ledoit-Wolf vs OAS estimation](../auto_examples/covariance/plot_lw_vs_oas.html#sphx-glr-auto-examples-covariance-plot-lw-vs-oas-py) to visualize the Mean Squared Error difference between a [`LedoitWolf`](generated/sklearn.covariance.LedoitWolf.html#sklearn.covariance.LedoitWolf "sklearn.covariance.LedoitWolf") and an [`OAS`](generated/sklearn.covariance.OAS.html#sklearn.covariance.OAS "sklearn.covariance.OAS") estimator of the covariance.
* Linear kernel (`kernel = 'linear'`)
![K(x; h) \propto 1 - x/h](img/81da9e05103270be5e500ebc67cd1b45.jpg) if ![x &lt; h](img/7d1d547b1a46ad827caeb7f0e85b213d.jpg)
* Cosine kernel (`kernel = 'cosine'`)
![K(x; h) \propto \cos(\frac{\pi x}{2h})](img/6b0371e0d33eebe00fe5ec3d59de5b43.jpg) 如果 ![x &lt; h](img/7d1d547b1a46ad827caeb7f0e85b213d.jpg)
核密度估计可以与任何有效的距离度量一起使用(可用度量列表请参见 [`sklearn.neighbors.DistanceMetric`](https://scikit-learn.org/stable/modules/generated/sklearn.neighbors.DistanceMetric.html#sklearn.neighbors.DistanceMetric "sklearn.neighbors.DistanceMetric") ), 但其结果被适当地归一化处理,仅适用于欧几里德度量标准。 一个特别有用的度量是测量球体上的点与点之间角距离 的 [Haversine distance](https://en.wikipedia.org/wiki/Haversine_formula) 。 下面是使用核密度估计来对地理空间数据进行可视化的示例,本例中南美大陆两种不同物种的观测分布如图:
**[![species_kde](../Images/97b3ab2cc18a43b00c07a1cf6e118798.jpg)](https://scikit-learn.org/stable/auto_examples/neighbors/plot_species_kde.html)**
核密度估计的另一个有用的应用是从数据集中学习出一个非参数生成模型,以便有效地从该生成模型中绘制新的样本。 以下是使用此过程创建一组新的手写数字的示例,使用的是高斯核对数据的 PCA 投影进行学习:
**[![digits_kde](img/de6303ad5b9808c7ae8c64ddc632d893.jpg)](https://scikit-learn.org/stable/auto_examples/neighbors/plot_digits_kde_sampling.html)**
“新”数据由输入数据线性组合而成,其权重根据 KDE 模型按概率给出。
示例:
[![http://sklearn.apachecn.org/cn/0.19.0/_images/sphx_glr_plot_lw_vs_oas_0011.png](img/bc35316fd422689a9bc65845d63c1433.jpg)](../auto_examples/covariance/plot_lw_vs_oas.html)
\ No newline at end of file
* [Simple 1D Kernel Density Estimation](https://scikit-learn.org/stable/auto_examples/neighbors/plot_kde_1d.html#sphx-glr-auto-examples-neighbors-plot-kde-1d-py): 一维简单核密度估计的计算。
* [Kernel Density Estimation](https://scikit-learn.org/stable/auto_examples/neighbors/plot_digits_kde_sampling.html#sphx-glr-auto-examples-neighbors-plot-digits-kde-sampling-py): 使用核密度估计来学习手写数字数据生成模型,以及使用该模型绘制新样本的示例
* [Kernel Density Estimate of Species Distributions](https://scikit-learn.org/stable/auto_examples/neighbors/plot_species_kde.html#sphx-glr-auto-examples-neighbors-plot-species-kde-py): 使用Haversine距离度量来显示地理空间数据的核密度估计示例.
\ No newline at end of file
# 2.9\. 稀疏逆协方差
# 2.13\. 神经网络模型(无监督)
协方差矩阵的逆矩阵,通常称为精度矩阵(precision matrix),它与部分相关矩阵(partial correlation matrix)成正比。 它给出部分独立性关系。换句话说,如果两个特征在其他特征上有条件地独立, 则精度矩阵中的对应系数将为零。这就是为什么估计一个稀疏精度矩阵是有道理的: 通过从数据中学习独立关系,协方差矩阵的估计能更好处理。这被称为协方差选择。
校验者:
        [@不将就](https://github.com/apachecn/scikit-learn-doc-zh)
翻译者:
        [@夜神月](https://github.com/apachecn/scikit-learn-doc-zh)
在小样本的情况,即 `n_samples` 是数量级 `n_features` 或更小, 稀疏的逆协方差估计往往比收敛的协方差估计更好。 然而,在相反的情况下,或者对于非常相关的数据,它们可能在数值上不稳定。 此外,与收敛估算不同,稀疏估计器能够恢复非对角线结构 (off-diagonal structure)。
## 2.13.1\. 限制波尔兹曼机
[`GraphLasso`](generated/sklearn.covariance.GraphLasso.html#sklearn.covariance.GraphLasso "sklearn.covariance.GraphLasso") 估计器使用 L1 惩罚执行关于精度矩阵的稀疏性: `alpha` 参数越高,精度矩阵的稀疏性越大。 相应的 [`GraphLassoCV`](generated/sklearn.covariance.GraphLassoCV.html#sklearn.covariance.GraphLassoCV "sklearn.covariance.GraphLassoCV") 对象使用交叉验证来自动设置 `alpha` 参数
Restricted Boltzmann machines (RBM)(限制玻尔兹曼机)是基于概率模型的无监督非线性特征学习器。当用 RBM 或 RBMs 中的层次结构提取的特征在馈入线性分类器(如线性支持向量机或感知机)时通常会获得良好的结果
[![http://sklearn.apachecn.org/cn/0.19.0/_images/sphx_glr_plot_sparse_cov_0011.png](img/7c9ce57d4e2ba5270352f4e118af7fac.jpg)](../auto_examples/covariance/plot_sparse_cov.html)
该模型对输入的分布作出假设。目前,scikit-learn 只提供了 [`BernoulliRBM`](https://scikit-learn.org/stable/modules/generated/sklearn.neural_network.BernoulliRBM.html#sklearn.neural_network.BernoulliRBM "sklearn.neural_network.BernoulliRBM"),它假定输入是二值的,或者是 0 到 1 之间的值,每个值都编码特定特征被激活的概率。
Note
RBM 尝试使用特定图形模型最大化数据的可能性。所使用的参数学习算法( [Stochastic Maximum Likelihood](#sml) (随机最大似然))要防止特征表示偏离输入数据,这使得它们能学习到有趣的特征,但使得该模型对于小数据集不太有用且通常对于密度估计无效。
**结构恢复**
该方法随着独立RBM的权重初始化深层神经网络而普及。这种方法被称为无监督的预训练(unsupervised pre-training)。
从数据中的相关性恢复图形结构是一个具有挑战性的事情。如果您对这种恢复感兴趣,请记住:
[![http://sklearn.apachecn.org/cn/0.19.0/_images/sphx_glr_plot_rbm_logistic_classification_0011.png](img/3af1cb0026efe5789c3acf3669ff5bc6.jpg)](https://scikit-learn.org/stable/auto_examples/neural_networks/plot_rbm_logistic_classification.html)
* 相关矩阵的恢复比协方差矩阵更容易:在运行 [`GraphLasso`](generated/sklearn.covariance.GraphLasso.html#sklearn.covariance.GraphLasso "sklearn.covariance.GraphLasso") 前先标准化观察值
* 如果底层图具有比平均节点更多的连接节点,则算法将错过其中一些连接。
* 如果您的观察次数与底层图形中的边数相比不大,则不会恢复。
* 即使您具有良好的恢复条件,通过交叉验证(例如使用GraphLassoCV对象)选择的 Alpha 参数将导致选择太多边。 然而,相关边缘将具有比不相关边缘更重的权重。
示例:
数学公式如下:
* [Restricted Boltzmann Machine features for digit classification](https://scikit-learn.org/stable/auto_examples/neural_networks/plot_rbm_logistic_classification.html#sphx-glr-auto-examples-neural-networks-plot-rbm-logistic-classification-py)
### 2.13.1.1\. 图形模型和参数化
![\hat{K} = \mathrm{argmin}_K \big(
\mathrm{tr} S K - \mathrm{log} \mathrm{det} K
+ \alpha \|K\|_1
\big)](img/43996aff9311511e6e2f81912a249c7e.jpg)
RBM 的图形模型是一个全连接的二分图(fully-connected bipartite graph)。
![http://sklearn.apachecn.org/cn/0.19.0/_images/rbm_graph.png](img/0dd4aedd59a06ecbaead248c17a3ce80.jpg)
其中:![K](img/e279b8169ddd6581c5606c868ba52fae.jpg) 是要估计的精度矩阵(precision matrix), ![S](img/12ecd862769bee1e71c75c134b6423bb.jpg) 是样本的协方差矩阵。 ![\|K\|_1](img/6122e23454910f4f076c71a84c068291.jpg) 是非对角系数 ![K](img/e279b8169ddd6581c5606c868ba52fae.jpg) (off-diagonal coefficients)的绝对值之和。 用于解决这个问题的算法是来自 Friedman 2008 Biostatistics 论文的 GLasso 算法。 它与 R 语言 `glasso` 包中的算法相同。
节点是随机变量,其状态取决于它连接到的其他节点的状态。 因此,为了简单起见,模型被参数化为连接的权重以及每个可见和隐藏单元的一个偏置项。 我们用能量函数衡量联合概率分布的质量:
例子:
* [Sparse inverse covariance estimation](../auto_examples/covariance/plot_sparse_cov.html#sphx-glr-auto-examples-covariance-plot-sparse-cov-py):
![E(\mathbf{v}, \mathbf{h}) = \sum_i \sum_j w_{ij}v_ih_j + \sum_i b_iv_i
+ \sum_j c_jh_j](img/5959a6fe3c27570b7d474f26126eb628.jpg)
合成数据示例,显示结构的一些恢复,并与其他协方差估计器进行比较。
* [Visualizing the stock market structure](../auto_examples/applications/plot_stock_market.html#sphx-glr-auto-examples-applications-plot-stock-market-py): 真实股票市场数据示例,查找哪些信号最相关。
在上面的公式中, ![\mathbf{b}](img/4dee38783cbd4faef5d5639ce23a5c59.jpg) 和 ![\mathbf{c}](img/a4dd5119f3eeb13b99180aab64917975.jpg) 分别是可见层和隐藏层的偏置向量。 模型的联合概率是根据能量来定义的:
![P(\mathbf{v}, \mathbf{h}) = \frac{e^{-E(\mathbf{v}, \mathbf{h})}}{Z}](img/224162d8cfff6c8ad85718be6b261b70.jpg)
“限制”是指模型的二分图结构,它禁止隐藏单元之间或可见单元之间的直接交互。 这代表以下条件独立性成立:
![h_i \bot h_j | \mathbf{v} \\
v_i \bot v_j | \mathbf{h}](img/9521899a181a367c5873e61b9f7785ce.jpg)
二分图结构允许使用高效的块吉比斯采样(block Gibbs sampling)进行推断。
### 2.13.1.2\. 伯努利限制玻尔兹曼机
[`BernoulliRBM`](https://scikit-learn.org/stable/modules/generated/sklearn.neural_network.BernoulliRBM.html#sklearn.neural_network.BernoulliRBM "sklearn.neural_network.BernoulliRBM") 中,所有单位都是二进制随机单元。 这意味着输入数据应该是二进制的,或者在 0 和 1 之间的实数值表示可见单元活跃或不活跃的概率。 这是一个很好的字符识别模型,其中的关注点是哪些像素是活跃的,哪些不是。 对于自然场景的图像,它不再适合,因为背景,深度和相邻像素的趋势取相同的值。
每个单位的条件概率分布由其接收的输入的sigmoid函数给出:
![P(v_i=1|\mathbf{h}) = \sigma(\sum_j w_{ij}h_j + b_i) \\
P(h_i=1|\mathbf{v}) = \sigma(\sum_i w_{ij}v_i + c_j)](img/e6811d3f6333e9490d602db8dc1e3d96.jpg)
其中 ![\sigma](img/8c4a5c99b21079b9fb1be49910ff96e3.jpg) 是Sigmoid函数:
![\sigma(x) = \frac{1}{1 + e^{-x}}](img/f1c8c9b812ae1b6ec189eda900e47269.jpg)
### 2.13.1.3\. 随机最大似然学习
[`BernoulliRBM`](https://scikit-learn.org/stable/modules/generated/sklearn.neural_network.BernoulliRBM.html#sklearn.neural_network.BernoulliRBM "sklearn.neural_network.BernoulliRBM") 函数中实现的学习算法被称为随机最大似然(Stochastic Maximum Likelihood (SML))或持续对比发散(Persistent Contrastive Divergence (PCD))。由于数据的似然函数的形式,直接优化最大似然是不可行的:
![\log P(v) = \log \sum_h e^{-E(v, h)} - \log \sum_{x, y} e^{-E(x, y)}](img/1252aa7af065b0afd424b2ff01b4e2a5.jpg)
为了简单起见,上面的等式是针对单个训练样本所写的。相对于权重的梯度由对应于上述的两个项构成。根据它们的符号,它们通常被称为正梯度和负梯度。在这种实现中,按照小批量梯度(mini-batches of samples )对梯度进行计算。
在 maximizing the log-likelihood (最大化对数似然度)的情况下,正梯度使模型更倾向于与观察到的训练数据兼容的隐藏状态。由于 RBM 的二分体结构,可以有效地计算。然而,负梯度是棘手的。其目标是降低模型偏好的联合状态的能量,从而使数据保持真实。可以通过马尔可夫链蒙特卡罗近似,使用块 Gibbs 采样,通过迭代地对每个给定另一个的 ![v](img/ce7fd91ddb116ab5d747de0316caf657.jpg) 和 ![h](img/c5f49595b56010ad04fce358940848e5.jpg) 进行采样,直到链混合。以这种方式产生的样品有时被称为幻想粒子。这是无效的,很难确定马可夫链是否混合。
对比发散方法建议在经过少量迭代后停止链,![k](img/f93871977da52a6d11045d57c3e18728.jpg) 通常为 1.该方法快速且方差小,但样本远离模型分布。
持续的对比分歧解决这个问题。而不是每次需要梯度启动一个新的链,并且只执行一个 Gibbs 采样步骤,在 PCD 中,我们保留了在每个权重更新之后更新的 ![k](img/f93871977da52a6d11045d57c3e18728.jpg) Gibbs 步长的多个链(幻想粒子)。这使得颗粒更彻底地探索空间.
参考文献:
* Friedman et al, [“Sparse inverse covariance estimation with the graphical lasso”](http://biostatistics.oxfordjournals.org/content/9/3/432.short), Biostatistics 9, pp 432, 2008
\ No newline at end of file
* [“A fast learning algorithm for deep belief nets”](http://www.cs.toronto.edu/~hinton/absps/fastnc.pdf) G. Hinton, S. Osindero, Y.-W. Teh, 2006
* [“Training Restricted Boltzmann Machines using Approximations to the Likelihood Gradient”](http://www.cs.toronto.edu/~tijmen/pcd/pcd.pdf) T. Tieleman, 2008
\ No newline at end of file
# 2.10\. Robust 协方差估计
实际数据集通常是会有测量或记录错误。合格但不常见的观察也可能出于各种原因。 每个不常见的观察称为异常值。 上面提出的经验协方差估计器和收缩协方差估计器对数据中异常观察值非常敏感。 因此,应该使用更好的协方差估计(robust covariance estimators)来估算其真实数据集的协方差。 或者,可以使用更好的协方差估计器(robust covariance estimators)来执行异常值检测, 并根据数据的进一步处理,丢弃/降低某些观察值。
`sklearn.covariance` 包实现了 robust estimator of covariance, 即 Minimum Covariance Determinant [[3]](#id14)
## 2.10.1\. 最小协方差决定
最小协方差决定(Minimum Covariance Determinant)估计器是 由 P.J. Rousseeuw 在 [[3]](#id14) 中引入的数据集协方差的鲁棒估计 (robust estimator)。 这个想法是找出一个给定比例(h)的 “好” 观察值,它们不是离群值, 且可以计算其经验协方差矩阵。 然后将该经验协方差矩阵重新缩放以补偿所执行的观察选择(”consistency step(一致性步骤)”)。 计算最小协方差决定估计器后,可以根据其马氏距离(Mahalanobis distance)给出观测值的权重, 这导致数据集的协方差矩阵的重新加权估计(”reweighting step(重新加权步骤)”)。
Rousseeuw 和 Van Driessen [[4]](#id15) 开发了 FastMCD 算法,以计算最小协方差决定因子(Minimum Covariance Determinant)。 在 scikit-learn 中,该算法在将 MCD 对象拟合到数据时应用。FastMCD 算法同时计算数据集位置的鲁棒估计。
Raw估计可通过 [`MinCovDet`](generated/sklearn.covariance.MinCovDet.html#sklearn.covariance.MinCovDet "sklearn.covariance.MinCovDet") 对象的 `raw_location_``raw_covariance_` 属性获得。
参考文献:
| [3] | _([1](#id10), [2](#id12))_ P. J. Rousseeuw. Least median of squares regression. J. Am Stat Ass, 79:871, 1984. |
| [[4]](#id13) | A Fast Algorithm for the Minimum Covariance Determinant Estimator, 1999, American Statistical Association and the American Society for Quality, TECHNOMETRICS. |
例子:
* See [Robust vs Empirical covariance estimate](../auto_examples/covariance/plot_robust_vs_empirical_covariance.html#sphx-glr-auto-examples-covariance-plot-robust-vs-empirical-covariance-py) 关于如何将对象 [`MinCovDet`](generated/sklearn.covariance.MinCovDet.html#sklearn.covariance.MinCovDet "sklearn.covariance.MinCovDet") 与数据拟合的示例, 尽管存在异常值,但估计结果仍然比较准确。
* See [Robust covariance estimation and Mahalanobis distances relevance](../auto_examples/covariance/plot_mahalanobis_distances.html#sphx-glr-auto-examples-covariance-plot-mahalanobis-distances-py) 马氏距离(Mahalanobis distance),针对协方差估计器 [`EmpiricalCovariance`](generated/sklearn.covariance.EmpiricalCovariance.html#sklearn.covariance.EmpiricalCovariance "sklearn.covariance.EmpiricalCovariance")[`MinCovDet`](generated/sklearn.covariance.MinCovDet.html#sklearn.covariance.MinCovDet "sklearn.covariance.MinCovDet") 之间的差异进行可视化。(所以我们得到了精度矩阵的更好估计)
| Influence of outliers on location and covariance estimates | Separating inliers from outliers using a Mahalanobis distance |
| --- | --- |
| [![robust_vs_emp](img/e1b4d08b93d1d222e96d99475a766281.jpg)](../auto_examples/covariance/plot_robust_vs_empirical_covariance.html) | [![mahalanobis](img/89807b9fa9f5924b027486f280324b1a.jpg)](../auto_examples/covariance/plot_mahalanobis_distances.html) |
\ No newline at end of file
......@@ -72,7 +72,7 @@ LDA 和 QDA 都是源于简单的概率模型,这些模型对于每一个类
`shrinkage` parameter (收缩参数)的值同样也可以手动被设置为 0-1 之间。特别地,0 值对应着没有收缩(这意味着经验协方差矩阵将会被使用), 而 1 值则对应着完全使用收缩(意味着方差的对角矩阵将被当作协方差矩阵的估计)。设置该参数在两个极端值之间会估计一个(特定的)协方差矩阵的收缩形式
**[![shrinkage](img/d5021b539c18587624a07ef6df00f585.jpg)](../auto_examples/classification/plot_lda.html)**
**[![shrinkage](img/d5021b539c18587624a07ef6df00f585.jpg)](https://scikit-learn.org/stable/auto_examples/classification/plot_lda.html)**
## 1.2.5\. 预估算法
......
# 2.11\. 新奇和异常值检测
校验者:
        [@RyanZhiNie](https://github.com/RyanZhiNie)
        [@羊三](https://github.com/apachecn/scikit-learn-doc-zh)
翻译者:
        [@羊三](https://github.com/apachecn/scikit-learn-doc-zh)
许多应用需要能够对新观测进行判断,判断其是否与现有观测服从同一分布(即新观测为内围值),相反则被认为不服从同一分布(即新观测为异常值)。 通常,这种能力被用于清理实际的数据集。必须做出两种重要区分:
| 新奇检测: | 训练数据未被异常值污染,我们对于新观测中的异常情况有兴趣检测。 |
| --- | --- |
| 异常值检测: | 训练数据包含异常值,我们需要拟合出训练数据的中心模式,以忽略有偏差的观测。 |
| --- | --- |
scikit-learn项目提供了一套可用于新奇或异常值检测的机器学习工具。 该策略是以无监督的方式学习数据中的对象来实现的:
```py
estimator.fit(X_train)
```
然后可以使用 &lt;cite&gt;predict&lt;/cite&gt; 方法将新观测归为内围值或异常值:
```py
estimator.predict(X_test)
```
内围值被标记为1,而异常值被标记为-1。
## 2.11.1\. Novelty Detection(新奇检测)
考虑一个来自同一分布的数据集,以 ![p](img/e2f9b08680b30cfb80102f69264fdd5c.jpg) 个特征描述、有 ![n](img/c87d9110f3d32ffa5fa08671e4af11fb.jpg) 个观测。 现在考虑我们再往该数据集中添加一个观测。 如果新观测与原有观测有很大差异,我们就可以怀疑它是否是内围值吗? (即是否来自同一分布?)或者相反,如果新观测与原有观测很相似,我们就无法将其与原有观测区分开吗? 这就是新奇检测工具和方法所解决的问题。
一般来说,它将要学习出一个粗略且紧密的边界,界定出初始观测分布的轮廓,绘制在相互嵌入的 ![p](img/e2f9b08680b30cfb80102f69264fdd5c.jpg) 维空间中。 那么,如果后续的观测在边界划分的子空间内,则它们被认为来自与初始观测相同的总体。 否则,如果它们在边界之外,我们可以说就我们评估中给定的置信度而言,它们是异常值。
One-Class SVM(一类支持向量机)已经由 Schölkopf 等人采用以实现新奇检测,并在 [支持向量机](svm.html#svm) 模块的 [`svm.OneClassSVM`](generated/sklearn.svm.OneClassSVM.html#sklearn.svm.OneClassSVM "sklearn.svm.OneClassSVM") 对象中实现。 需要选择 kernel 和 scalar 参数来定义边界。 通常选择 RBF kernel,即使没有确切的公式或算法来设置其带宽参数。 这是 scikit-learn 实现中的默认值。 ![\nu](img/f996477bc9806499e6b6a1ea4d9ae8eb.jpg) 参数,也称为一类支持向量机的边沿,对应于在边界之外找到新的但内围的观测的概率。
参考文献:
* [Estimating the support of a high-dimensional distribution](http://dl.acm.org/citation.cfm?id=1119749) Schölkopf, Bernhard, et al. Neural computation 13.7 (2001): 1443-1471.
例子:
* 参见 [One-class SVM with non-linear kernel (RBF)](../auto_examples/svm/plot_oneclass.html#sphx-glr-auto-examples-svm-plot-oneclass-py) ,通过 [`svm.OneClassSVM`](generated/sklearn.svm.OneClassSVM.html#sklearn.svm.OneClassSVM "sklearn.svm.OneClassSVM") 对象学习一些数据来将边界可视化。
[![http://sklearn.apachecn.org/cn/0.19.0/_images/sphx_glr_plot_oneclass_0011.png](img/b29b59eca5c581c3f54d92c1671f2b2e.jpg)](../auto_examples/svm/plot_oneclass.html)
## 2.11.2\. Outlier Detection(异常值检测)
异常值检测类似于新奇检测,其目的是将内围观测的中心与一些被称为 “异常值” 的污染数据进行分离。 然而,在异常值检测的情况下,我们没有干净且适用于训练任何工具的数据集来代表内围观测的总体。
### 2.11.2.1\. Fitting an elliptic envelope(椭圆模型拟合)
实现异常值检测的一种常见方式是假设内围数据来自已知分布(例如,数据服从高斯分布)。 从这个假设来看,我们通常试图定义数据的 “形状”,并且可以将异常观测定义为足够远离拟合形状的观测。
scikit-learn 提供了 [`covariance.EllipticEnvelope`](generated/sklearn.covariance.EllipticEnvelope.html#sklearn.covariance.EllipticEnvelope "sklearn.covariance.EllipticEnvelope") 对象,它能拟合出数据的稳健协方差估计,从而为中心数据点拟合出一个椭圆,忽略中心模式之外的点。
例如,假设内围数据服从高斯分布,它将稳健地(即不受异常值的影响)估计内围位置和协方差。 从该估计得到的马氏距离用于得出异常度量。 该策略如下图所示。
[![http://sklearn.apachecn.org/cn/0.19.0/_images/sphx_glr_plot_mahalanobis_distances_0011.png](img/89807b9fa9f5924b027486f280324b1a.jpg)](../auto_examples/covariance/plot_mahalanobis_distances.html)
示例:
* 参见 [Robust covariance estimation and Mahalanobis distances relevance](../auto_examples/covariance/plot_mahalanobis_distances.html#sphx-glr-auto-examples-covariance-plot-mahalanobis-distances-py) 说明对位置和协方差使用标准估计 ([`covariance.EmpiricalCovariance`](generated/sklearn.covariance.EmpiricalCovariance.html#sklearn.covariance.EmpiricalCovariance "sklearn.covariance.EmpiricalCovariance")) 或稳健估计 ([`covariance.MinCovDet`](generated/sklearn.covariance.MinCovDet.html#sklearn.covariance.MinCovDet "sklearn.covariance.MinCovDet")) 来评估观测的异常程度的差异。
参考文献:
* Rousseeuw, P.J., Van Driessen, K. “A fast algorithm for the minimum covariance determinant estimator” Technometrics 41(3), 212 (1999)
### 2.11.2.2\. Isolation Forest(隔离森林)
```py
在高维数据集中实现异常值检测的一种有效方法是使用随机森林
```
[`ensemble.IsolationForest`](generated/sklearn.ensemble.IsolationForest.html#sklearn.ensemble.IsolationForest "sklearn.ensemble.IsolationForest") 通过随机选择特征然后随机选择所选特征的最大值和最小值之间的分割值来隔离观测。
由于递归划分可以由树形结构表示,因此隔离样本所需的分割次数等同于从根节点到终止节点的路径长度。
在这样的随机树的森林中取平均的路径长度是数据正态性和我们的决策功能的量度。
随机划分能为异常观测产生明显的较短路径。 因此,当随机树的森林共同为特定样本产生较短的路径长度时,这些样本就很有可能是异常观测。
该策略如下图所示。
[![http://sklearn.apachecn.org/cn/0.19.0/_images/sphx_glr_plot_isolation_forest_0011.png](img/769ae28b43c9c1f02556a7d6d878c7e0.jpg)](../auto_examples/ensemble/plot_isolation_forest.html)
例子:
* 参见 [IsolationForest example](../auto_examples/ensemble/plot_isolation_forest.html#sphx-glr-auto-examples-ensemble-plot-isolation-forest-py) 说明隔离森林的用法。
* 参见 [Outlier detection with several methods.](../auto_examples/covariance/plot_outlier_detection.html#sphx-glr-auto-examples-covariance-plot-outlier-detection-py) 比较 [`ensemble.IsolationForest`](generated/sklearn.ensemble.IsolationForest.html#sklearn.ensemble.IsolationForest "sklearn.ensemble.IsolationForest")[`neighbors.LocalOutlierFactor`](generated/sklearn.neighbors.LocalOutlierFactor.html#sklearn.neighbors.LocalOutlierFactor "sklearn.neighbors.LocalOutlierFactor"), [`svm.OneClassSVM`](generated/sklearn.svm.OneClassSVM.html#sklearn.svm.OneClassSVM "sklearn.svm.OneClassSVM") (调整为执行类似异常值检测的方法)和基于协方差使用 [`covariance.EllipticEnvelope`](generated/sklearn.covariance.EllipticEnvelope.html#sklearn.covariance.EllipticEnvelope "sklearn.covariance.EllipticEnvelope") 进行异常值检测。
参考文献:
* Liu, Fei Tony, Ting, Kai Ming and Zhou, Zhi-Hua. “Isolation forest.” Data Mining, 2008\. ICDM‘08\. Eighth IEEE International Conference on.
### 2.11.2.3\. Local Outlier Factor(局部异常系数)
对中等高维数据集实现异常值检测的另一种有效方法是使用局部异常系数(LOF)算法。
> [`neighbors.LocalOutlierFactor`](generated/sklearn.neighbors.LocalOutlierFactor.html#sklearn.neighbors.LocalOutlierFactor "sklearn.neighbors.LocalOutlierFactor") (LOF)算法计算出反映观测异常程度的得分(称为局部异常系数)。 它测量给定数据点相对于其邻近点的局部密度偏差。 算法思想是检测出具有比其邻近点明显更低密度的样本。
实际上,局部密度从 k 个最近邻得到。 观测数据的 LOF 得分等于其 k 个最近邻的平均局部密度与其本身密度的比值:正常情况预期具有与其近邻类似的局部密度,而异常数据 则预计比局部密度要小得多。
考虑的k个近邻数(别名参数 n_neighbors )通常选择 1) 大于聚类必须包含的对象最小数量,以便其它对象相对于该聚类成为局部异常值,并且 2) 小于可能成为局部异常值对象的最大数量。 在实践中,这样的信息通常不可用,并且使 n_neighbors = 20 似乎通常都能很好地工作。 当异常值的比例较高时(即大于 10% 时,如下面的例子),n_neighbors 应该较大(在下面的例子中,n_neighbors = 35)。
LOF 算法的优点是考虑到数据集的局部和全局属性:即使在异常样本具有不同潜在密度的数据集中,它也能够表现得很好。 问题不在于样本是如何被分离的,而是样本与周围近邻的分离程度有多大。
该策略如下图所示。
[![http://sklearn.apachecn.org/cn/0.19.0/_images/sphx_glr_plot_lof_0011.png](img/7a958d3bae3db42cc0811b186bbc4e00.jpg)](../auto_examples/neighbors/plot_lof.html)
示例:
* 参见 [Anomaly detection with Local Outlier Factor (LOF)](../auto_examples/neighbors/plot_lof.html#sphx-glr-auto-examples-neighbors-plot-lof-py)[`neighbors.LocalOutlierFactor`](generated/sklearn.neighbors.LocalOutlierFactor.html#sklearn.neighbors.LocalOutlierFactor "sklearn.neighbors.LocalOutlierFactor") 使用说明。
* 参见 [Outlier detection with several methods.](../auto_examples/covariance/plot_outlier_detection.html#sphx-glr-auto-examples-covariance-plot-outlier-detection-py) 与其它异常检测方法进行比较。
参考文献:
* Breunig, Kriegel, Ng, and Sander (2000) [LOF: identifying density-based local outliers.](http://www.dbs.ifi.lmu.de/Publikationen/Papers/LOF.pdf) Proc. ACM SIGMOD
### 2.11.2.4\. 一类支持向量机与椭圆模型与隔离森林与局部异常系数
严格来说, One-class SVM (一类支持向量机)不是异常值检测方法,而是一种新奇检测方法:其训练集不应该被异常值污染,因为算法可能将它们拟合。 也就是说,高维度的异常值检测或对数据分布不做任何假设是非常具有挑战性的, 而一类支持向量机在这些情况下产生出有用的结果。
下面的例子说明了当数据越来越不单峰时, [`covariance.EllipticEnvelope`](generated/sklearn.covariance.EllipticEnvelope.html#sklearn.covariance.EllipticEnvelope "sklearn.covariance.EllipticEnvelope") 的表现越来越差。 [`svm.OneClassSVM`](generated/sklearn.svm.OneClassSVM.html#sklearn.svm.OneClassSVM "sklearn.svm.OneClassSVM") 在具有多种模式的数据上表现得更好, [`ensemble.IsolationForest`](generated/sklearn.ensemble.IsolationForest.html#sklearn.ensemble.IsolationForest "sklearn.ensemble.IsolationForest")[`neighbors.LocalOutlierFactor`](generated/sklearn.neighbors.LocalOutlierFactor.html#sklearn.neighbors.LocalOutlierFactor "sklearn.neighbors.LocalOutlierFactor") 在每种情况下都表现良好。
<caption>**Comparing One-class SVM, Isolation Forest, LOF, and Elliptic Envelope**</caption>
| 对于中心化和椭圆形的内围模式, [`svm.OneClassSVM`](generated/sklearn.svm.OneClassSVM.html#sklearn.svm.OneClassSVM "sklearn.svm.OneClassSVM") 不适用于内围总体的旋转对称性。 此外,它拟合了一小部分存在于训练集中的异常值。 相反,基于拟合 [`covariance.EllipticEnvelope`](generated/sklearn.covariance.EllipticEnvelope.html#sklearn.covariance.EllipticEnvelope "sklearn.covariance.EllipticEnvelope") 的决策规则学习出一个椭圆,对于内围分布拟合良好。 [`ensemble.IsolationForest`](generated/sklearn.ensemble.IsolationForest.html#sklearn.ensemble.IsolationForest "sklearn.ensemble.IsolationForest")[`neighbors.LocalOutlierFactor`](generated/sklearn.neighbors.LocalOutlierFactor.html#sklearn.neighbors.LocalOutlierFactor "sklearn.neighbors.LocalOutlierFactor") 也表现良好。 | [![outlier1](img/84298218bd1f4087ce7f8d55a4de1843.jpg)](../auto_examples/covariance/plot_outlier_detection.html) |
| 由于内围分布变为双峰,所以 [`covariance.EllipticEnvelope`](generated/sklearn.covariance.EllipticEnvelope.html#sklearn.covariance.EllipticEnvelope "sklearn.covariance.EllipticEnvelope") 对内围数据拟合得不好。 但是,我们可以看到 [`ensemble.IsolationForest`](generated/sklearn.ensemble.IsolationForest.html#sklearn.ensemble.IsolationForest "sklearn.ensemble.IsolationForest"), [`svm.OneClassSVM`](generated/sklearn.svm.OneClassSVM.html#sklearn.svm.OneClassSVM "sklearn.svm.OneClassSVM")[`neighbors.LocalOutlierFactor`](generated/sklearn.neighbors.LocalOutlierFactor.html#sklearn.neighbors.LocalOutlierFactor "sklearn.neighbors.LocalOutlierFactor") 难以检测双峰,而且 [`svm.OneClassSVM`](generated/sklearn.svm.OneClassSVM.html#sklearn.svm.OneClassSVM "sklearn.svm.OneClassSVM") 往往会过拟合:因为它没有内围模型, 便随机地把一些异常值当做内围值聚类在某个区域。 | [![outlier2](img/d7e5ca264ffbd335db57ba194e2a5d0f.jpg)](../auto_examples/covariance/plot_outlier_detection.html) |
| 如果内围分布极度不正态,则 [`svm.OneClassSVM`](generated/sklearn.svm.OneClassSVM.html#sklearn.svm.OneClassSVM "sklearn.svm.OneClassSVM"), [`ensemble.IsolationForest`](generated/sklearn.ensemble.IsolationForest.html#sklearn.ensemble.IsolationForest "sklearn.ensemble.IsolationForest")[`neighbors.LocalOutlierFactor`](generated/sklearn.neighbors.LocalOutlierFactor.html#sklearn.neighbors.LocalOutlierFactor "sklearn.neighbors.LocalOutlierFactor") 能构造出合理的近似结果, 而 [`covariance.EllipticEnvelope`](generated/sklearn.covariance.EllipticEnvelope.html#sklearn.covariance.EllipticEnvelope "sklearn.covariance.EllipticEnvelope") 完全失败。 | [![outlier3](img/9e49f9c5d629711b032b027324d531b9.jpg)](../auto_examples/covariance/plot_outlier_detection.html) |
示例:
* 参见 [Outlier detection with several methods.](../auto_examples/covariance/plot_outlier_detection.html#sphx-glr-auto-examples-covariance-plot-outlier-detection-py) 比较 [`svm.OneClassSVM`](generated/sklearn.svm.OneClassSVM.html#sklearn.svm.OneClassSVM "sklearn.svm.OneClassSVM") (调整为执行类似异常值检测的方法), [`ensemble.IsolationForest`](generated/sklearn.ensemble.IsolationForest.html#sklearn.ensemble.IsolationForest "sklearn.ensemble.IsolationForest"), [`neighbors.LocalOutlierFactor`](generated/sklearn.neighbors.LocalOutlierFactor.html#sklearn.neighbors.LocalOutlierFactor "sklearn.neighbors.LocalOutlierFactor") 和基于协方差使用 [`covariance.EllipticEnvelope`](generated/sklearn.covariance.EllipticEnvelope.html#sklearn.covariance.EllipticEnvelope "sklearn.covariance.EllipticEnvelope") 进行异常值检测。
\ No newline at end of file
# 2.12\. 密度估计
校验者:
        [@不将就](https://github.com/apachecn/scikit-learn-doc-zh)
翻译者:
        [@Xi](https://github.com/apachecn/scikit-learn-doc-zh)
密度估计在无监督学习,特征工程和数据建模之间划分了界线。一些最流行和最有用的密度估计方法是混合模型,如高斯混合( [`sklearn.mixture.GaussianMixture`](generated/sklearn.mixture.GaussianMixture.html#sklearn.mixture.GaussianMixture "sklearn.mixture.GaussianMixture") ), 和基于邻近的方法( [`sklearn.neighbors.KernelDensity`](generated/sklearn.neighbors.KernelDensity.html#sklearn.neighbors.KernelDensity "sklearn.neighbors.KernelDensity") ),如核密度估计。 [clustering](clustering.html#clustering) 一节中更充分地讨论了高斯混合,因为此方法也用作为一种无监督聚类方案。
密度估计是一个非常简单的概念,大多数人已经熟悉了其中一种常用的密度估计技术:直方图。
## 2.12.1\. 密度估计: 直方图
直方图是一种简单的数据可视化方法,其中定义了组( bins ),并且统计了每个组( bin )中的数据点的数量。在下图的左上角中可以看到一个直方图的例子:
**[![hist_to_kde](img/10a8344b866fca53744b728ef788a668.jpg)](../auto_examples/neighbors/plot_kde_1d.html)**
然而,直方图的一个主要问题是组( binning )的选择可能会对得到的可视化结果造成不相称的影响。考虑上图中右上角的图, 它显示了相同数据下组( bins )向右移动后的直方图。这两个可视化的结果看起来完全不同,可能会导致对数据作出不同的解释。
直观地说,你也可以把一个直方图看成由一堆块组成,每个点上放一个块,通过在合适的网格空间中堆积这些块,我们就可以得到直方图。但是,如果不是把这些块堆叠在一个规则的网格上,而是把每个块的中心定位在它所代表的点上,然后把每个位置的总高度相加呢?这样可以得到如上图左 下角所示的可视化.它可能不像直方图那样整洁,但是由数据决定块的位置意味着它能更好地表示基本的数据。
这个可视化是核密度估计的一个例子,该例中用的是一种”顶帽”核(即每个点上的方块)。我们可以通过使用一种更平滑的核来得到一个更平滑的分布。上图右下角展示了一个高斯核密度估计,其中每个点都给总的分布贡献一条高斯曲线。结果是从数据中得到了一个平滑的密度估计,并且可作为一个强大的非参数模型用来估计这些点的分布。
## 2.12.2\. 核密度估计
[`sklearn.neighbors.KernelDensity`](generated/sklearn.neighbors.KernelDensity.html#sklearn.neighbors.KernelDensity "sklearn.neighbors.KernelDensity") 实现了 scikit-learn 中的核密度估计,它使用 Ball Tree 或 KD Tree 来进行高效查询(有关这些讨论请参见 [最近邻](neighbors.html#neighbors) )。尽管为了简单起见上述示例采用的是一维数据集,但实际上核密度估计能够用在任意维度上, 不过在实际应用中,维数灾难会导致其在高维上的性能降低。
如下图所示, 从双峰分布中绘制了100个点,并展示了选用三个不同核的核密度估计:
**[![kde_1d_distribution](img/dda3ed622f104bd7e6abffbcb1691998.jpg)](../auto_examples/neighbors/plot_kde_1d.html)**
图中可以很明显地看到核的形状如何影响结果分布的平滑度. 使用 scikit-learn 核密度估计的方法如下所示:
```py
>>> from sklearn.neighbors.kde import KernelDensity
>>> import numpy as np
>>> X = np.array([[-1, -1], [-2, -1], [-3, -2], [1, 1], [2, 1], [3, 2]])
>>> kde = KernelDensity(kernel='gaussian', bandwidth=0.2).fit(X)
>>> kde.score_samples(X)
array([-0.41075698, -0.41075698, -0.41076071, -0.41075698, -0.41075698,
-0.41076071])
```
如上所示,这里我们选用的是高斯核 `kernel='gaussian'` .在数学上,核是由带宽参数 ![h](img/c5f49595b56010ad04fce358940848e5.jpg) 控制的正值函数 ![K(x;h)](img/3dae1c97513f643047c2e33ee90ca8b5.jpg) . 给定核的形状后,在一组点 ![x_i; i=1\cdots N](img/fae30a190cd9e5f5d06f534d956df5f5.jpg) 内的 ![y](img/0775c03fc710a24df297dedcec515aaf.jpg) 点处的密度估计由下式给出:
![\rho_K(y) = \sum_{i=1}^{N} K((y - x_i) / h)](img/8c8211edce4dbaeb44032f8d71a12135.jpg)
这里的带宽作为平滑参数,用来平衡结果中偏差和方差的值。 大的带宽会导致非常平滑(即高偏差)密度分布,而小的带宽则导致不平滑(即高方差)密度分布。
[`sklearn.neighbors.KernelDensity`](generated/sklearn.neighbors.KernelDensity.html#sklearn.neighbors.KernelDensity "sklearn.neighbors.KernelDensity") 实现了一些常见形状的核, 如下图所示:
**[![kde_kernels](img/f1fc9ca63c663059f76c2af6729189d1.jpg)](../auto_examples/neighbors/plot_kde_1d.html)**
这些核的形式如下所示:
* Gaussian kernel (`kernel = 'gaussian'`)
![K(x; h) \propto \exp(- \frac{x^2}{2h^2} )](img/a960dd9452e5e5da7b211f6fdc120a0d.jpg)
* Tophat kernel (`kernel = 'tophat'`)
![K(x; h) \propto 1](img/2a58cf81e4ccf9022d6033557d49aed5.jpg) if ![x &lt; h](img/7d1d547b1a46ad827caeb7f0e85b213d.jpg)
* Epanechnikov kernel (`kernel = 'epanechnikov'`)
![K(x; h) \propto 1 - \frac{x^2}{h^2}](img/088266f72839f4195c9058dfd17d778b.jpg)
* Exponential kernel (`kernel = 'exponential'`)
![K(x; h) \propto \exp(-x/h)](img/3944d32654b4bf939d248b496f950b10.jpg)
* Linear kernel (`kernel = 'linear'`)
![K(x; h) \propto 1 - x/h](img/81da9e05103270be5e500ebc67cd1b45.jpg) if ![x &lt; h](img/7d1d547b1a46ad827caeb7f0e85b213d.jpg)
* Cosine kernel (`kernel = 'cosine'`)
![K(x; h) \propto \cos(\frac{\pi x}{2h})](img/6b0371e0d33eebe00fe5ec3d59de5b43.jpg) 如果 ![x &lt; h](img/7d1d547b1a46ad827caeb7f0e85b213d.jpg)
核密度估计可以与任何有效的距离度量一起使用(可用度量列表请参见 [`sklearn.neighbors.DistanceMetric`](generated/sklearn.neighbors.DistanceMetric.html#sklearn.neighbors.DistanceMetric "sklearn.neighbors.DistanceMetric") ), 但其结果被适当地归一化处理,仅适用于欧几里德度量标准。 一个特别有用的度量是测量球体上的点与点之间角距离 的 [Haversine distance](https://en.wikipedia.org/wiki/Haversine_formula) 。 下面是使用核密度估计来对地理空间数据进行可视化的示例,本例中南美大陆两种不同物种的观测分布如图:
**[![species_kde](../Images/97b3ab2cc18a43b00c07a1cf6e118798.jpg)](../auto_examples/neighbors/plot_species_kde.html)**
核密度估计的另一个有用的应用是从数据集中学习出一个非参数生成模型,以便有效地从该生成模型中绘制新的样本。 以下是使用此过程创建一组新的手写数字的示例,使用的是高斯核对数据的 PCA 投影进行学习:
**[![digits_kde](img/de6303ad5b9808c7ae8c64ddc632d893.jpg)](../auto_examples/neighbors/plot_digits_kde_sampling.html)**
“新”数据由输入数据线性组合而成,其权重根据 KDE 模型按概率给出。
示例:
* [Simple 1D Kernel Density Estimation](../auto_examples/neighbors/plot_kde_1d.html#sphx-glr-auto-examples-neighbors-plot-kde-1d-py): 一维简单核密度估计的计算。
* [Kernel Density Estimation](../auto_examples/neighbors/plot_digits_kde_sampling.html#sphx-glr-auto-examples-neighbors-plot-digits-kde-sampling-py): 使用核密度估计来学习手写数字数据生成模型,以及使用该模型绘制新样本的示例
* [Kernel Density Estimate of Species Distributions](../auto_examples/neighbors/plot_species_kde.html#sphx-glr-auto-examples-neighbors-plot-species-kde-py): 使用Haversine距离度量来显示地理空间数据的核密度估计示例.
\ No newline at end of file
# 2.13\. 神经网络模型(无监督)
校验者:
        [@不将就](https://github.com/apachecn/scikit-learn-doc-zh)
翻译者:
        [@夜神月](https://github.com/apachecn/scikit-learn-doc-zh)
## 2.13.1\. 限制波尔兹曼机
Restricted Boltzmann machines (RBM)(限制玻尔兹曼机)是基于概率模型的无监督非线性特征学习器。当用 RBM 或 RBMs 中的层次结构提取的特征在馈入线性分类器(如线性支持向量机或感知机)时通常会获得良好的结果。
该模型对输入的分布作出假设。目前,scikit-learn 只提供了 [`BernoulliRBM`](generated/sklearn.neural_network.BernoulliRBM.html#sklearn.neural_network.BernoulliRBM "sklearn.neural_network.BernoulliRBM"),它假定输入是二值的,或者是 0 到 1 之间的值,每个值都编码特定特征被激活的概率。
RBM 尝试使用特定图形模型最大化数据的可能性。所使用的参数学习算法( [Stochastic Maximum Likelihood](#sml) (随机最大似然))要防止特征表示偏离输入数据,这使得它们能学习到有趣的特征,但使得该模型对于小数据集不太有用且通常对于密度估计无效。
该方法随着独立RBM的权重初始化深层神经网络而普及。这种方法被称为无监督的预训练(unsupervised pre-training)。
[![http://sklearn.apachecn.org/cn/0.19.0/_images/sphx_glr_plot_rbm_logistic_classification_0011.png](img/3af1cb0026efe5789c3acf3669ff5bc6.jpg)](../auto_examples/neural_networks/plot_rbm_logistic_classification.html)
示例:
* [Restricted Boltzmann Machine features for digit classification](../auto_examples/neural_networks/plot_rbm_logistic_classification.html#sphx-glr-auto-examples-neural-networks-plot-rbm-logistic-classification-py)
### 2.13.1.1\. 图形模型和参数化
RBM 的图形模型是一个全连接的二分图(fully-connected bipartite graph)。
![http://sklearn.apachecn.org/cn/0.19.0/_images/rbm_graph.png](img/0dd4aedd59a06ecbaead248c17a3ce80.jpg)
节点是随机变量,其状态取决于它连接到的其他节点的状态。 因此,为了简单起见,模型被参数化为连接的权重以及每个可见和隐藏单元的一个偏置项。 我们用能量函数衡量联合概率分布的质量:
![E(\mathbf{v}, \mathbf{h}) = \sum_i \sum_j w_{ij}v_ih_j + \sum_i b_iv_i
+ \sum_j c_jh_j](img/5959a6fe3c27570b7d474f26126eb628.jpg)
在上面的公式中, ![\mathbf{b}](img/4dee38783cbd4faef5d5639ce23a5c59.jpg) 和 ![\mathbf{c}](img/a4dd5119f3eeb13b99180aab64917975.jpg) 分别是可见层和隐藏层的偏置向量。 模型的联合概率是根据能量来定义的:
![P(\mathbf{v}, \mathbf{h}) = \frac{e^{-E(\mathbf{v}, \mathbf{h})}}{Z}](img/224162d8cfff6c8ad85718be6b261b70.jpg)
“限制”是指模型的二分图结构,它禁止隐藏单元之间或可见单元之间的直接交互。 这代表以下条件独立性成立:
![h_i \bot h_j | \mathbf{v} \\
v_i \bot v_j | \mathbf{h}](img/9521899a181a367c5873e61b9f7785ce.jpg)
二分图结构允许使用高效的块吉比斯采样(block Gibbs sampling)进行推断。
### 2.13.1.2\. 伯努利限制玻尔兹曼机
[`BernoulliRBM`](generated/sklearn.neural_network.BernoulliRBM.html#sklearn.neural_network.BernoulliRBM "sklearn.neural_network.BernoulliRBM") 中,所有单位都是二进制随机单元。 这意味着输入数据应该是二进制的,或者在 0 和 1 之间的实数值表示可见单元活跃或不活跃的概率。 这是一个很好的字符识别模型,其中的关注点是哪些像素是活跃的,哪些不是。 对于自然场景的图像,它不再适合,因为背景,深度和相邻像素的趋势取相同的值。
每个单位的条件概率分布由其接收的输入的sigmoid函数给出:
![P(v_i=1|\mathbf{h}) = \sigma(\sum_j w_{ij}h_j + b_i) \\
P(h_i=1|\mathbf{v}) = \sigma(\sum_i w_{ij}v_i + c_j)](img/e6811d3f6333e9490d602db8dc1e3d96.jpg)
其中 ![\sigma](img/8c4a5c99b21079b9fb1be49910ff96e3.jpg) 是Sigmoid函数:
![\sigma(x) = \frac{1}{1 + e^{-x}}](img/f1c8c9b812ae1b6ec189eda900e47269.jpg)
### 2.13.1.3\. 随机最大似然学习
[`BernoulliRBM`](generated/sklearn.neural_network.BernoulliRBM.html#sklearn.neural_network.BernoulliRBM "sklearn.neural_network.BernoulliRBM") 函数中实现的学习算法被称为随机最大似然(Stochastic Maximum Likelihood (SML))或持续对比发散(Persistent Contrastive Divergence (PCD))。由于数据的似然函数的形式,直接优化最大似然是不可行的:
![\log P(v) = \log \sum_h e^{-E(v, h)} - \log \sum_{x, y} e^{-E(x, y)}](img/1252aa7af065b0afd424b2ff01b4e2a5.jpg)
为了简单起见,上面的等式是针对单个训练样本所写的。相对于权重的梯度由对应于上述的两个项构成。根据它们的符号,它们通常被称为正梯度和负梯度。在这种实现中,按照小批量梯度(mini-batches of samples )对梯度进行计算。
在 maximizing the log-likelihood (最大化对数似然度)的情况下,正梯度使模型更倾向于与观察到的训练数据兼容的隐藏状态。由于 RBM 的二分体结构,可以有效地计算。然而,负梯度是棘手的。其目标是降低模型偏好的联合状态的能量,从而使数据保持真实。可以通过马尔可夫链蒙特卡罗近似,使用块 Gibbs 采样,通过迭代地对每个给定另一个的 ![v](img/ce7fd91ddb116ab5d747de0316caf657.jpg) 和 ![h](img/c5f49595b56010ad04fce358940848e5.jpg) 进行采样,直到链混合。以这种方式产生的样品有时被称为幻想粒子。这是无效的,很难确定马可夫链是否混合。
对比发散方法建议在经过少量迭代后停止链,![k](img/f93871977da52a6d11045d57c3e18728.jpg) 通常为 1.该方法快速且方差小,但样本远离模型分布。
持续的对比分歧解决这个问题。而不是每次需要梯度启动一个新的链,并且只执行一个 Gibbs 采样步骤,在 PCD 中,我们保留了在每个权重更新之后更新的 ![k](img/f93871977da52a6d11045d57c3e18728.jpg) Gibbs 步长的多个链(幻想粒子)。这使得颗粒更彻底地探索空间.
参考文献:
* [“A fast learning algorithm for deep belief nets”](http://www.cs.toronto.edu/~hinton/absps/fastnc.pdf) G. Hinton, S. Osindero, Y.-W. Teh, 2006
* [“Training Restricted Boltzmann Machines using Approximations to the Likelihood Gradient”](http://www.cs.toronto.edu/~tijmen/pcd/pcd.pdf) T. Tieleman, 2008
\ No newline at end of file
此差异已折叠。
......@@ -47,7 +47,7 @@ param_grid = [
示例:
* 有关在数字数据集上的网格搜索计算示例,请参阅 [基于交叉验证的网格搜索参数估计](http://sklearn.apachecn.org/cn/0.19.0/auto_examples/model_selection/plot_grid_search_digits.html#sphx-glr-auto-examples-model-selection-plot-grid-search-digits-py)
* 有关来自文本文档特征提取器(n-gram计数向量化器和TF-IDF变换器)的网格搜索耦合参数与分类器(这里是使用具有弹性网格的SGD训练的线性SVM 或L2惩罚)使用 &lt;cite&gt;pipeline.Pipeline&lt;/cite&gt; 示例,请参阅 [用于文本特征提取和评估的示例管道](http://sklearn.apachecn.org/cn/0.19.0/auto_examples/model_selection/grid_search_text_feature_extraction.html#sphx-glr-auto-examples-model-selection-grid-search-text-feature-extraction-py)
* 有关来自文本文档特征提取器(n-gram计数向量化器和TF-IDF变换器)的网格搜索耦合参数与分类器(这里是使用具有弹性网格的SGD训练的线性SVM 或L2惩罚)使用 *pipeline.Pipeline* 示例,请参阅 [用于文本特征提取和评估的示例管道](http://sklearn.apachecn.org/cn/0.19.0/auto_examples/model_selection/grid_search_text_feature_extraction.html#sphx-glr-auto-examples-model-selection-grid-search-text-feature-extraction-py)
* 有关iris数据集的交叉验证循环中的网格搜索示例, 请参阅 [嵌套与非嵌套交叉验证](http://sklearn.apachecn.org/cn/0.19.0/auto_examples/model_selection/plot_nested_cross_validation_iris.html#sphx-glr-auto-examples-model-selection-plot-nested-cross-validation-iris-py)
* 有关用于同时评估多个指标的GridSearchCV示例,请参阅 [cross_val_score 与 GridSearchCV 多指标评价的实证研究](http://sklearn.apachecn.org/cn/0.19.0/auto_examples/model_selection/plot_multi_metric_evaluation.html#sphx-glr-auto-examples-model-selection-plot-multi-metric-evaluation-py)
......@@ -128,16 +128,16 @@ param_grid = [
以下是这些模型的列表:
| [`linear_model.ElasticNetCV`](generated/sklearn.linear_model.ElasticNetCV.html#sklearn.linear_model.ElasticNetCV "sklearn.linear_model.ElasticNetCV")([l1_ratio, eps, …]) | Elastic Net model with iterative fitting along a regularization path |
| [`linear_model.LarsCV`](generated/sklearn.linear_model.LarsCV.html#sklearn.linear_model.LarsCV "sklearn.linear_model.LarsCV")([fit_intercept, …]) | Cross-validated Least Angle Regression model |
| [`linear_model.LassoCV`](generated/sklearn.linear_model.LassoCV.html#sklearn.linear_model.LassoCV "sklearn.linear_model.LassoCV")([eps, n_alphas, …]) | Lasso linear model with iterative fitting along a regularization path |
| [`linear_model.LassoLarsCV`](generated/sklearn.linear_model.LassoLarsCV.html#sklearn.linear_model.LassoLarsCV "sklearn.linear_model.LassoLarsCV")([fit_intercept, …]) | Cross-validated Lasso, using the LARS algorithm |
| [`linear_model.LogisticRegressionCV`](generated/sklearn.linear_model.LogisticRegressionCV.html#sklearn.linear_model.LogisticRegressionCV "sklearn.linear_model.LogisticRegressionCV")([Cs, …]) | Logistic Regression CV (aka logit, MaxEnt) classifier. |
| [`linear_model.MultiTaskElasticNetCV`](generated/sklearn.linear_model.MultiTaskElasticNetCV.html#sklearn.linear_model.MultiTaskElasticNetCV "sklearn.linear_model.MultiTaskElasticNetCV")([…]) | Multi-task L1/L2 ElasticNet with built-in cross-validation. |
| [`linear_model.MultiTaskLassoCV`](generated/sklearn.linear_model.MultiTaskLassoCV.html#sklearn.linear_model.MultiTaskLassoCV "sklearn.linear_model.MultiTaskLassoCV")([eps, …]) | Multi-task L1/L2 Lasso with built-in cross-validation. |
| [`linear_model.OrthogonalMatchingPursuitCV`](generated/sklearn.linear_model.OrthogonalMatchingPursuitCV.html#sklearn.linear_model.OrthogonalMatchingPursuitCV "sklearn.linear_model.OrthogonalMatchingPursuitCV")([…]) | Cross-validated Orthogonal Matching Pursuit model (OMP) |
| [`linear_model.RidgeCV`](generated/sklearn.linear_model.RidgeCV.html#sklearn.linear_model.RidgeCV "sklearn.linear_model.RidgeCV")([alphas, …]) | Ridge regression with built-in cross-validation. |
| [`linear_model.RidgeClassifierCV`](generated/sklearn.linear_model.RidgeClassifierCV.html#sklearn.linear_model.RidgeClassifierCV "sklearn.linear_model.RidgeClassifierCV")([alphas, …]) | Ridge classifier with built-in cross-validation. |
| [`linear_model.ElasticNetCV`](https://scikit-learn.org/stable/modules/generated/sklearn.linear_model.ElasticNetCV.html#sklearn.linear_model.ElasticNetCV "sklearn.linear_model.ElasticNetCV")([l1_ratio, eps, …]) | Elastic Net model with iterative fitting along a regularization path |
| [`linear_model.LarsCV`](https://scikit-learn.org/stable/modules/generated/sklearn.linear_model.LarsCV.html#sklearn.linear_model.LarsCV "sklearn.linear_model.LarsCV")([fit_intercept, …]) | Cross-validated Least Angle Regression model |
| [`linear_model.LassoCV`](https://scikit-learn.org/stable/modules/generated/sklearn.linear_model.LassoCV.html#sklearn.linear_model.LassoCV "sklearn.linear_model.LassoCV")([eps, n_alphas, …]) | Lasso linear model with iterative fitting along a regularization path |
| [`linear_model.LassoLarsCV`](https://scikit-learn.org/stable/modules/generated/sklearn.linear_model.LassoLarsCV.html#sklearn.linear_model.LassoLarsCV "sklearn.linear_model.LassoLarsCV")([fit_intercept, …]) | Cross-validated Lasso, using the LARS algorithm |
| [`linear_model.LogisticRegressionCV`](https://scikit-learn.org/stable/modules/generated/sklearn.linear_model.LogisticRegressionCV.html#sklearn.linear_model.LogisticRegressionCV "sklearn.linear_model.LogisticRegressionCV")([Cs, …]) | Logistic Regression CV (aka logit, MaxEnt) classifier. |
| [`linear_model.MultiTaskElasticNetCV`](https://scikit-learn.org/stable/modules/generated/sklearn.linear_model.MultiTaskElasticNetCV.html#sklearn.linear_model.MultiTaskElasticNetCV "sklearn.linear_model.MultiTaskElasticNetCV")([…]) | Multi-task L1/L2 ElasticNet with built-in cross-validation. |
| [`linear_model.MultiTaskLassoCV`](https://scikit-learn.org/stable/modules/generated/sklearn.linear_model.MultiTaskLassoCV.html#sklearn.linear_model.MultiTaskLassoCV "sklearn.linear_model.MultiTaskLassoCV")([eps, …]) | Multi-task L1/L2 Lasso with built-in cross-validation. |
| [`linear_model.OrthogonalMatchingPursuitCV`](https://scikit-learn.org/stable/modules/generated/sklearn.linear_model.OrthogonalMatchingPursuitCV.html#sklearn.linear_model.OrthogonalMatchingPursuitCV "sklearn.linear_model.OrthogonalMatchingPursuitCV")([…]) | Cross-validated Orthogonal Matching Pursuit model (OMP) |
| [`linear_model.RidgeCV`](https://scikit-learn.org/stable/modules/generated/sklearn.linear_model.RidgeCV.html#sklearn.linear_model.RidgeCV "sklearn.linear_model.RidgeCV")([alphas, …]) | Ridge regression with built-in cross-validation. |
| [`linear_model.RidgeClassifierCV`](https://scikit-learn.org/stable/modules/generated/sklearn.linear_model.RidgeClassifierCV.html#sklearn.linear_model.RidgeClassifierCV "sklearn.linear_model.RidgeClassifierCV")([alphas, …]) | Ridge classifier with built-in cross-validation. |
### 3.2.4.2\. 信息标准
......@@ -145,7 +145,7 @@ param_grid = [
以下是从 Akaike 信息标准 (AIC) 或贝叶斯信息标准 (可用于自动选择模型) 中受益的模型列表:
| [`linear_model.LassoLarsIC`](generated/sklearn.linear_model.LassoLarsIC.html#sklearn.linear_model.LassoLarsIC "sklearn.linear_model.LassoLarsIC")([criterion, …]) | Lasso model fit with Lars using BIC or AIC for model selection |
| [`linear_model.LassoLarsIC`](https://scikit-learn.org/stable/modules/generated/sklearn.linear_model.LassoLarsIC.html#sklearn.linear_model.LassoLarsIC "sklearn.linear_model.LassoLarsIC")([criterion, …]) | Lasso model fit with Lars using BIC or AIC for model selection |
### 3.2.4.3\. 出袋估计
......@@ -155,9 +155,9 @@ param_grid = [
目前该方法已经实现的类以下几个:
| [`ensemble.RandomForestClassifier`](generated/sklearn.ensemble.RandomForestClassifier.html#sklearn.ensemble.RandomForestClassifier "sklearn.ensemble.RandomForestClassifier")([…]) | A random forest classifier. |
| [`ensemble.RandomForestRegressor`](generated/sklearn.ensemble.RandomForestRegressor.html#sklearn.ensemble.RandomForestRegressor "sklearn.ensemble.RandomForestRegressor")([…]) | A random forest regressor. |
| [`ensemble.ExtraTreesClassifier`](generated/sklearn.ensemble.ExtraTreesClassifier.html#sklearn.ensemble.ExtraTreesClassifier "sklearn.ensemble.ExtraTreesClassifier")([…]) | An extra-trees classifier. |
| [`ensemble.ExtraTreesRegressor`](generated/sklearn.ensemble.ExtraTreesRegressor.html#sklearn.ensemble.ExtraTreesRegressor "sklearn.ensemble.ExtraTreesRegressor")([n_estimators, …]) | An extra-trees regressor. |
| [`ensemble.GradientBoostingClassifier`](generated/sklearn.ensemble.GradientBoostingClassifier.html#sklearn.ensemble.GradientBoostingClassifier "sklearn.ensemble.GradientBoostingClassifier")([loss, …]) | Gradient Boosting for classification. |
| [`ensemble.GradientBoostingRegressor`](generated/sklearn.ensemble.GradientBoostingRegressor.html#sklearn.ensemble.GradientBoostingRegressor "sklearn.ensemble.GradientBoostingRegressor")([loss, …]) | Gradient Boosting for regression. |
\ No newline at end of file
| [`ensemble.RandomForestClassifier`](https://scikit-learn.org/stable/modules/generated/sklearn.ensemble.RandomForestClassifier.html#sklearn.ensemble.RandomForestClassifier "sklearn.ensemble.RandomForestClassifier")([…]) | A random forest classifier. |
| [`ensemble.RandomForestRegressor`](https://scikit-learn.org/stable/modules/generated/sklearn.ensemble.RandomForestRegressor.html#sklearn.ensemble.RandomForestRegressor "sklearn.ensemble.RandomForestRegressor")([…]) | A random forest regressor. |
| [`ensemble.ExtraTreesClassifier`](https://scikit-learn.org/stable/modules/generated/sklearn.ensemble.ExtraTreesClassifier.html#sklearn.ensemble.ExtraTreesClassifier "sklearn.ensemble.ExtraTreesClassifier")([…]) | An extra-trees classifier. |
| [`ensemble.ExtraTreesRegressor`](https://scikit-learn.org/stable/modules/generated/sklearn.ensemble.ExtraTreesRegressor.html#sklearn.ensemble.ExtraTreesRegressor "sklearn.ensemble.ExtraTreesRegressor")([n_estimators, …]) | An extra-trees regressor. |
| [`ensemble.GradientBoostingClassifier`](https://scikit-learn.org/stable/modules/generated/sklearn.ensemble.GradientBoostingClassifier.html#sklearn.ensemble.GradientBoostingClassifier "sklearn.ensemble.GradientBoostingClassifier")([loss, …]) | Gradient Boosting for classification. |
| [`ensemble.GradientBoostingRegressor`](https://scikit-learn.org/stable/modules/generated/sklearn.ensemble.GradientBoostingRegressor.html#sklearn.ensemble.GradientBoostingRegressor "sklearn.ensemble.GradientBoostingRegressor")([loss, …]) | Gradient Boosting for regression. |
\ No newline at end of file
此差异已折叠。
......@@ -11,7 +11,7 @@
在下面的图中,我们可以看到一个函数 [:math:`f(x) = \cos (\frac{3}{2} \pi x)`_](#id4) 和这个函数的一些噪声样本。 我们用三个不同的估计来拟合函数: 多项式特征为1,4和15的线性回归。我们看到,第一个估计最多只能为样本和真正的函数提供一个很差的拟合 ,因为它太简单了(高偏差),第二个估计几乎完全近似,最后一个估计完全接近训练数据, 但不能很好地拟合真实的函数,即对训练数据的变化(高方差)非常敏感。
[![http://sklearn.apachecn.org/cn/0.19.0/_images/sphx_glr_plot_underfitting_overfitting_0011.png](img/a6509a4f7b1838eb6d76d30036a00ffd.jpg)](../auto_examples/model_selection/plot_underfitting_overfitting.html)
[![http://sklearn.apachecn.org/cn/0.19.0/_images/sphx_glr_plot_underfitting_overfitting_0011.png](img/a6509a4f7b1838eb6d76d30036a00ffd.jpg)](https://scikit-learn.org/stable/auto_examples/model_selection/plot_underfitting_overfitting.html)
偏差和方差是估计所固有的属性,我们通常必须选择合适的学习算法和超参数,以使得偏差和 方差都尽可能的低(参见偏差-方差困境 [`Bias-variance dilemma&lt;https://en.wikipedia.org/wiki/Bias-variance_dilemma&gt;`_](#id10))。 另一种降低方差的方法是使用更多的训练数据。不论如何,如果真实函数过于复杂并且不能用一个方 差较小的估计值来近似,则只能去收集更多的训练数据。
......@@ -19,9 +19,9 @@
示例:
* [Underfitting vs. Overfitting](../auto_examples/model_selection/plot_underfitting_overfitting.html#sphx-glr-auto-examples-model-selection-plot-underfitting-overfitting-py)
* [Plotting Validation Curves](../auto_examples/model_selection/plot_validation_curve.html#sphx-glr-auto-examples-model-selection-plot-validation-curve-py)
* [Plotting Learning Curves](../auto_examples/model_selection/plot_learning_curve.html#sphx-glr-auto-examples-model-selection-plot-learning-curve-py)
* [Underfitting vs. Overfitting](https://scikit-learn.org/stable/auto_examples/model_selection/plot_underfitting_overfitting.html#sphx-glr-auto-examples-model-selection-plot-underfitting-overfitting-py)
* [Plotting Validation Curves](https://scikit-learn.org/stable/auto_examples/model_selection/plot_validation_curve.html#sphx-glr-auto-examples-model-selection-plot-validation-curve-py)
* [Plotting Learning Curves](https://scikit-learn.org/stable/auto_examples/model_selection/plot_learning_curve.html#sphx-glr-auto-examples-model-selection-plot-learning-curve-py)
## 3.5.1\. 验证曲线
......@@ -29,7 +29,7 @@
然而,绘制单个超参数对训练分数和验证分数的影响,有时有助于发现该估计是否因为某些超参数的值 而出现过拟合或欠拟合。
本例中,下面的方程 [`validation_curve`](generated/sklearn.model_selection.validation_curve.html#sklearn.model_selection.validation_curve "sklearn.model_selection.validation_curve") 能起到如下作用:
本例中,下面的方程 [`validation_curve`](https://scikit-learn.org/stable/modules/generated/sklearn.model_selection.validation_curve.html#sklearn.model_selection.validation_curve "sklearn.model_selection.validation_curve") 能起到如下作用:
```py
>>> import numpy as np
......@@ -65,17 +65,17 @@ array([[ 0.90..., 0.92..., 0.94...],
如果训练得分和验证得分都很低,则估计器是不合适的。如果训练得分高,验证得分低,则估计器过拟合, 否则估计会拟合得很好。通常不可能有较低的训练得分和较高的验证得分。所有三种情况都可以 在下面的图中找到,其中我们改变了数字数据集上 SVM 的参数 ![\gamma](img/6552bde3d3999c1a9728016416932af7.jpg)
[![http://sklearn.apachecn.org/cn/0.19.0/_images/sphx_glr_plot_validation_curve_0011.png](img/d49a868d9c8c944f42202a30647ff7cf.jpg)](../auto_examples/model_selection/plot_validation_curve.html)
[![http://sklearn.apachecn.org/cn/0.19.0/_images/sphx_glr_plot_validation_curve_0011.png](img/d49a868d9c8c944f42202a30647ff7cf.jpg)](https://scikit-learn.org/stable/auto_examples/model_selection/plot_validation_curve.html)
## 3.5.2\. 学习曲线
学习曲线显示了对于不同数量的训练样本的估计器的验证和训练评分。它可以帮助我们发现从增加更多的训 练数据中能获益多少,以及估计是否受到更多来自方差误差或偏差误差的影响。如果在增加训练集大小时,验证分数和训练 分数都收敛到一个很低的值,那么我们将不会从更多的训练数据中获益。在下面的图中看到一个例子:朴素贝叶斯大致收敛到一个较低的分数。
[![http://sklearn.apachecn.org/cn/0.19.0/_images/sphx_glr_plot_learning_curve_0011.png](img/a70da26e29baa95b2f65f3cbc2e5fbfa.jpg)](../auto_examples/model_selection/plot_learning_curve.html)
[![http://sklearn.apachecn.org/cn/0.19.0/_images/sphx_glr_plot_learning_curve_0011.png](img/a70da26e29baa95b2f65f3cbc2e5fbfa.jpg)](https://scikit-learn.org/stable/auto_examples/model_selection/plot_learning_curve.html)
我们可能需要使用评估器或者一个当前评估器的参数化形式来学习更复杂概念(例如有一个较低的偏差)。 如果训练样本的最大时,训练分数比验证分数得分大得多,那么增加训练样本很可能会增加泛化能力。 在下面的图中,可以看到支持向量机(SVM)可以从更多的训练样本中获益。
[![http://sklearn.apachecn.org/cn/0.19.0/_images/sphx_glr_plot_learning_curve_0021.png](img/86d43b01a4c13ac9d69265ff0d546720.jpg)](../auto_examples/model_selection/plot_learning_curve.html)
[![http://sklearn.apachecn.org/cn/0.19.0/_images/sphx_glr_plot_learning_curve_0021.png](img/86d43b01a4c13ac9d69265ff0d546720.jpg)](https://scikit-learn.org/stable/auto_examples/model_selection/plot_learning_curve.html)
我们可以使用:func:[`](#id8)learning_curve`函数来绘制这样一个学习曲线所需的值(已使用的样本数量,训练集 上的平均分数和验证集上的平均分数):
......
......@@ -2,21 +2,25 @@
校验者:
        [@不吃曲奇的趣多多](https://github.com/apachecn/scikit-learn-doc-zh)
        [@Loopy](https://github.com/loopyme)
翻译者:
        [@Counting stars](https://github.com/apachecn/scikit-learn-doc-zh)
Kernel ridge regression (KRR) (内核岭回归)[M2012]_ 由 使用内核方法的 :ref:[`](#id2)ridge_regression`(岭回归)(使用 l2 正则化的最小二乘法)所组成。因此,它所学习到的在空间中不同的线性函数是由不同的内核和数据所导致的。对于非线性的内核,它与原始空间中的非线性函数相对应。
内核岭回归(Kernel ridge regression-KRR)[1] 由使用内核方法的[岭回归](/docs/2?id=_112-岭回归)(使用 l2 正则化的最小二乘法)所组成。因此,它所拟合到的在空间中不同的线性函数是由不同的内核和数据所导致的。对于非线性的内核,它与原始空间中的非线性函数相对应。
由 [`KernelRidge`](generated/sklearn.kernel_ridge.KernelRidge.html#sklearn.kernel_ridge.KernelRidge "sklearn.kernel_ridge.KernelRidge") 学习的模型的形式与支持向量回归( `SVR` ) 是一样的。但是他们使用不同的损失函数:内核岭回归(KRR)使用 squared error loss (平方误差损失函数)而 support vector regression (支持向量回归)(SVR)使用 ![\epsilon](img/58ef9e1b5d2ee139dcb588a3879ca1a6.jpg)-insensitive loss ( ε-不敏感损失 ),两者都使用 l2 regularization (l2 正则化)。与 `SVR` 相反,拟合 [`KernelRidge`](generated/sklearn.kernel_ridge.KernelRidge.html#sklearn.kernel_ridge.KernelRidge "sklearn.kernel_ridge.KernelRidge") 可以以 closed-form (封闭形式)完成,对于中型数据集通常更快。另一方面,学习的模型是非稀疏的,因此比 SVR 慢, 在预测时间,SVR 学习了:math:&lt;cite&gt;epsilon &gt; 0&lt;/cite&gt; 的稀疏模型。
[`KernelRidge`](https://scikit-learn.org/stable/modules/generated/sklearn.kernel_ridge.KernelRidge.html) 学习的模型的形式与支持向量回归( `SVR` ) 是一样的。但是他们使用不同的损失函数:内核岭回归(KRR)使用 squared error loss (平方误差损失函数)而 support vector regression (支持向量回归)(SVR)使用 ![\epsilon](img/58ef9e1b5d2ee139dcb588a3879ca1a6.jpg)-insensitive loss ( ε-不敏感损失 ),两者都使用 l2 regularization (l2 正则化)。与 `SVR` 相反,拟合 [`KernelRidge`](https://scikit-learn.org/stable/modules/generated/sklearn.kernel_ridge.KernelRidge.html) 可以以 closed-form (封闭形式)完成,对于中型数据集通常更快。另一方面,学习的模型是非稀疏的,因此比 SVR 慢。在预测时间内,SVR 拟合的是ε>0的稀疏模型。
下图比较了人造数据集上的 [`KernelRidge`](generated/sklearn.kernel_ridge.KernelRidge.html#sklearn.kernel_ridge.KernelRidge "sklearn.kernel_ridge.KernelRidge") 和 `SVR` 的区别,它由一个正弦目标函数和每五个数据点产生一个强噪声组成。图中分别绘制了由 [`KernelRidge`](generated/sklearn.kernel_ridge.KernelRidge.html#sklearn.kernel_ridge.KernelRidge "sklearn.kernel_ridge.KernelRidge") 和 `SVR` 学习到的回归曲线。两者都使用网格搜索优化了 RBF 内核的 complexity/regularization (复杂性/正则化)和 bandwidth (带宽)。它们的 learned functions (学习函数)非常相似;但是,拟合 [`KernelRidge`](generated/sklearn.kernel_ridge.KernelRidge.html#sklearn.kernel_ridge.KernelRidge "sklearn.kernel_ridge.KernelRidge") 大约比拟合 `SVR` 快七倍(都使用 grid-search ( 网格搜索 ) )。然而,由于 SVR 只学习了一个稀疏模型,所以 SVR 预测 10 万个目标值比使用 KernelRidge 快三倍以上。SVR 只使用了百分之三十的数据点做为支撑向量。
下图比较了人造数据集上的 [`KernelRidge`](https://scikit-learn.org/stable/modules/generated/sklearn.kernel_ridge.KernelRidge.html)`SVR` 的区别,它由一个正弦目标函数和每五个数据点产生一个强噪声组成。图中分别绘制了由 [`KernelRidge`](https://scikit-learn.org/stable/modules/generated/sklearn.kernel_ridge.KernelRidge.html)`SVR` 学习到的回归曲线。两者都使用网格搜索优化了 RBF 内核的 complexity/regularization (复杂性/正则化)和 bandwidth (带宽)。它们的 learned functions (学习函数)非常相似;但是,拟合 [`KernelRidge`](https://scikit-learn.org/stable/modules/generated/sklearn.kernel_ridge.KernelRidge.html) 大约比拟合 `SVR` 快七倍(都使用 grid-search ( 网格搜索 ) )。然而,由于 SVR 只学习了一个稀疏模型,所以 SVR 预测 10 万个目标值比使用 KernelRidge 快三倍以上。SVR 只使用了百分之三十的数据点做为支撑向量。
[![http://sklearn.apachecn.org/cn/0.19.0/_images/sphx_glr_plot_kernel_ridge_regression_0011.png](img/5d71bcc9676eddcac89936397b2cd79c.jpg)](../auto_examples/plot_kernel_ridge_regression.html)
[![http://sklearn.apachecn.org/cn/0.19.0/_images/sphx_glr_plot_kernel_ridge_regression_0011.png](img/5d71bcc9676eddcac89936397b2cd79c.jpg)](https://scikit-learn.org/stable/auto_examples/plot_kernel_ridge_regression.html)
下图显示不同大小训练集的 [`KernelRidge`](generated/sklearn.kernel_ridge.KernelRidge.html#sklearn.kernel_ridge.KernelRidge "sklearn.kernel_ridge.KernelRidge") 和 `SVR` 的 fitting (拟合)和 prediction (预测)时间。 对于中型训练集(小于 1000 个样本),拟合 [`KernelRidge`](generated/sklearn.kernel_ridge.KernelRidge.html#sklearn.kernel_ridge.KernelRidge "sklearn.kernel_ridge.KernelRidge") 比 `SVR` 快; 然而,对于更大的训练集 `SVR` 通常更好。 关于预测时间,由于学习的稀疏解,`SVR` 对于所有不同大小的训练集都比 [`KernelRidge`](generated/sklearn.kernel_ridge.KernelRidge.html#sklearn.kernel_ridge.KernelRidge "sklearn.kernel_ridge.KernelRidge") 快。 注意,稀疏度和预测时间取决于 `SVR` 的参数 ![\epsilon](img/58ef9e1b5d2ee139dcb588a3879ca1a6.jpg) 和 ![C](img/4b6d782a67ac392e97215c46b7590bf7.jpg) ; ![\epsilon = 0](img/1d9a1eb9200948482ebfd5811679276a.jpg) 将对应于密集模型。
下图显示不同大小训练集的 [`KernelRidge`](https://scikit-learn.org/stable/modules/generated/sklearn.kernel_ridge.KernelRidge.html)`SVR` 的 fitting (拟合)和 prediction (预测)时间。 对于中型训练集(小于 1000 个样本),拟合 [`KernelRidge`](https://scikit-learn.org/stable/modules/generated/sklearn.kernel_ridge.KernelRidge.html)`SVR` 快; 然而,对于更大的训练集 `SVR` 通常更好。 关于预测时间,由于学习的稀疏解,`SVR` 对于所有不同大小的训练集都比 [`KernelRidge`](https://scikit-learn.org/stable/modules/generated/sklearn.kernel_ridge.KernelRidge.html) 快。 注意,稀疏度和预测时间取决于 `SVR` 的参数 ![\epsilon](img/58ef9e1b5d2ee139dcb588a3879ca1a6.jpg) 和 ![C](img/4b6d782a67ac392e97215c46b7590bf7.jpg) ; ![\epsilon = 0](img/1d9a1eb9200948482ebfd5811679276a.jpg) 将对应于密集模型。
[![http://sklearn.apachecn.org/cn/0.19.0/_images/sphx_glr_plot_kernel_ridge_regression_0021.png](img/a921f24886997e2a5b94e2abb46d8402.jpg)](../auto_examples/plot_kernel_ridge_regression.html)
[![http://sklearn.apachecn.org/cn/0.19.0/_images/sphx_glr_plot_kernel_ridge_regression_0021.png](img/a921f24886997e2a5b94e2abb46d8402.jpg)](https://scikit-learn.org/stable/auto_examples/plot_kernel_ridge_regression.html)
参考:
> 示例:
>
>* [内核岭回归与SVR的对比](https://scikit-learn.org/stable/auto_examples/plot_kernel_ridge_regression.html)
| [M2012] | “Machine Learning: A Probabilistic Perspective” Murphy, K. P. - chapter 14.4.3, pp. 492-493, The MIT Press, 2012 |
\ No newline at end of file
> 参考:
>* [1] “Machine Learning: A Probabilistic Perspective” Murphy, K. P. - chapter 14.4.3, pp. 492-493, The MIT Press, 2012
此差异已折叠。
......@@ -11,7 +11,7 @@
可以把多个评估器链接成一个这个是很有用的因为处理数据的步骤一般都是固定的例如特征选择标准化和分类
```
[`Pipeline`](generated/sklearn.pipeline.Pipeline.html#sklearn.pipeline.Pipeline "sklearn.pipeline.Pipeline") 主要有两个目的:
[`Pipeline`](https://scikit-learn.org/stable/modules/generated/sklearn.pipeline.Pipeline.html#sklearn.pipeline.Pipeline "sklearn.pipeline.Pipeline") 主要有两个目的:
```py
便捷性和封装性
......@@ -35,7 +35,7 @@
### 4.1.1.1\. 用法
> [`Pipeline`](generated/sklearn.pipeline.Pipeline.html#sklearn.pipeline.Pipeline "sklearn.pipeline.Pipeline") 使用一系列 `(key, value)` 键值对来构建,其中 `key` 是你给这个步骤起的名字, `value` 是一个评估器对象:
> [`Pipeline`](https://scikit-learn.org/stable/modules/generated/sklearn.pipeline.Pipeline.html#sklearn.pipeline.Pipeline "sklearn.pipeline.Pipeline") 使用一系列 `(key, value)` 键值对来构建,其中 `key` 是你给这个步骤起的名字, `value` 是一个评估器对象:
>
> ```py
> &gt;&gt;&gt; from sklearn.pipeline import Pipeline
......@@ -50,7 +50,7 @@
>
> ```
功能函数 [`make_pipeline`](generated/sklearn.pipeline.make_pipeline.html#sklearn.pipeline.make_pipeline "sklearn.pipeline.make_pipeline") 是构建管道的缩写; 它接收多个评估器并返回一个管道,自动填充评估器名:
功能函数 [`make_pipeline`](https://scikit-learn.org/stable/modules/generated/sklearn.pipeline.make_pipeline.html#sklearn.pipeline.make_pipeline "sklearn.pipeline.make_pipeline") 是构建管道的缩写; 它接收多个评估器并返回一个管道,自动填充评估器名:
```py
>>> from sklearn.pipeline import make_pipeline
......@@ -124,12 +124,12 @@ True
例子:
* [Pipeline Anova SVM](../auto_examples/feature_selection/plot_feature_selection_pipeline.html#sphx-glr-auto-examples-feature-selection-plot-feature-selection-pipeline-py)
* [Sample pipeline for text feature extraction and evaluation](../auto_examples/model_selection/grid_search_text_feature_extraction.html#sphx-glr-auto-examples-model-selection-grid-search-text-feature-extraction-py)
* [Pipelining: chaining a PCA and a logistic regression](../auto_examples/plot_digits_pipe.html#sphx-glr-auto-examples-plot-digits-pipe-py)
* [Explicit feature map approximation for RBF kernels](../auto_examples/plot_kernel_approximation.html#sphx-glr-auto-examples-plot-kernel-approximation-py)
* [SVM-Anova: SVM with univariate feature selection](../auto_examples/svm/plot_svm_anova.html#sphx-glr-auto-examples-svm-plot-svm-anova-py)
* [Selecting dimensionality reduction with Pipeline and GridSearchCV](../auto_examples/plot_compare_reduction.html#sphx-glr-auto-examples-plot-compare-reduction-py)
* [Pipeline Anova SVM](https://scikit-learn.org/stable/auto_examples/feature_selection/plot_feature_selection_pipeline.html#sphx-glr-auto-examples-feature-selection-plot-feature-selection-pipeline-py)
* [Sample pipeline for text feature extraction and evaluation](https://scikit-learn.org/stable/auto_examples/model_selection/grid_search_text_feature_extraction.html#sphx-glr-auto-examples-model-selection-grid-search-text-feature-extraction-py)
* [Pipelining: chaining a PCA and a logistic regression](https://scikit-learn.org/stable/auto_examples/plot_digits_pipe.html#sphx-glr-auto-examples-plot-digits-pipe-py)
* [Explicit feature map approximation for RBF kernels](https://scikit-learn.org/stable/auto_examples/plot_kernel_approximation.html#sphx-glr-auto-examples-plot-kernel-approximation-py)
* [SVM-Anova: SVM with univariate feature selection](https://scikit-learn.org/stable/auto_examples/svm/plot_svm_anova.html#sphx-glr-auto-examples-svm-plot-svm-anova-py)
* [Selecting dimensionality reduction with Pipeline and GridSearchCV](https://scikit-learn.org/stable/auto_examples/plot_compare_reduction.html#sphx-glr-auto-examples-plot-compare-reduction-py)
也可以参阅:
......@@ -137,11 +137,11 @@ True
### 4.1.1.2\. 注意点
对管道调用 `fit` 方法的效果跟依次对每个评估器调用 `fit` 方法一样, 都是``transform`` 输入并传递给下个步骤。 管道中最后一个评估器的所有方法,管道都有,例如,如果最后的评估器是一个分类器, [`Pipeline`](generated/sklearn.pipeline.Pipeline.html#sklearn.pipeline.Pipeline "sklearn.pipeline.Pipeline") 可以当做分类器来用。如果最后一个评估器是转换器,管道也一样可以。
对管道调用 `fit` 方法的效果跟依次对每个评估器调用 `fit` 方法一样, 都是``transform`` 输入并传递给下个步骤。 管道中最后一个评估器的所有方法,管道都有,例如,如果最后的评估器是一个分类器, [`Pipeline`](https://scikit-learn.org/stable/modules/generated/sklearn.pipeline.Pipeline.html#sklearn.pipeline.Pipeline "sklearn.pipeline.Pipeline") 可以当做分类器来用。如果最后一个评估器是转换器,管道也一样可以。
### 4.1.1.3\. 缓存转换器:避免重复计算
适配转换器是很耗费计算资源的。设置了``memory`` 参数, [`Pipeline`](generated/sklearn.pipeline.Pipeline.html#sklearn.pipeline.Pipeline "sklearn.pipeline.Pipeline") 将会在调用``fit``方法后缓存每个转换器。 如果参数和输入数据相同,这个特征用于避免重复计算适配的转换器。典型的例子是网格搜索转换器,该转化器只要适配一次就可以多次使用。
适配转换器是很耗费计算资源的。设置了``memory`` 参数, [`Pipeline`](https://scikit-learn.org/stable/modules/generated/sklearn.pipeline.Pipeline.html#sklearn.pipeline.Pipeline "sklearn.pipeline.Pipeline") 将会在调用``fit``方法后缓存每个转换器。 如果参数和输入数据相同,这个特征用于避免重复计算适配的转换器。典型的例子是网格搜索转换器,该转化器只要适配一次就可以多次使用。
> `memory` 参数用于缓存转换器。
......@@ -169,7 +169,7 @@ Warning
**Side effect of caching transfomers**
使用 [`Pipeline`](generated/sklearn.pipeline.Pipeline.html#sklearn.pipeline.Pipeline "sklearn.pipeline.Pipeline") 而不开启缓存功能,还是可以通过查看原始实例的,例如:
使用 [`Pipeline`](https://scikit-learn.org/stable/modules/generated/sklearn.pipeline.Pipeline.html#sklearn.pipeline.Pipeline "sklearn.pipeline.Pipeline") 而不开启缓存功能,还是可以通过查看原始实例的,例如:
```py
>>> from sklearn.datasets import load_digits
......@@ -209,21 +209,21 @@ Pipeline(memory=None,
例子:
* [Selecting dimensionality reduction with Pipeline and GridSearchCV](../auto_examples/plot_compare_reduction.html#sphx-glr-auto-examples-plot-compare-reduction-py)
* [Selecting dimensionality reduction with Pipeline and GridSearchCV](https://scikit-learn.org/stable/auto_examples/plot_compare_reduction.html#sphx-glr-auto-examples-plot-compare-reduction-py)
## 4.1.2\. FeatureUnion(特征联合): 个特征层面
[`FeatureUnion`](generated/sklearn.pipeline.FeatureUnion.html#sklearn.pipeline.FeatureUnion "sklearn.pipeline.FeatureUnion") 合并了多个转换器对象形成一个新的转换器,该转换器合并了他们的输出。一个 [`FeatureUnion`](generated/sklearn.pipeline.FeatureUnion.html#sklearn.pipeline.FeatureUnion "sklearn.pipeline.FeatureUnion") 可以接收多个转换器对象。在适配期间,每个转换器都单独的和数据适配。 对于转换数据,转换器可以并发使用,且输出的样本向量被连接成更大的向量。
[`FeatureUnion`](https://scikit-learn.org/stable/modules/generated/sklearn.pipeline.FeatureUnion.html#sklearn.pipeline.FeatureUnion "sklearn.pipeline.FeatureUnion") 合并了多个转换器对象形成一个新的转换器,该转换器合并了他们的输出。一个 [`FeatureUnion`](https://scikit-learn.org/stable/modules/generated/sklearn.pipeline.FeatureUnion.html#sklearn.pipeline.FeatureUnion "sklearn.pipeline.FeatureUnion") 可以接收多个转换器对象。在适配期间,每个转换器都单独的和数据适配。 对于转换数据,转换器可以并发使用,且输出的样本向量被连接成更大的向量。
[`FeatureUnion`](generated/sklearn.pipeline.FeatureUnion.html#sklearn.pipeline.FeatureUnion "sklearn.pipeline.FeatureUnion") 功能与 [`Pipeline`](generated/sklearn.pipeline.Pipeline.html#sklearn.pipeline.Pipeline "sklearn.pipeline.Pipeline") 一样- 便捷性和联合参数的估计和验证。
[`FeatureUnion`](https://scikit-learn.org/stable/modules/generated/sklearn.pipeline.FeatureUnion.html#sklearn.pipeline.FeatureUnion "sklearn.pipeline.FeatureUnion") 功能与 [`Pipeline`](https://scikit-learn.org/stable/modules/generated/sklearn.pipeline.Pipeline.html#sklearn.pipeline.Pipeline "sklearn.pipeline.Pipeline") 一样- 便捷性和联合参数的估计和验证。
可以结合:class:&lt;cite&gt;FeatureUnion&lt;/cite&gt; 和 [`Pipeline`](generated/sklearn.pipeline.Pipeline.html#sklearn.pipeline.Pipeline "sklearn.pipeline.Pipeline") 来创造出复杂模型。
可以结合:class:*FeatureUnion* 和 [`Pipeline`](https://scikit-learn.org/stable/modules/generated/sklearn.pipeline.Pipeline.html#sklearn.pipeline.Pipeline "sklearn.pipeline.Pipeline") 来创造出复杂模型。
(一个 [`FeatureUnion`](generated/sklearn.pipeline.FeatureUnion.html#sklearn.pipeline.FeatureUnion "sklearn.pipeline.FeatureUnion") 没办法检查两个转换器是否会产出相同的特征。它仅仅在特征集合不相关时产生联合并确认是调用者的职责。)
(一个 [`FeatureUnion`](https://scikit-learn.org/stable/modules/generated/sklearn.pipeline.FeatureUnion.html#sklearn.pipeline.FeatureUnion "sklearn.pipeline.FeatureUnion") 没办法检查两个转换器是否会产出相同的特征。它仅仅在特征集合不相关时产生联合并确认是调用者的职责。)
### 4.1.2.1\. 用法
一个 [`FeatureUnion`](generated/sklearn.pipeline.FeatureUnion.html#sklearn.pipeline.FeatureUnion "sklearn.pipeline.FeatureUnion") 是通过一系列 `(key, value)` 键值对来构建的,其中的 `key` 给转换器指定的名字 (一个绝对的字符串; 他只是一个代号), `value` 是一个评估器对象:
一个 [`FeatureUnion`](https://scikit-learn.org/stable/modules/generated/sklearn.pipeline.FeatureUnion.html#sklearn.pipeline.FeatureUnion "sklearn.pipeline.FeatureUnion") 是通过一系列 `(key, value)` 键值对来构建的,其中的 `key` 给转换器指定的名字 (一个绝对的字符串; 他只是一个代号), `value` 是一个评估器对象:
```py
>>> from sklearn.pipeline import FeatureUnion
......@@ -239,7 +239,7 @@ FeatureUnion(n_jobs=1,
```
跟管道一样,特征联合有一个精简版的构造器叫做:func:&lt;cite&gt;make_union&lt;/cite&gt; ,该构造器不需要显式给每个组价起名字。
跟管道一样,特征联合有一个精简版的构造器叫做:func:*make_union* ,该构造器不需要显式给每个组价起名字。
正如 `Pipeline`, 单独的步骤可能用``set_params``替换 ,并设置为 [``](#id12)None``来跳过:
......@@ -255,5 +255,5 @@ FeatureUnion(n_jobs=1,
例子:
* [Concatenating multiple feature extraction methods](../auto_examples/plot_feature_stacker.html#sphx-glr-auto-examples-plot-feature-stacker-py)
* [Feature Union with Heterogeneous Data Sources](../auto_examples/hetero_feature_union.html#sphx-glr-auto-examples-hetero-feature-union-py)
\ No newline at end of file
* [Concatenating multiple feature extraction methods](https://scikit-learn.org/stable/auto_examples/plot_feature_stacker.html#sphx-glr-auto-examples-plot-feature-stacker-py)
* [Feature Union with Heterogeneous Data Sources](https://scikit-learn.org/stable/auto_examples/hetero_feature_union.html#sphx-glr-auto-examples-hetero-feature-union-py)
\ No newline at end of file
此差异已折叠。
此差异已折叠。
此差异已折叠。
......@@ -9,7 +9,7 @@
## 4.4.1\. PCA: 主成份分析
[`decomposition.PCA`](generated/sklearn.decomposition.PCA.html#sklearn.decomposition.PCA "sklearn.decomposition.PCA") 寻找能够捕捉原始特征的差异的特征的组合. 请参阅 [分解成分中的信号(矩阵分解问题)](decomposition.html#decompositions).
[`decomposition.PCA`](https://scikit-learn.org/stable/modules/generated/sklearn.decomposition.PCA.html#sklearn.decomposition.PCA "sklearn.decomposition.PCA") 寻找能够捕捉原始特征的差异的特征的组合. 请参阅 [分解成分中的信号(矩阵分解问题)](decomposition.html#decompositions).
**示例**
......@@ -23,17 +23,17 @@
**示例**
* [The Johnson-Lindenstrauss bound for embedding with random projections](../auto_examples/plot_johnson_lindenstrauss_bound.html#sphx-glr-auto-examples-plot-johnson-lindenstrauss-bound-py)
* [The Johnson-Lindenstrauss bound for embedding with random projections](https://scikit-learn.org/stable/auto_examples/plot_johnson_lindenstrauss_bound.html#sphx-glr-auto-examples-plot-johnson-lindenstrauss-bound-py)
## 4.4.3\. 特征聚集
[`cluster.FeatureAgglomeration`](generated/sklearn.cluster.FeatureAgglomeration.html#sklearn.cluster.FeatureAgglomeration "sklearn.cluster.FeatureAgglomeration") 应用 [层次聚类](clustering.html#hierarchical-clustering) 将行为类似的特征分组在一起.
[`cluster.FeatureAgglomeration`](https://scikit-learn.org/stable/modules/generated/sklearn.cluster.FeatureAgglomeration.html#sklearn.cluster.FeatureAgglomeration "sklearn.cluster.FeatureAgglomeration") 应用 [层次聚类](clustering.html#hierarchical-clustering) 将行为类似的特征分组在一起.
**示例**
* [Feature agglomeration vs. univariate selection](../auto_examples/cluster/plot_feature_agglomeration_vs_univariate_selection.html#sphx-glr-auto-examples-cluster-plot-feature-agglomeration-vs-univariate-selection-py)
* [Feature agglomeration](../auto_examples/cluster/plot_digits_agglomeration.html#sphx-glr-auto-examples-cluster-plot-digits-agglomeration-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)
* [Feature agglomeration](https://scikit-learn.org/stable/auto_examples/cluster/plot_digits_agglomeration.html#sphx-glr-auto-examples-cluster-plot-digits-agglomeration-py)
**特征缩放**
请注意,如果功能具有明显不同的缩放或统计属性,则 [`cluster.FeatureAgglomeration`](generated/sklearn.cluster.FeatureAgglomeration.html#sklearn.cluster.FeatureAgglomeration "sklearn.cluster.FeatureAgglomeration") 可能无法捕获相关特征之间的关系.使用一个 [`preprocessing.StandardScaler`](generated/sklearn.preprocessing.StandardScaler.html#sklearn.preprocessing.StandardScaler "sklearn.preprocessing.StandardScaler") 可以在这些 设置中使用.
\ No newline at end of file
请注意,如果功能具有明显不同的缩放或统计属性,则 [`cluster.FeatureAgglomeration`](https://scikit-learn.org/stable/modules/generated/sklearn.cluster.FeatureAgglomeration.html#sklearn.cluster.FeatureAgglomeration "sklearn.cluster.FeatureAgglomeration") 可能无法捕获相关特征之间的关系.使用一个 [`preprocessing.StandardScaler`](https://scikit-learn.org/stable/modules/generated/sklearn.preprocessing.StandardScaler.html#sklearn.preprocessing.StandardScaler "sklearn.preprocessing.StandardScaler") 可以在这些 设置中使用.
\ No newline at end of file
......@@ -21,7 +21,7 @@
> 在数学中,johnson - lindenstrauss 引理是一种将高维的点从高维到低维欧几里得空间的低失真嵌入的方案。 引理阐释了高维空间下的一小部分的点集可以内嵌到非常低维的空间,这种方式下点之间的距离几乎全部被保留。 内嵌所用到的映射至少符合 Lipschitz 条件,甚至可以被当做正交投影。
有了样本数量, [`sklearn.random_projection.johnson_lindenstrauss_min_dim`](generated/sklearn.random_projection.johnson_lindenstrauss_min_dim.html#sklearn.random_projection.johnson_lindenstrauss_min_dim "sklearn.random_projection.johnson_lindenstrauss_min_dim") 会保守估计随机子空间的最小大小来保证随机投影导致的变形在一定范围内:
有了样本数量, [`sklearn.random_projection.johnson_lindenstrauss_min_dim`](https://scikit-learn.org/stable/modules/generated/sklearn.random_projection.johnson_lindenstrauss_min_dim.html#sklearn.random_projection.johnson_lindenstrauss_min_dim "sklearn.random_projection.johnson_lindenstrauss_min_dim") 会保守估计随机子空间的最小大小来保证随机投影导致的变形在一定范围内:
```py
>>> from sklearn.random_projection import johnson_lindenstrauss_min_dim
......@@ -34,11 +34,11 @@ array([ 7894, 9868, 11841])`
```
[![http://sklearn.apachecn.org/cn/0.19.0/_images/sphx_glr_plot_johnson_lindenstrauss_bound_0011.png](img/01024e528443374ebac4e8cb2f6dc463.jpg)](../auto_examples/plot_johnson_lindenstrauss_bound.html)[![http://sklearn.apachecn.org/cn/0.19.0/_images/sphx_glr_plot_johnson_lindenstrauss_bound_0021.png](img/bc0cfc8c8661055fd60ca8e90b21d1dd.jpg)](../auto_examples/plot_johnson_lindenstrauss_bound.html)
[![http://sklearn.apachecn.org/cn/0.19.0/_images/sphx_glr_plot_johnson_lindenstrauss_bound_0011.png](img/01024e528443374ebac4e8cb2f6dc463.jpg)](https://scikit-learn.org/stable/auto_examples/plot_johnson_lindenstrauss_bound.html)[![http://sklearn.apachecn.org/cn/0.19.0/_images/sphx_glr_plot_johnson_lindenstrauss_bound_0021.png](img/bc0cfc8c8661055fd60ca8e90b21d1dd.jpg)](https://scikit-learn.org/stable/auto_examples/plot_johnson_lindenstrauss_bound.html)
例子:
* 查看 [The Johnson-Lindenstrauss bound for embedding with random projections](../auto_examples/plot_johnson_lindenstrauss_bound.html#sphx-glr-auto-examples-plot-johnson-lindenstrauss-bound-py) 里面有Johnson-Lindenstrauss引理的理论说明和使用稀疏随机矩阵的经验验证。
* 查看 [The Johnson-Lindenstrauss bound for embedding with random projections](https://scikit-learn.org/stable/auto_examples/plot_johnson_lindenstrauss_bound.html#sphx-glr-auto-examples-plot-johnson-lindenstrauss-bound-py) 里面有Johnson-Lindenstrauss引理的理论说明和使用稀疏随机矩阵的经验验证。
参考:
......@@ -46,7 +46,7 @@ array([ 7894, 9868, 11841])`
## 4.5.2\. 高斯随机投影
The [`sklearn.random_projection.GaussianRandomProjection`](generated/sklearn.random_projection.GaussianRandomProjection.html#sklearn.random_projection.GaussianRandomProjection "sklearn.random_projection.GaussianRandomProjection") 通过将原始输入空间投影到随机生成的矩阵(该矩阵的组件由以下分布中抽取) :math:[`](#id4)N(0, frac{1}{n_{components}})`降低维度。
The [`sklearn.random_projection.GaussianRandomProjection`](https://scikit-learn.org/stable/modules/generated/sklearn.random_projection.GaussianRandomProjection.html#sklearn.random_projection.GaussianRandomProjection "sklearn.random_projection.GaussianRandomProjection") 通过将原始输入空间投影到随机生成的矩阵(该矩阵的组件由以下分布中抽取) :math:[`](#id4)N(0, frac{1}{n_{components}})`降低维度。
以下小片段演示了任何使用高斯随机投影转换器:
......@@ -63,7 +63,7 @@ The [`sklearn.random_projection.GaussianRandomProjection`](generated/sklearn.ran
## 4.5.3\. 稀疏随机矩阵
> [`sklearn.random_projection.SparseRandomProjection`](generated/sklearn.random_projection.SparseRandomProjection.html#sklearn.random_projection.SparseRandomProjection "sklearn.random_projection.SparseRandomProjection") 使用稀疏随机矩阵,通过投影原始输入空间来降低维度。
> [`sklearn.random_projection.SparseRandomProjection`](https://scikit-learn.org/stable/modules/generated/sklearn.random_projection.SparseRandomProjection.html#sklearn.random_projection.SparseRandomProjection "sklearn.random_projection.SparseRandomProjection") 使用稀疏随机矩阵,通过投影原始输入空间来降低维度。
稀疏矩阵可以替换高斯随机投影矩阵来保证相似的嵌入质量,且内存利用率更高、投影数据的计算更快。
......
......@@ -8,7 +8,7 @@
这个子模块包含与某些 kernel 对应的特征映射的函数,这个会用于例如支持向量机的算法当中(see [支持向量机](svm.html#svm))。 下面这些特征函数对输入执行非线性转换,可以用于线性分类或者其他算法。
[kernel trick](https://en.wikipedia.org/wiki/Kernel_trick) 相比,近似的进行特征映射更适合在线学习,并能够有效 减少学习大量数据的内存开销。使用标准核技巧的 svm 不能有效的适用到海量数据,但是使用近似内核映射的方法,对于线性 SVM 来说效果可能更好。 而且,使用 [`SGDClassifier`](generated/sklearn.linear_model.SGDClassifier.html#sklearn.linear_model.SGDClassifier "sklearn.linear_model.SGDClassifier") 进行近似的内核映射,使得对海量数据进行非线性学习也成为了可能。
[kernel trick](https://en.wikipedia.org/wiki/Kernel_trick) 相比,近似的进行特征映射更适合在线学习,并能够有效 减少学习大量数据的内存开销。使用标准核技巧的 svm 不能有效的适用到海量数据,但是使用近似内核映射的方法,对于线性 SVM 来说效果可能更好。 而且,使用 [`SGDClassifier`](https://scikit-learn.org/stable/modules/generated/sklearn.linear_model.SGDClassifier.html#sklearn.linear_model.SGDClassifier "sklearn.linear_model.SGDClassifier") 进行近似的内核映射,使得对海量数据进行非线性学习也成为了可能。
由于近似嵌入的方法没有太多经验性的验证,所以建议将结果和使用精确的内核方法的结果进行比较。
......@@ -18,11 +18,11 @@ See also
## 4.6.1\. 内核近似的 Nystroem 方法
[`Nystroem`](generated/sklearn.kernel_approximation.Nystroem.html#sklearn.kernel_approximation.Nystroem "sklearn.kernel_approximation.Nystroem") 中实现了 Nystroem 方法用于低等级的近似核。它是通过采样 kernel 已经评估好的数据。默认情况下, [`Nystroem`](generated/sklearn.kernel_approximation.Nystroem.html#sklearn.kernel_approximation.Nystroem "sklearn.kernel_approximation.Nystroem") 使用 `rbf` kernel,但它可以使用任何内核函数和预计算内核矩阵. 使用的样本数量 - 计算的特征维数 - 由参数 `n_components` 给出.
[`Nystroem`](https://scikit-learn.org/stable/modules/generated/sklearn.kernel_approximation.Nystroem.html#sklearn.kernel_approximation.Nystroem "sklearn.kernel_approximation.Nystroem") 中实现了 Nystroem 方法用于低等级的近似核。它是通过采样 kernel 已经评估好的数据。默认情况下, [`Nystroem`](https://scikit-learn.org/stable/modules/generated/sklearn.kernel_approximation.Nystroem.html#sklearn.kernel_approximation.Nystroem "sklearn.kernel_approximation.Nystroem") 使用 `rbf` kernel,但它可以使用任何内核函数和预计算内核矩阵. 使用的样本数量 - 计算的特征维数 - 由参数 `n_components` 给出.
## 4.6.2\. 径向基函数内核
[`RBFSampler`](generated/sklearn.kernel_approximation.RBFSampler.html#sklearn.kernel_approximation.RBFSampler "sklearn.kernel_approximation.RBFSampler") 为径向基函数核构造一个近似映射,又称为 Random Kitchen Sinks [RR2007]. 在应用线性算法(例如线性 SVM )之前,可以使用此转换来明确建模内核映射:
[`RBFSampler`](https://scikit-learn.org/stable/modules/generated/sklearn.kernel_approximation.RBFSampler.html#sklearn.kernel_approximation.RBFSampler "sklearn.kernel_approximation.RBFSampler") 为径向基函数核构造一个近似映射,又称为 Random Kitchen Sinks [RR2007]. 在应用线性算法(例如线性 SVM )之前,可以使用此转换来明确建模内核映射:
```py
>>> from sklearn.kernel_approximation import RBFSampler
......@@ -47,15 +47,15 @@ SGDClassifier(alpha=0.0001, average=False, class_weight=None, epsilon=0.1,
`fit` 函数有两个参数: `n_components` 是特征变换的目标维数. `gamma` 是 RBF-kernel 的参数. `n_components` 越高,会导致更好的内核近似, 并且将产生与内核 SVM 产生的结果更相似的结果。请注意,”拟合” 特征函数实际上不取决于 `fit` 函数传递的数据。只有数据的维数被使用。 详情可以参考 [[RR2007]](#rr2007).
对于给定的值 `n_components` [`RBFSampler`](generated/sklearn.kernel_approximation.RBFSampler.html#sklearn.kernel_approximation.RBFSampler "sklearn.kernel_approximation.RBFSampler")[`Nystroem`](generated/sklearn.kernel_approximation.Nystroem.html#sklearn.kernel_approximation.Nystroem "sklearn.kernel_approximation.Nystroem") 中使用通常不太准确, 但是 [`RBFSampler`](generated/sklearn.kernel_approximation.RBFSampler.html#sklearn.kernel_approximation.RBFSampler "sklearn.kernel_approximation.RBFSampler") 使用更大的特征空间,更容易计算。
对于给定的值 `n_components` [`RBFSampler`](https://scikit-learn.org/stable/modules/generated/sklearn.kernel_approximation.RBFSampler.html#sklearn.kernel_approximation.RBFSampler "sklearn.kernel_approximation.RBFSampler")[`Nystroem`](https://scikit-learn.org/stable/modules/generated/sklearn.kernel_approximation.Nystroem.html#sklearn.kernel_approximation.Nystroem "sklearn.kernel_approximation.Nystroem") 中使用通常不太准确, 但是 [`RBFSampler`](https://scikit-learn.org/stable/modules/generated/sklearn.kernel_approximation.RBFSampler.html#sklearn.kernel_approximation.RBFSampler "sklearn.kernel_approximation.RBFSampler") 使用更大的特征空间,更容易计算。
[![http://sklearn.apachecn.org/cn/0.19.0/_images/sphx_glr_plot_kernel_approximation_0021.png](img/d90bb77f4f60c523c2bc041f768e8a49.jpg)](../auto_examples/plot_kernel_approximation.html)
[![http://sklearn.apachecn.org/cn/0.19.0/_images/sphx_glr_plot_kernel_approximation_0021.png](img/d90bb77f4f60c523c2bc041f768e8a49.jpg)](https://scikit-learn.org/stable/auto_examples/plot_kernel_approximation.html)
将精确的 RBF kernel (左) 与 approximation (右) 进行比较。
示例:
* [Explicit feature map approximation for RBF kernels](../auto_examples/plot_kernel_approximation.html#sphx-glr-auto-examples-plot-kernel-approximation-py)
* [Explicit feature map approximation for RBF kernels](https://scikit-learn.org/stable/auto_examples/plot_kernel_approximation.html#sphx-glr-auto-examples-plot-kernel-approximation-py)
## 4.6.3\. 加性卡方核
......@@ -67,9 +67,9 @@ Additive Chi Squared Kernel (加性卡方核)是直方图的核心,通常用
这个和 `sklearn.metrics.additive_chi2_kernel` 不完全一样.[VZ2010]_ 的作者喜欢上面的版本,因为它总是积极的。 由于这个 kernel 是可添加的,因此可以分别处理嵌入的 ![x_i](img/cf52655ee609af9f3c27c06448a5bf67.jpg). 这使得在规则的间隔类对傅里叶变换进行性才赢,代替近似的 Monte Carlo 采样。
> [`AdditiveChi2Sampler`](generated/sklearn.kernel_approximation.AdditiveChi2Sampler.html#sklearn.kernel_approximation.AdditiveChi2Sampler "sklearn.kernel_approximation.AdditiveChi2Sampler") 类实现了这个组件采样方法. 每个组件都被采样 ![n](img/c87d9110f3d32ffa5fa08671e4af11fb.jpg) 次,每一个输入维数都会产生 &lt;cite&gt;2n+1&lt;/cite&gt; 维(来自傅立叶变换的实部和复数部分的两个数据段的倍数). 在文献中,![n](img/c87d9110f3d32ffa5fa08671e4af11fb.jpg) 经常取为 1 或者 2,将数据集转换为 `n_samples * 5 * n_features` 大小(在 ![n=2](img/b94b3a3837e7741f704e3b9b23ba0880.jpg) 的情况下 ).
> [`AdditiveChi2Sampler`](https://scikit-learn.org/stable/modules/generated/sklearn.kernel_approximation.AdditiveChi2Sampler.html#sklearn.kernel_approximation.AdditiveChi2Sampler "sklearn.kernel_approximation.AdditiveChi2Sampler") 类实现了这个组件采样方法. 每个组件都被采样 ![n](img/c87d9110f3d32ffa5fa08671e4af11fb.jpg) 次,每一个输入维数都会产生 *2n+1* 维(来自傅立叶变换的实部和复数部分的两个数据段的倍数). 在文献中,![n](img/c87d9110f3d32ffa5fa08671e4af11fb.jpg) 经常取为 1 或者 2,将数据集转换为 `n_samples * 5 * n_features` 大小(在 ![n=2](img/b94b3a3837e7741f704e3b9b23ba0880.jpg) 的情况下 ).
[`AdditiveChi2Sampler`](generated/sklearn.kernel_approximation.AdditiveChi2Sampler.html#sklearn.kernel_approximation.AdditiveChi2Sampler "sklearn.kernel_approximation.AdditiveChi2Sampler") 提供的近似特征映射可以和 [`RBFSampler`](generated/sklearn.kernel_approximation.RBFSampler.html#sklearn.kernel_approximation.RBFSampler "sklearn.kernel_approximation.RBFSampler") 提供的近似特征映射合并,得到一个取幂的 chi squared kerne。可以查看 [[VZ2010]](#vz2010)[[VVZ2010]](#vvz2010) [`RBFSampler`](generated/sklearn.kernel_approximation.RBFSampler.html#sklearn.kernel_approximation.RBFSampler "sklearn.kernel_approximation.RBFSampler") 的合并.
[`AdditiveChi2Sampler`](https://scikit-learn.org/stable/modules/generated/sklearn.kernel_approximation.AdditiveChi2Sampler.html#sklearn.kernel_approximation.AdditiveChi2Sampler "sklearn.kernel_approximation.AdditiveChi2Sampler") 提供的近似特征映射可以和 [`RBFSampler`](https://scikit-learn.org/stable/modules/generated/sklearn.kernel_approximation.RBFSampler.html#sklearn.kernel_approximation.RBFSampler "sklearn.kernel_approximation.RBFSampler") 提供的近似特征映射合并,得到一个取幂的 chi squared kerne。可以查看 [[VZ2010]](#vz2010)[[VVZ2010]](#vvz2010) [`RBFSampler`](https://scikit-learn.org/stable/modules/generated/sklearn.kernel_approximation.RBFSampler.html#sklearn.kernel_approximation.RBFSampler "sklearn.kernel_approximation.RBFSampler") 的合并.
## 4.6.4\. Skewed Chi Squared Kernel (偏斜卡方核?暂译)
......@@ -79,7 +79,7 @@ skewed chi squared kernel 给出下面公式
它有和 指数卡方核 相似的属性,用于计算机视觉.但是允许进行简单的 蒙特卡洛 近似 的特征映射。
[`SkewedChi2Sampler`](generated/sklearn.kernel_approximation.SkewedChi2Sampler.html#sklearn.kernel_approximation.SkewedChi2Sampler "sklearn.kernel_approximation.SkewedChi2Sampler") 的使用和之前描述的 [`RBFSampler`](generated/sklearn.kernel_approximation.RBFSampler.html#sklearn.kernel_approximation.RBFSampler "sklearn.kernel_approximation.RBFSampler") 一样.唯一的区别是自由参数,称之为 ![c](img/d5c9a11453ea30a1be50a1034052bd6b.jpg). 这种映射和数学细节可以参考 [[LS2010]](#ls2010).
[`SkewedChi2Sampler`](https://scikit-learn.org/stable/modules/generated/sklearn.kernel_approximation.SkewedChi2Sampler.html#sklearn.kernel_approximation.SkewedChi2Sampler "sklearn.kernel_approximation.SkewedChi2Sampler") 的使用和之前描述的 [`RBFSampler`](https://scikit-learn.org/stable/modules/generated/sklearn.kernel_approximation.RBFSampler.html#sklearn.kernel_approximation.RBFSampler "sklearn.kernel_approximation.RBFSampler") 一样.唯一的区别是自由参数,称之为 ![c](img/d5c9a11453ea30a1be50a1034052bd6b.jpg). 这种映射和数学细节可以参考 [[LS2010]](#ls2010).
## 4.6.5\. 数学方面的细节
......
......@@ -26,13 +26,13 @@ The [`sklearn.metrics.pairwise`](classes.html#module-sklearn.metrics.pairwise "s
## 4.7.1\. 余弦相似度
[`cosine_similarity`](generated/sklearn.metrics.pairwise.cosine_similarity.html#sklearn.metrics.pairwise.cosine_similarity "sklearn.metrics.pairwise.cosine_similarity") 计算L2正则化的向量的点积. 也就是说, if ![x](img/5c82dbae35dc43d2f556f9f284d9d184.jpg) 和 ![y](img/0775c03fc710a24df297dedcec515aaf.jpg) 都是行向量,, 它们的余弦相似度 ![k](img/f93871977da52a6d11045d57c3e18728.jpg) 定义为:
[`cosine_similarity`](https://scikit-learn.org/stable/modules/generated/sklearn.metrics.pairwise.cosine_similarity.html#sklearn.metrics.pairwise.cosine_similarity "sklearn.metrics.pairwise.cosine_similarity") 计算L2正则化的向量的点积. 也就是说, if ![x](img/5c82dbae35dc43d2f556f9f284d9d184.jpg) 和 ![y](img/0775c03fc710a24df297dedcec515aaf.jpg) 都是行向量,, 它们的余弦相似度 ![k](img/f93871977da52a6d11045d57c3e18728.jpg) 定义为:
![k(x, y) = \frac{x y^\top}{\|x\| \|y\|}](img/e5b3516a2cd7fbf2916643478e0bed70.jpg)
这被称为余弦相似度, 因为欧几里得(L2) 正则化将向量投影到单元球面内,那么它们的点积就是被向量表示的点之间的角度。
这种核函数对于计算以tf-idf向量表示的文档之间的相似度是一个通常的选择. [`cosine_similarity`](generated/sklearn.metrics.pairwise.cosine_similarity.html#sklearn.metrics.pairwise.cosine_similarity "sklearn.metrics.pairwise.cosine_similarity") 接受 `scipy.sparse` 矩阵. (注意到 `sklearn.feature_extraction.text` 中的tf-idf函数能计算归一化的向量,在这种情况下 [`cosine_similarity`](generated/sklearn.metrics.pairwise.cosine_similarity.html#sklearn.metrics.pairwise.cosine_similarity "sklearn.metrics.pairwise.cosine_similarity") 等同于 [`linear_kernel`](generated/sklearn.metrics.pairwise.linear_kernel.html#sklearn.metrics.pairwise.linear_kernel "sklearn.metrics.pairwise.linear_kernel"), 只是慢一点而已.)
这种核函数对于计算以tf-idf向量表示的文档之间的相似度是一个通常的选择. [`cosine_similarity`](https://scikit-learn.org/stable/modules/generated/sklearn.metrics.pairwise.cosine_similarity.html#sklearn.metrics.pairwise.cosine_similarity "sklearn.metrics.pairwise.cosine_similarity") 接受 `scipy.sparse` 矩阵. (注意到 `sklearn.feature_extraction.text` 中的tf-idf函数能计算归一化的向量,在这种情况下 [`cosine_similarity`](https://scikit-learn.org/stable/modules/generated/sklearn.metrics.pairwise.cosine_similarity.html#sklearn.metrics.pairwise.cosine_similarity "sklearn.metrics.pairwise.cosine_similarity") 等同于 [`linear_kernel`](https://scikit-learn.org/stable/modules/generated/sklearn.metrics.pairwise.linear_kernel.html#sklearn.metrics.pairwise.linear_kernel "sklearn.metrics.pairwise.linear_kernel"), 只是慢一点而已.)
References:
......@@ -40,7 +40,7 @@ References:
## 4.7.2\. 线性核函数
函数 [`linear_kernel`](generated/sklearn.metrics.pairwise.linear_kernel.html#sklearn.metrics.pairwise.linear_kernel "sklearn.metrics.pairwise.linear_kernel") 是计算线性核函数, 也就是一种在 `degree=1``coef0=0` (同质化) 情况下的 [`polynomial_kernel`](generated/sklearn.metrics.pairwise.polynomial_kernel.html#sklearn.metrics.pairwise.polynomial_kernel "sklearn.metrics.pairwise.polynomial_kernel") 的特殊形式. 如果 `x``y` 是列向量, 它们的线性核函数是:
函数 [`linear_kernel`](https://scikit-learn.org/stable/modules/generated/sklearn.metrics.pairwise.linear_kernel.html#sklearn.metrics.pairwise.linear_kernel "sklearn.metrics.pairwise.linear_kernel") 是计算线性核函数, 也就是一种在 `degree=1``coef0=0` (同质化) 情况下的 [`polynomial_kernel`](https://scikit-learn.org/stable/modules/generated/sklearn.metrics.pairwise.polynomial_kernel.html#sklearn.metrics.pairwise.polynomial_kernel "sklearn.metrics.pairwise.polynomial_kernel") 的特殊形式. 如果 `x``y` 是列向量, 它们的线性核函数是:
![k(x, y) = x^\top y](img/adc60d285d73d89dac7cb76f51617e64.jpg)
......@@ -65,7 +65,7 @@ References:
## 4.7.4\. Sigmoid 核函数
函数 [`sigmoid_kernel`](generated/sklearn.metrics.pairwise.sigmoid_kernel.html#sklearn.metrics.pairwise.sigmoid_kernel "sklearn.metrics.pairwise.sigmoid_kernel") 计算两个向量之间的S型核函数. S型核函数也被称为双曲切线或者 多层感知机(因为在神经网络领域,它经常被当做激活函数). S型核函数定义为:
函数 [`sigmoid_kernel`](https://scikit-learn.org/stable/modules/generated/sklearn.metrics.pairwise.sigmoid_kernel.html#sklearn.metrics.pairwise.sigmoid_kernel "sklearn.metrics.pairwise.sigmoid_kernel") 计算两个向量之间的S型核函数. S型核函数也被称为双曲切线或者 多层感知机(因为在神经网络领域,它经常被当做激活函数). S型核函数定义为:
![k(x, y) = \tanh( \gamma x^\top y + c_0)](img/11265c80ea298a58e0a1010736d28b38.jpg)
......@@ -77,7 +77,7 @@ where:
## 4.7.5\. RBF 核函数
函数 [`rbf_kernel`](generated/sklearn.metrics.pairwise.rbf_kernel.html#sklearn.metrics.pairwise.rbf_kernel "sklearn.metrics.pairwise.rbf_kernel") 计算计算两个向量之间的径向基函数核 (RBF) 。 其定义为:
函数 [`rbf_kernel`](https://scikit-learn.org/stable/modules/generated/sklearn.metrics.pairwise.rbf_kernel.html#sklearn.metrics.pairwise.rbf_kernel "sklearn.metrics.pairwise.rbf_kernel") 计算计算两个向量之间的径向基函数核 (RBF) 。 其定义为:
![k(x, y) = \exp( -\gamma \| x-y \|^2)](img/a35122280170c396ab3c9d8fa3b62446.jpg)
......@@ -85,7 +85,7 @@ where:
## 4.7.6\. 拉普拉斯核函数
函数 [`laplacian_kernel`](generated/sklearn.metrics.pairwise.laplacian_kernel.html#sklearn.metrics.pairwise.laplacian_kernel "sklearn.metrics.pairwise.laplacian_kernel") 是一种径向基函数核的变体,定义为:
函数 [`laplacian_kernel`](https://scikit-learn.org/stable/modules/generated/sklearn.metrics.pairwise.laplacian_kernel.html#sklearn.metrics.pairwise.laplacian_kernel "sklearn.metrics.pairwise.laplacian_kernel") 是一种径向基函数核的变体,定义为:
![k(x, y) = \exp( -\gamma \| x-y \|_1)](img/99dfcad081b3f6e1f4648a9f7d24f103.jpg)
......@@ -99,9 +99,9 @@ where:
在计算机视觉应用中训练非线性支持向量机时卡方核函数是一种非常流行的选择.
```
它能以 [`chi2_kernel`](generated/sklearn.metrics.pairwise.chi2_kernel.html#sklearn.metrics.pairwise.chi2_kernel "sklearn.metrics.pairwise.chi2_kernel") 计算然后将参数 [``](#id7)kernel=”precomputed”[``](#id9)传递到
它能以 [`chi2_kernel`](https://scikit-learn.org/stable/modules/generated/sklearn.metrics.pairwise.chi2_kernel.html#sklearn.metrics.pairwise.chi2_kernel "sklearn.metrics.pairwise.chi2_kernel") 计算然后将参数 [``](#id7)kernel=”precomputed”[``](#id9)传递到
[`sklearn.svm.SVC`](generated/sklearn.svm.SVC.html#sklearn.svm.SVC "sklearn.svm.SVC") :
[`sklearn.svm.SVC`](https://scikit-learn.org/stable/modules/generated/sklearn.svm.SVC.html#sklearn.svm.SVC "sklearn.svm.SVC") :
```py
>>> from sklearn.svm import SVC
......
......@@ -8,7 +8,7 @@
## 4.8.1\. 标签二值化
[`LabelBinarizer`](generated/sklearn.preprocessing.LabelBinarizer.html#sklearn.preprocessing.LabelBinarizer "sklearn.preprocessing.LabelBinarizer") 是一个用来从多类别列表创建标签矩阵的工具类:
[`LabelBinarizer`](https://scikit-learn.org/stable/modules/generated/sklearn.preprocessing.LabelBinarizer.html#sklearn.preprocessing.LabelBinarizer "sklearn.preprocessing.LabelBinarizer") 是一个用来从多类别列表创建标签矩阵的工具类:
```py
>>> from sklearn import preprocessing
......@@ -23,7 +23,7 @@ array([[1, 0, 0, 0],
```
对于多类别是实例,可以使用 [`MultiLabelBinarizer`](generated/sklearn.preprocessing.MultiLabelBinarizer.html#sklearn.preprocessing.MultiLabelBinarizer "sklearn.preprocessing.MultiLabelBinarizer"):
对于多类别是实例,可以使用 [`MultiLabelBinarizer`](https://scikit-learn.org/stable/modules/generated/sklearn.preprocessing.MultiLabelBinarizer.html#sklearn.preprocessing.MultiLabelBinarizer "sklearn.preprocessing.MultiLabelBinarizer"):
```py
>>> lb = preprocessing.MultiLabelBinarizer()
......@@ -37,7 +37,7 @@ array([1, 2, 3])
## 4.8.2\. 标签编码
[`LabelEncoder`](generated/sklearn.preprocessing.LabelEncoder.html#sklearn.preprocessing.LabelEncoder "sklearn.preprocessing.LabelEncoder") 是一个可以用来将标签规范化的工具类,它可以将标签的编码值范围限定在[0,n_classes-1]. 这在编写高效的Cython程序时是非常有用的. [`LabelEncoder`](generated/sklearn.preprocessing.LabelEncoder.html#sklearn.preprocessing.LabelEncoder "sklearn.preprocessing.LabelEncoder") 可以如下使用:
[`LabelEncoder`](https://scikit-learn.org/stable/modules/generated/sklearn.preprocessing.LabelEncoder.html#sklearn.preprocessing.LabelEncoder "sklearn.preprocessing.LabelEncoder") 是一个可以用来将标签规范化的工具类,它可以将标签的编码值范围限定在[0,n_classes-1]. 这在编写高效的Cython程序时是非常有用的. [`LabelEncoder`](https://scikit-learn.org/stable/modules/generated/sklearn.preprocessing.LabelEncoder.html#sklearn.preprocessing.LabelEncoder "sklearn.preprocessing.LabelEncoder") 可以如下使用:
```py
>>> from sklearn import preprocessing
......
此差异已折叠。
此差异已折叠。
此差异已折叠。
......@@ -37,7 +37,7 @@
## 加载示例数据集
&lt;cite&gt;scikit-learn&lt;/cite&gt; 提供了一些标准数据集,例如 用于分类的 [iris](https://en.wikipedia.org/wiki/Iris_flower_data_set)[digits](http://archive.ics.uci.edu/ml/datasets/Pen-Based+Recognition+of+Handwritten+Digits) 数据集 和 [波士顿房价回归数据集](http://archive.ics.uci.edu/ml/datasets/Housing) .
*scikit-learn* 提供了一些标准数据集,例如 用于分类的 [iris](https://en.wikipedia.org/wiki/Iris_flower_data_set)[digits](http://archive.ics.uci.edu/ml/datasets/Pen-Based+Recognition+of+Handwritten+Digits) 数据集 和 [波士顿房价回归数据集](http://archive.ics.uci.edu/ml/datasets/Housing) .
在下文中,我们从我们的 shell 启动一个 Python 解释器,然后加载 `iris``digits` 数据集。我们的符号约定是 `$` 表示 shell 提示符,而 `&gt;&gt;&gt;` 表示 Python 解释器提示符:
......
此差异已折叠。
此差异已折叠。
......@@ -84,7 +84,7 @@ array([ 0.93489149, 0.95659432, 0.93989983])
```
&lt;cite&gt;n_jobs=-1&lt;/cite&gt; 意味着运算会被调度到所有 CPU 上进行。
*n_jobs=-1* 意味着运算会被调度到所有 CPU 上进行。
或者,可以提供 `scoring` 参数来指定替换的评分方法。
......
......@@ -204,7 +204,7 @@ sci.med
```
在上面的样例代码中,我们首先使用了 `fit(..)` 方法来拟合对数据的 estimator(估算器),接着使用 `transform(..)` 方法来把我们的计数矩阵转换成 &lt;cite&gt;tf-idf&lt;/cite&gt; 型。 通过跳过冗余处理,这两步可以结合起来,来更快地得到同样的结果。这种操作可以使用上节提到过的 `fit_transform(..)` 方法来完成,如下:
在上面的样例代码中,我们首先使用了 `fit(..)` 方法来拟合对数据的 estimator(估算器),接着使用 `transform(..)` 方法来把我们的计数矩阵转换成 *tf-idf* 型。 通过跳过冗余处理,这两步可以结合起来,来更快地得到同样的结果。这种操作可以使用上节提到过的 `fit_transform(..)` 方法来完成,如下:
```py
>>> tfidf_transformer = TfidfTransformer()
......
此差异已折叠。
......@@ -17,7 +17,7 @@ sy-kit learn。sci代表着科学!
## 选择 scikit的理由 ?
scikit拥有很多围绕Scipy构建的科学工具箱。你可以在 &lt;cite&gt;&lt;https://scikits.appspot.com/scikits&gt;&lt;/cite&gt; 查找工具列表。 &lt;cite&gt;scikit-image &lt;http://scikit-image.org/&gt;&lt;/cite&gt; 和 scikit-learn_一样受欢迎 。
scikit拥有很多围绕Scipy构建的科学工具箱。你可以在 *&lt;https://scikits.appspot.com/scikits&gt;* 查找工具列表。 *scikit-image &lt;http://scikit-image.org/&gt;* 和 scikit-learn_一样受欢迎 。
## 如何才能为 scikit-learn 贡献自己的力量?
......@@ -128,9 +128,9 @@ array([[0],
## 为什么我有时会在 OSX 或 Linux 下遇到 n_jobs &gt; 1 崩溃/冻结?
一些例如 `GridSearchCV``cross_val_score` 的scikit-learn工具,它们可以依靠 Python 的内置 &lt;cite&gt;multiprocessing&lt;/cite&gt; 模块,通过 `n_jobs &gt; 1` 作为参数,将执行并行化到多个 Python 进程。
一些例如 `GridSearchCV``cross_val_score` 的scikit-learn工具,它们可以依靠 Python 的内置 *multiprocessing* 模块,通过 `n_jobs &gt; 1` 作为参数,将执行并行化到多个 Python 进程。
问题是 Python 由于性能原因 `multiprocessing` 会执行 `fork` 系统调用,而不是 `exec` 系统调用。许多库如 OSX 下的(某些版本的)Accelerate / vecLib, (某些版本的) MKL, GCC 的 OpenMP 运行时,nvidia 的 Cuda (可能还有一些其他的),都是自行管理自己的内部线程池。在调用 &lt;cite&gt;fork&lt;/cite&gt; 时,子进程中的线程池状态已损坏:线程池认为它有许多线程,而只有主线程状态已被 fork。有可能更改库,使它们在发生 fork 时检测,并在该情况下重新初始化线程池:我们对 OpenBLAS 执行了此操作(从 0.2.10 开始在 master 中合并),并且我们向 GCC 的 OpenMP 运行时提供了一个 [补丁](https://gcc.gnu.org/bugzilla/show_bug.cgi?id=60035) (尚未审查)
问题是 Python 由于性能原因 `multiprocessing` 会执行 `fork` 系统调用,而不是 `exec` 系统调用。许多库如 OSX 下的(某些版本的)Accelerate / vecLib, (某些版本的) MKL, GCC 的 OpenMP 运行时,nvidia 的 Cuda (可能还有一些其他的),都是自行管理自己的内部线程池。在调用 *fork* 时,子进程中的线程池状态已损坏:线程池认为它有许多线程,而只有主线程状态已被 fork。有可能更改库,使它们在发生 fork 时检测,并在该情况下重新初始化线程池:我们对 OpenBLAS 执行了此操作(从 0.2.10 开始在 master 中合并),并且我们向 GCC 的 OpenMP 运行时提供了一个 [补丁](https://gcc.gnu.org/bugzilla/show_bug.cgi?id=60035) (尚未审查)
但最终,真正的罪魁祸首是 Python 的 `multiprocessing` ,执行 `fork` 而不是执行 `exec` 来减少开始的和新使用的并行计算的 Python 进程的开销。但这这违反了 POSIX 标准,因而被一些软件编辑器(如苹果)拒绝认为在 Accelerate / vecLib 中缺乏 fork 安全是一个 bug。
......
此差异已折叠。
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册