ML之sklearn:sklearn的make_pipeline函数、RobustScaler函数、KFold函数、cross_val_score函数的代码解释、使用方法之详细攻略(一)

简介: ML之sklearn:sklearn的make_pipeline函数、RobustScaler函数、KFold函数、cross_val_score函数的代码解释、使用方法之详细攻略

sklearn的make_pipeline函数的代码解释、使用方法


         为了简化构建变换和模型链的过程,Scikit-Learn提供了pipeline类,可以将多个处理步骤合并为单个Scikit-Learn估计器。pipeline类本身具有fit、predict和score方法,其行为与Scikit-Learn中的其他模型相同。


sklearn的make_pipeline函数的代码解释


def make_pipeline(*steps, **kwargs):

   """Construct a Pipeline from the given estimators.

   This is a shorthand for the Pipeline constructor; it does not require, and does not permit, naming the estimators. Instead, their names will be set  to the lowercase of their types automatically.

   Parameters

   ----------

   *steps : list of estimators,

   memory : None, str or object with the joblib.Memory interface, optional

       Used to cache the fitted transformers of the pipeline. By default, no caching is performed. If a string is given, it is the path to the caching directory. Enabling caching triggers a clone of  the transformers before fitting. Therefore, the transformer  instance given to the pipeline cannot be inspected directly. Use the attribute ``named_steps`` or ``steps`` to  inspect estimators within the pipeline. Caching the transformers is advantageous when fitting is time consuming.

根据给定的估算器构造一条管道。

这是管道构造函数的简写;它不需要,也不允许命名估算器。相反,它们的名称将自动设置为类型的小写。

参数

   ----------

*steps :评估表、

memory:无,str或带有joblib的对象。内存接口,可选

用于缓存安装在管道中的变压器。默认情况下,不执行缓存。如果给定一个字符串,它就是到缓存目录的路径。启用缓存会在安装前触发变压器的克隆。因此,给管线的变压器实例不能直接检查。使用属性' ' named_steps ' ' '或' ' steps ' '检查管道中的评估器。当装配耗时时,缓存变压器是有利的。

   Examples

   --------

   >>> from sklearn.naive_bayes import GaussianNB

   >>> from sklearn.preprocessing import StandardScaler

   >>> make_pipeline(StandardScaler(), GaussianNB(priors=None))

   ...     # doctest: +NORMALIZE_WHITESPACE

   Pipeline(memory=None,

            steps=[('standardscaler',

                    StandardScaler(copy=True, with_mean=True, with_std=True)),

                   ('gaussiannb', GaussianNB(priors=None))])

   Returns

   -------

   p : Pipeline

   """

   memory = kwargs.pop('memory', None)

   if kwargs:

       raise TypeError('Unknown keyword arguments: "{}"'

                       .format(list(kwargs.keys())[0]))

   return Pipeline(_name_estimators(steps), memory=memory)



sklearn的make_pipeline函数的使用方法


   Examples

   --------

   >>> from sklearn.naive_bayes import GaussianNB

   >>> from sklearn.preprocessing import StandardScaler

   >>> make_pipeline(StandardScaler(), GaussianNB(priors=None))

   ...     # doctest: +NORMALIZE_WHITESPACE

   Pipeline(memory=None,

            steps=[('standardscaler',

                    StandardScaler(copy=True, with_mean=True, with_std=True)),

                   ('gaussiannb', GaussianNB(priors=None))])

   Returns

   -------

   p : Pipeline


1、使用Pipeline类来表示在使用MinMaxScaler缩放数据之后再训练一个SVM的工作流程


from sklearn.pipeline import Pipeline

pipe = Pipeline([("scaler",MinMaxScaler()),("svm",SVC())])

pip.fit(X_train,y_train)

pip.score(X_test,y_test)


2、make_pipeline函数创建管道


用Pipeline类构建管道时语法有点麻烦,我们通常不需要为每一个步骤提供用户指定的名称,这种情况下,就可以用make_pipeline函数创建管道,它可以为我们创建管道并根据每个步骤所属的类为其自动命名。


from sklearn.pipeline import make_pipeline

pipe = make_pipeline(MinMaxScaler(),SVC())


参考文章

《Python机器学习基础教程》构建管道(make_pipeline)

Python sklearn.pipeline.make_pipeline() Examples



sklearn的RobustScaler函数的代码解释、使用方法


RobustScaler函数的代码解释


class RobustScaler(BaseEstimator, TransformerMixin):

   """Scale features using statistics that are robust to outliers.

   This Scaler removes the median and scales the data according to  the quantile range (defaults to IQR: Interquartile Range).

   The IQR is the range between the 1st quartile (25th quantile)  and the 3rd quartile (75th quantile). Centering and scaling happen independently on each feature (or each sample, depending on the ``axis`` argument) by computing the relevant statistics on the samples in the training set. Median and  interquartile  range are then stored to be used on later data using the ``transform`` method.

   Standardization of a dataset is a common requirement for many  machine learning estimators. Typically this is done by removing the mean and scaling to unit variance. However, outliers can often influence the sample mean / variance in a negative way. In such cases, the median and  the interquartile range often give better results.

   .. versionadded:: 0.17

   Read more in the :ref:`User Guide <preprocessing_scaler>`.

   Parameters

   ----------

   with_centering : boolean, True by default

       If True, center the data before scaling.  This will cause ``transform`` to raise an exception when attempted on sparse matrices, because centering them entails building a dense matrix which in common use cases is likely to be too large to fit in  memory.

   with_scaling : boolean, True by default

       If True, scale the data to interquartile range.

   quantile_range : tuple (q_min, q_max), 0.0 < q_min < q_max < 100.0

       Default: (25.0, 75.0) = (1st quantile, 3rd quantile) = IQR

       Quantile range used to calculate ``scale_``.

       .. versionadded:: 0.18

   copy : boolean, optional, default is True

       If False, try to avoid a copy and do inplace scaling instead. This is not guaranteed to always work inplace; e.g. if the data is not a NumPy array or scipy.sparse CSR matrix, a copy may still be

       returned.

   Attributes

   ----------

   center_ : array of floats

       The median value for each feature in the training set.

   scale_ : array of floats

       The (scaled) interquartile range for each feature in the training set.

       .. versionadded:: 0.17

          *scale_* attribute.

   See also

   --------

   robust_scale: Equivalent function without the estimator API.

   :class:`sklearn.decomposition.PCA`

       Further removes the linear correlation across features with   'whiten=True'.

   Notes

   -----

   For a comparison of the different scalers, transformers, and normalizers, see :ref:`examples/preprocessing/plot_all_scaling.py

   <sphx_glr_auto_examples_preprocessing_plot_all_scaling.py>`.

  https://en.wikipedia.org/wiki/Median_(statistics)

  https://en.wikipedia.org/wiki/Interquartile_range

   """

使用对离群值稳健的统计数据来衡量特征。

这个标量去除中值,并根据分位数范围(默认为IQR:四分位数范围)对数据进行缩放。

IQR是第1个四分位数(第25分位数)和第3个四分位数(第75分位数)之间的范围。通过计算训练集中样本的相关统计数据,在每个特征(或每个样本,取决于“轴”参数)上独立地进行定心和缩放。然后,中值和四分位范围被存储起来,以便使用“变换”方法在以后的数据中使用。

数据集的标准化是许多机器学习估计器的常见需求。这通常是通过去除平均值和缩放到单位方差来实现的。然而,异常值往往会对样本均值/方差产生负面影响。在这种情况下,中位数和四分位范围通常会得到更好的结果。

. .versionadded:: 0.17

详见:ref: ' User Guide  '。</preprocessing_scaler>

参数

----------

with_center:boolean,默认为True

如果为真,在缩放前将数据居中。这将导致“转换”在尝试处理稀疏矩阵时引发异常,因为围绕它们需要构建一个密集的矩阵,在常见的用例中,这个矩阵可能太大而无法装入内存。

with_scaling:布尔值,默认为True

如果为真,将数据缩放到四分位范围。

quantile_range:元组(q_min, q_max), 0.0 < q_min < q_max < 100.0

默认:(25.0,75.0)=(第1分位数,第3分位数)= IQR

用于计算' ' scale_ ' '的分位数范围。

. .versionadded:: 0.18

布尔值,可选,默认为真

如果为False,则尽量避免复制,而改为就地缩放。这并不能保证总是有效的;例如,如果数据不是一个NumPy数组或scipy。稀疏CSR矩阵,仍可复制

返回。

属性

----------

浮点数数组

训练集中每个特征的中值。

浮点数数组

训练集中每个特征的(缩放的)四分位范围。

. .versionadded:: 0.17

* scale_ *属性。

另请参阅

--------

没有estimator API的等价函数。

类:“sklearn.decomposition.PCA”

进一步消除了“whiten=True”特征之间的线性相关性。

笔记

-----

对于不同的标量、转换器和规整器的比较,请参见:ref: ' examples/preprocessing/ plot_all_scale .py

< sphx_glr_auto_examples_preprocessing_plot_all_scaling.py >”。

https://en.wikipedia.org/wiki/Median_(统计)

https://en.wikipedia.org/wiki/Interquartile_range

”“”

   def __init__(self, with_centering=True, with_scaling=True,

                quantile_range=(25.0, 75.0), copy=True):

       self.with_centering = with_centering

       self.with_scaling = with_scaling

       self.quantile_range = quantile_range

       self.copy = copy

   def _check_array(self, X, copy):

       """Makes sure centering is not enabled for sparse matrices."""

       X = check_array(X, accept_sparse=('csr', 'csc'), copy=self.copy,

                       estimator=self, dtype=FLOAT_DTYPES)

       if sparse.issparse(X):

           if self.with_centering:

               raise ValueError(

                   "Cannot center sparse matrices: use `with_centering=False`"

                   " instead. See docstring for motivation and alternatives.")

       return X

   def fit(self, X, y=None):

       """Compute the median and quantiles to be used for scaling.

       Parameters

       ----------

       X : array-like, shape [n_samples, n_features]

           The data used to compute the median and quantiles

           used for later scaling along the features axis.

       """

       if sparse.issparse(X):

           raise TypeError("RobustScaler cannot be fitted on sparse inputs")

       X = self._check_array(X, self.copy)

       if self.with_centering:

           self.center_ = np.median(X, axis=0)

       if self.with_scaling:

           q_min, q_max = self.quantile_range

           if not 0 <= q_min <= q_max <= 100:

               raise ValueError("Invalid quantile range: %s" %

                                str(self.quantile_range))

           q = np.percentile(X, self.quantile_range, axis=0)

           self.scale_ = (q[1] - q[0])

           self.scale_ = _handle_zeros_in_scale(self.scale_, copy=False)

       return self

   def transform(self, X):

       """Center and scale the data.

       Can be called on sparse input, provided that ``RobustScaler`` has been

       fitted to dense input and ``with_centering=False``.

       Parameters

       ----------

       X : {array-like, sparse matrix}

           The data used to scale along the specified axis.

       """

       if self.with_centering:

           check_is_fitted(self, 'center_')

       if self.with_scaling:

           check_is_fitted(self, 'scale_')

       X = self._check_array(X, self.copy)

       if sparse.issparse(X):

           if self.with_scaling:

               inplace_column_scale(X, 1.0 / self.scale_)

       else:

           if self.with_centering:

               X -= self.center_

           if self.with_scaling:

               X /= self.scale_

       return X

   def inverse_transform(self, X):

       """Scale back the data to the original representation

       Parameters

       ----------

       X : array-like

           The data used to scale along the specified axis.

       """

       if self.with_centering:

           check_is_fitted(self, 'center_')

       if self.with_scaling:

           check_is_fitted(self, 'scale_')

       X = self._check_array(X, self.copy)

       if sparse.issparse(X):

           if self.with_scaling:

               inplace_column_scale(X, self.scale_)

       else:

           if self.with_scaling:

               X *= self.scale_

           if self.with_centering:

               X += self.center_

       return X

 


相关文章
|
机器学习/深度学习 测试技术 TensorFlow
dataset.py代码解释
这段代码主要定义了三个函数来创建 TensorFlow 数据集对象,这些数据集对象将被用于训练、评估和推断神经网络模型。
|
机器学习/深度学习 数据采集 搜索推荐
training.py的代码解释
labels、test_loss_op 和 mae_ops 计算模型的性能指标。最后,我们输出当前 epoch 的训练损失、测试损失和平均绝对误差(MAE),并保存模型参数(如果 MAE 小于 0.9)。 整个代码的目的是使用协同过滤算法建立电影推荐系统的模型,训练模型并计算模型的性能指标。
|
机器学习/深度学习 搜索推荐 TensorFlow
inference.py的代码解释
这是一个 Python 脚本,它用于导出经过训练的模型,使其可以在生产环境中进行推理。该脚本首先使用 TensorFlow 的 flags 定义了一些参数,如模型版本号、模型路径、输出目录等等。然后,它创建了一个名为 inference_graph 的 TensorFlow 图,并定义了一个 InferenceModel,该模型用于从输入数据中推断评级。
357 0
|
机器学习/深度学习 数据可视化 PyTorch
visdom的用法,详细的介绍torch相关案例
Visdom是一款用于创建交互式可视化的Python库,通常在深度学习中用于监视训练进度和可视化结果。在PyTorch中,可以使用Visdom轻松地创建图形和可视化数据。在这个例子中,我们首先使用 torchvision.utils.make_grid 方法加载了一批随机生成的图像,并使用 visdom.image 方法将其可视化。然后,我们创建了一些随机的二维数据,并使用 visdom.scatter 方法将其可视化为散点图。 需要注意的是,Visdom的Web界面支持实时更新,这意味着在训练模型或执行其他任务时,可以使用Visdom实时监视进度和结果。
192 0
|
自然语言处理 数据可视化 Java
Python中enumerate函数的解释和可视化
Python中enumerate函数的解释和可视化
Python中enumerate函数的解释和可视化
|
存储 机器学习/深度学习 算法
(读Yolo3源码发现的不会的函数)Pytorch常用函数记录-pretrained-torch.nn.Upsample()函数-torch.cat-a.permute-a.view()等
(读Yolo3源码发现的不会的函数)Pytorch常用函数记录-pretrained-torch.nn.Upsample()函数-torch.cat-a.permute-a.view()等
|
TensorFlow 算法框架/工具
Python-Tensorflow基础(二)变量 Fetch Feed 使用
Python-Tensorflow基础(二)变量 Fetch Feed 使用
74 0
|
机器学习/深度学习 存储 缓存
ML之sklearn:sklearn的make_pipeline函数、RobustScaler函数、KFold函数、cross_val_score函数的代码解释、使用方法之详细攻略
ML之sklearn:sklearn的make_pipeline函数、RobustScaler函数、KFold函数、cross_val_score函数的代码解释、使用方法之详细攻略
|
机器学习/深度学习 数据挖掘 索引
ML之sklearn:sklearn.metrics中常用的函数参数(比如confusion_matrix等 )解释及其用法说明之详细攻略
ML之sklearn:sklearn.metrics中常用的函数参数(比如confusion_matrix等 )解释及其用法说明之详细攻略
sklearn之pipeline:sklearn.pipeline函数使用及其参数解释之详细攻略
sklearn之pipeline:sklearn.pipeline函数使用及其参数解释之详细攻略

热门文章

最新文章