Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
OpenDocCN
sklearn-doc-zh
提交
c0034d1b
S
sklearn-doc-zh
项目概览
OpenDocCN
/
sklearn-doc-zh
通知
3
Star
3
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
S
sklearn-doc-zh
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
未验证
提交
c0034d1b
编写于
4月 01, 2020
作者:
片刻小哥哥
提交者:
GitHub
4月 01, 2020
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #404 from LovelyBuggies/nino-patch-tutorial
n!no-patch-tutorial
上级
fa107c94
852e2c8d
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
47 addition
and
52 deletion
+47
-52
docs/0.21.3/54.md
docs/0.21.3/54.md
+47
-52
未找到文件。
docs/0.21.3/54.md
浏览文件 @
c0034d1b
# 监督学习:从高维观察预测输出变量
校验者
:
校验者
:
[
@Kyrie
](
https://github.com/apachecn/scikit-learn-doc-zh
)
[
@片刻
](
https://github.com/apachecn/scikit-learn-doc-zh
)
[
@Loopy
](
https://github.com/loopyme
)
翻译者:
[
@N!no
](
https://github.com/lovelybuggies
)
翻译者:
[
@森系
](
https://github.com/apachecn/scikit-learn-doc-zh
)
>**监督学习解决的问题**
...
...
@@ -27,31 +28,31 @@
>
>[![http://sklearn.apachecn.org/cn/0.19.0/_images/sphx_glr_plot_iris_dataset_001.png](img/4246a718076893e37084bc69a7e16007.jpg)](https://scikit-learn.org/stable/auto_examples/datasets/plot_iris_dataset.html)
>
>鸢尾属植物数据集是根据花瓣长度、花瓣度度、萼片长度和萼片宽度4个特征对3种不同类型的鸢尾属植物进行分类
:
>鸢尾属植物数据集是根据花瓣长度、花瓣度度、萼片长度和萼片宽度4个特征对3种不同类型的鸢尾属植物进行分类
:
>
>```py
>```py
thon
>>> import numpy as np
>>> from sklearn import datasets
>>> iris = datasets.load_iris()
>>> iris_X = iris.data
>>> iris_y = iris.target
>>> np.unique(iris_y)
array([0, 1, 2])
```
>
array([0, 1, 2])
>
```
### K近邻分类器
[最近邻](https://en.wikipedia.org/wiki/K-nearest_neighbor_algorithm)
:
也许是最简单的分类器:给定一个新的观察值 `X_test`,用最接近的特征向量在训练集(比如,用于训练估计器的数据)找到观察值。(请看 Scikit-learn 在线学习文档的 [最近邻章节](7#16-最近邻) 获取更多关于这种分类器的信息)
[
最近邻
](
https://en.wikipedia.org/wiki/K-nearest_neighbor_algorithm
)
:
也许是最简单的分类器:给定一个新的观察值
`X_test`
,用最接近的特征向量在训练集(比如,用于训练估计器的数据)找到观察值。(请看 Scikit-learn 在线学习文档的
[
最近邻章节
](
7#16-最近邻
)
获取更多关于这种分类器的信息)
>**训练集和测试集**
>
>当用任意的学习算法进行实验时,最重要的就是不要在用于拟合估计器的数据上测试一个估计器的预期值,因为这不会评估在 **新数据** 上估计器的执行情况。这也是数据集经常被分为 _训练_ 和 _测试_ 数据的原因。
**KNN(k 最近邻)分类器例子**
:
**KNN(k 最近邻)分类器例子**
:
[
![http://sklearn.apachecn.org/cn/0.19.0/_images/sphx_glr_plot_classification_001.png
](
img/86003b5287219bcbec1586985a110629.jpg
)
](https://scikit-learn.org/stable/auto_examples/neighbors/plot_classification.html)
```
py
```
py
thon
>>>
# 将鸢尾属植物数据集分解为训练集和测试集
>>>
# 随机排列,用于使分解的数据随机分布
>>>
np
.
random
.
seed
(
0
)
...
...
@@ -71,7 +72,6 @@ KNeighborsClassifier(algorithm='auto', leaf_size=30, metric='minkowski',
array
([
1
,
2
,
1
,
0
,
0
,
0
,
2
,
1
,
2
,
0
])
>>>
iris_y_test
array
([
1
,
1
,
1
,
0
,
0
,
0
,
2
,
1
,
2
,
0
])
```
### 维度惩罚
...
...
@@ -88,15 +88,15 @@ array([1, 1, 1, 0, 0, 0, 2, 1, 2, 0])
>糖尿病数据集
>
>糖尿病数据集包括442名患者的10个生理特征(年龄,性别,体重,血压),和一年后的疾病级别指标
:
>糖尿病数据集包括442名患者的10个生理特征(年龄,性别,体重,血压),和一年后的疾病级别指标
:
>
>```py
>```py
thon
>>> diabetes = datasets.load_diabetes()
>>> diabetes_X_train = diabetes.data[:-20]
>>> diabetes_X_test = diabetes.data[-20:]
>>> diabetes_y_train = diabetes.target[:-20]
>>> diabetes_y_test = diabetes.target[-20:]
```
>
```
>
>任务是使用生理特征来预测疾病级别。
...
...
@@ -108,12 +108,12 @@ array([1, 1, 1, 0, 0, 0, 2, 1, 2, 0])
Linear models: !
[
y = X\beta + \epsilon
](
img/c5f2af9df9f65f0e399542ecf7f40554.jpg
)
*
!
[
X
](
img/43c1fea57579e54f80c0535bc582626f.jpg
)
:
数据
*
!
[
y
](
img/0775c03fc710a24df297dedcec515aaf.jpg
)
:
目标变量
*
!
[
\beta
](
img/533e54759d696211ebe7819cc107d3bc.jpg
)
:
回归系数
*
!
[
\epsilon
](
img/58ef9e1b5d2ee139dcb588a3879ca1a6.jpg
)
:
观察噪声
*
!
[
X
](
img/43c1fea57579e54f80c0535bc582626f.jpg
)
:
数据
*
!
[
y
](
img/0775c03fc710a24df297dedcec515aaf.jpg
)
:
目标变量
*
!
[
\beta
](
img/533e54759d696211ebe7819cc107d3bc.jpg
)
:
回归系数
*
!
[
\epsilon
](
img/58ef9e1b5d2ee139dcb588a3879ca1a6.jpg
)
:
观察噪声
```
py
```
py
thon
>>>
from
sklearn
import
linear_model
>>>
regr
=
linear_model
.
LinearRegression
()
>>>
regr
.
fit
(
diabetes_X_train
,
diabetes_y_train
)
...
...
@@ -130,7 +130,6 @@ LinearRegression(copy_X=True, fit_intercept=True, n_jobs=1, normalize=False)
>>>
# 0 意味着 X 和 y 之间没有线性关系。
>>>
regr
.
score
(
diabetes_X_test
,
diabetes_y_test
)
0.5850753022690
...
```
### 收缩
...
...
@@ -139,7 +138,7 @@ LinearRegression(copy_X=True, fit_intercept=True, n_jobs=1, normalize=False)
[
![http://sklearn.apachecn.org/cn/0.19.0/_images/sphx_glr_plot_ols_ridge_variance_001.png
](
img/f28436a66fb892c9e8923e6649f19065.jpg
)
](https://scikit-learn.org/stable/auto_examples/linear_model/plot_ols_ridge_variance.html)
```
py
```
py
thon
>>>
X
=
np
.
c_
[
.
5
,
1
].
T
>>>
y
=
[.
5
,
1
]
>>>
test
=
np
.
c_
[
0
,
2
].
T
...
...
@@ -154,14 +153,13 @@ LinearRegression(copy_X=True, fit_intercept=True, n_jobs=1, normalize=False)
...
regr
.
fit
(
this_X
,
y
)
...
plt
.
plot
(
test
,
regr
.
predict
(
test
))
...
plt
.
scatter
(
this_X
,
y
,
s
=
3
)
```
高
纬
统计学习中的一个解决方法是 _收缩_ 回归系数到0:任何两个随机选择的观察值数据集都很可能是不相关的。这称为
[
岭回归
](
2#112-岭回归
)
:
高
维
统计学习中的一个解决方法是 _收缩_ 回归系数到0:任何两个随机选择的观察值数据集都很可能是不相关的。这称为
[
岭回归
](
2#112-岭回归
)
:
[
![http://sklearn.apachecn.org/cn/0.19.0/_images/sphx_glr_plot_ols_ridge_variance_002.png
](
img/6518e8fbaaadd8a258c9a3f96b2ef42e.jpg
)
](https://scikit-learn.org/stable/auto_examples/linear_model/plot_ols_ridge_variance.html)
```
py
```
py
thon
>>>
regr
=
linear_model
.
Ridge
(
alpha
=
.
1
)
>>>
plt
.
figure
()
...
...
@@ -172,21 +170,19 @@ LinearRegression(copy_X=True, fit_intercept=True, n_jobs=1, normalize=False)
...
regr
.
fit
(
this_X
,
y
)
...
plt
.
plot
(
test
,
regr
.
predict
(
test
))
...
plt
.
scatter
(
this_X
,
y
,
s
=
3
)
```
这是
**bias/variance tradeoff**
中的一个例子:岭参数
`alpha`
越大,偏差越大,方差越小。
我们可以选择
`alpha`
来最小化排除错误,这里使用糖尿病数据集而不是人为数据
:
我们可以选择
`alpha`
来最小化排除错误,这里使用糖尿病数据集而不是人为数据
:
```
py
```
py
thon
>>>
alphas
=
np
.
logspace
(
-
4
,
-
1
,
6
)
>>>
from
__future__
import
print_function
>>>
print
([
regr
.
set_params
(
alpha
=
alpha
...
).
fit
(
diabetes_X_train
,
diabetes_y_train
,
...
).
score
(
diabetes_X_test
,
diabetes_y_test
)
for
alpha
in
alphas
])
[
0.5851110683883
...,
0.5852073015444
...,
0.5854677540698
...,
0.5855512036503
...,
0.5830717085554
...,
0.57058999437
...]
```
>**注意**:
...
...
@@ -205,7 +201,7 @@ LinearRegression(copy_X=True, fit_intercept=True, n_jobs=1, normalize=False)
为了提高问题的条件(比如,缓解
[
维度惩罚
](
#维度惩罚
)
),只选择信息特征和设置无信息时就会变得有趣,比如特征2到0。岭回归会减小他们的值,但不会减到0.另一种抑制方法,称为
[
Lasso
](
2#113-lasso
)
(
最小绝对收缩和选择算子
)
,可以把一些系数设为0。这些方法称为
**稀疏法**
,稀疏可以看作是奥卡姆剃刀的应用:_模型越简单越好_。
```
py
```
py
thon
>>>
regr
=
linear_model
.
Lasso
()
>>>
scores
=
[
regr
.
set_params
(
alpha
=
alpha
...
).
fit
(
diabetes_X_train
,
diabetes_y_train
...
...
@@ -220,12 +216,11 @@ Lasso(alpha=0.025118864315095794, copy_X=True, fit_intercept=True,
>>>
print
(
regr
.
coef_
)
[
0.
-
212.43764548
517.19478111
313.77959962
-
160.8303982
-
0.
-
187.19554705
69.38229038
508.66011217
71.84239008
]
```
>**同一个问题的不同算法**
>
>不同的算法可以用于解决同一个数学问题。比如在 scikit-learn 里 `Lasso` 对象使用 [coordinate descent](https://en.wikipedia.org/wiki/Coordinate_descent) 方法解决 lasso 回归问题,对于大型数据集很有效。但是,scikit-learn 也提供了使用 _LARS_ 算法 的
:
[`LassoLars`](https://scikit-learn.org/stable/modules/generated/sklearn.linear_model.LassoLars.html#sklearn.linear_model.LassoLars)对象,对于处理带权向量非常稀疏的数据非常有效(比如,问题的观察值很少)。
>不同的算法可以用于解决同一个数学问题。比如在 scikit-learn 里 `Lasso` 对象使用 [coordinate descent](https://en.wikipedia.org/wiki/Coordinate_descent) 方法解决 lasso 回归问题,对于大型数据集很有效。但是,scikit-learn 也提供了使用 _LARS_ 算法 的
:
[`LassoLars`](https://scikit-learn.org/stable/modules/generated/sklearn.linear_model.LassoLars.html#sklearn.linear_model.LassoLars)对象,对于处理带权向量非常稀疏的数据非常有效(比如,问题的观察值很少)。
### 分类
...
...
@@ -237,14 +232,13 @@ Lasso(alpha=0.025118864315095794, copy_X=True, fit_intercept=True,
![
y = \textrm{sigmoid}(X\beta - \textrm{offset}) + \epsilon =\frac{1}{1 + \textrm{exp}(- X\beta + \textrm{offset})} + \epsilon
](
img/5b84281b8f1a26c9e9cba1b6cb0126ce.jpg
)
```
py
```
py
thon
>>>
logistic
=
linear_model
.
LogisticRegression
(
C
=
1e5
)
>>>
logistic
.
fit
(
iris_X_train
,
iris_y_train
)
LogisticRegression
(
C
=
100000.0
,
class_weight
=
None
,
dual
=
False
,
fit_intercept
=
True
,
intercept_scaling
=
1
,
max_iter
=
100
,
multi_class
=
'ovr'
,
n_jobs
=
1
,
penalty
=
'l2'
,
random_state
=
None
,
solver
=
'liblinear'
,
tol
=
0.0001
,
verbose
=
0
,
warm_start
=
False
)
```
这就是有名的:
[
`LogisticRegression`
](
2#1111-logistic-回归
)
...
...
@@ -263,12 +257,12 @@ LogisticRegression(C=100000.0, class_weight=None, dual=False,
>
>尝试用最近邻和线性模型分类数字数据集。留出最后 10%的数据,并测试观察值预期效果。
>
>```py
from sklearn import datasets, neighbors, linear_model
digits = datasets.load_digits()
X_digits = digits.data
y_digits = digits.target
```
>```py
thon
>
from sklearn import datasets, neighbors, linear_model
>
digits = datasets.load_digits()
>
X_digits = digits.data
>
y_digits = digits.target
>
```
>[解决方法](https://scikit-learn.org/stable/_downloads/plot_digits_classification_exercise.py)
## 支持向量机(SVMs)
...
...
@@ -277,12 +271,13 @@ y_digits = digits.target
[
支持向量机
](
5#14-支持向量机
)
属于判别模型家族:它们尝试通过找到样例的一个组合来构建一个两类之间最大化的平面。通过
`C`
参数进行正则化设置:
`C`
的值小意味着边缘是通过分割线周围的所有观测样例进行计算得到的(更正则化);
`C`
的值大意味着边缘是通过邻近分割线的观测样例计算得到的(更少正则化)。
>例子:
>例子:
>
>* [Plot different SVM classifiers in the iris dataset](https://scikit-learn.org/stable/auto_examples/svm/plot_iris.html#sphx-glr-auto-examples-svm-plot-iris-py)
SVMs 可以用于回归
:
*SVR* (支持向量回归)–,或者分类 *SVC* (支持向量分类)。
SVMs 可以用于回归
:
*SVR*
(支持向量回归)–,或者分类
*SVC*
(支持向量分类)。
```
py
```
py
thon
>>>
from
sklearn
import
svm
>>>
svc
=
svm
.
SVC
(
kernel
=
'linear'
)
>>>
svc
.
fit
(
iris_X_train
,
iris_y_train
)
...
...
@@ -290,7 +285,6 @@ SVC(C=1.0, cache_size=200, class_weight=None, coef0=0.0,
decision_function_shape
=
'ovr'
,
degree
=
3
,
gamma
=
'auto'
,
kernel
=
'linear'
,
max_iter
=-
1
,
probability
=
False
,
random_state
=
None
,
shrinking
=
True
,
tol
=
0.001
,
verbose
=
False
)
```
>**警告:规格化数据**
...
...
@@ -315,14 +309,15 @@ SVC(C=1.0, cache_size=200, class_weight=None, coef0=0.0,
>
>根据特征1和特征2,尝试用 SVMs 把1和2类从鸢尾属植物数据集中分出来。为每一个类留下10%,并测试这些观察值预期效果。
>[![http://sklearn.apachecn.org/cn/0.19.0/_images/sphx_glr_plot_iris_dataset_001.png](img/4246a718076893e37084bc69a7e16007.jpg)](https://scikit-learn.org/stable/auto_examples/datasets/plot_iris_dataset.html)
>**警告**: 类是有序的,不要留下最后10%,不然你只能测试一个类了。
>**提示**: 为了直观显示,你可以在网格上使用 `decision_function` 方法。
>```py
iris = datasets.load_iris()
X = iris.data
y = iris.target
X = X[y != 0, :2]
y = y[y != 0]
```
>**警告**:类是有序的,不要留下最后10%,不然你只能测试一个类了。
>**提示**:为了直观显示,你可以在网格上使用 `decision_function` 方法。
>
>```python
>iris = datasets.load_iris()
>X = iris.data
>y = iris.target
>X = X[y != 0, :2]
>y = y[y != 0]
>```
>
>[解决方法](https://scikit-learn.org/stable/_downloads/plot_iris_exercise.py)
>[解决方法](https://scikit-learn.org/stable/_downloads/plot_iris_exercise.py)
\ No newline at end of file
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录