70.md 2.6 KB
Newer Older
片刻小哥哥's avatar
片刻小哥哥 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
# 机器学习: scikit-learn 中的设置以及预估对象

校验者:
        [@Kyrie](https://github.com/apachecn/scikit-learn-doc-zh)
        [@片刻](https://github.com/apachecn/scikit-learn-doc-zh)
翻译者:
        [@冰块](https://github.com/apachecn/scikit-learn-doc-zh)

## 数据集

Scikit-learn可以从一个或者多个数据集中学习信息,这些数据集合可表示为2维阵列,也可认为是一个列表。列表的第一个维度代表 **样本** ,第二个维度代表 **特征** (每一行代表一个样本,每一列代表一种特征)。

样例: iris 数据集(鸢尾花卉数据集)

```py
>>> from sklearn import datasets
>>> iris = datasets.load_iris()
>>> data = iris.data
>>> data.shape
(150, 4)

```

这个数据集包含150个样本,每个样本包含4个特征:花萼长度,花萼宽度,花瓣长度,花瓣宽度,详细数据可以通过``iris.DESCR``查看。

如果原始数据不是``(n_samples, n_features)``的形状时,使用之前需要进行预处理以供scikit-learn使用。

数据预处理样例:digits数据集(手写数字数据集)

[![http://sklearn.apachecn.org/cn/0.19.0/_images/sphx_glr_plot_digits_last_image_001.png](img/377b02d08a6b388008fc8cb132080d20.jpg)](../../auto_examples/datasets/plot_digits_last_image.html)

digits数据集包含1797个手写数字的图像,每个图像为8*8像素

```py
>>> digits = datasets.load_digits()
>>> digits.images.shape
(1797, 8, 8)
>>> import matplotlib.pyplot as plt 
>>> plt.imshow(digits.images[-1], cmap=plt.cm.gray_r) 
<matplotlib.image.AxesImage object at ...>

```

为了在scikit中使用这一数据集,需要将每一张8×8的图像转换成长度为64的特征向量

```py
>>> data = digits.images.reshape((digits.images.shape[0], -1))

```

## 预估对象

**拟合数据**: scikit-learn实现最重要的一个API是`estimator`。estimators是基于数据进行学习的任何对象,它可以是一个分类器,回归或者是一个聚类算法,或者是从原始数据中提取/过滤有用特征的变换器。

所有的拟合模型对象拥有一个名为``fit``的方法,参数是一个数据集(通常是一个2维列表):

```py
>>> estimator.fit(data)

```

**拟合模型对象构造参数**: 在创建一个拟合模型时,可以设置相关参数,在创建之后也可以修改对应的参数:

```py
>>> estimator = Estimator(param1=1, param2=2)
>>> estimator.param1
1

```

**拟合参数**: 当拟合模型完成对数据的拟合之后,可以从拟合模型中获取拟合的参数结果,所有拟合完成的参数均以下划线(_)作为结尾:

```py
>>> estimator.estimated_param_ 

```