sklearn:sklearn.preprocessing的MinMaxScaler简介、使用方法之详细攻略

简介: sklearn:sklearn.preprocessing的MinMaxScaler简介、使用方法之详细攻略

MinMaxScaler简介


MinMaxScaler函数解释


   """Transforms features by scaling each feature to a given range.

 

   This estimator scales and translates each feature individually such that it is in the given range on the training set, i.e. between zero and one.

 

   The transformation is given by::

 

   X_std = (X - X.min(axis=0)) / (X.max(axis=0) - X.min(axis=0))

   X_scaled = X_std * (max - min) + min

 

   where min, max = feature_range.

 

   This transformation is often used as an alternative to zero mean, unit variance scaling.

 

   Read more in the :ref:`User Guide <preprocessing_scaler>`. “”通过将每个特性缩放到给定范围来转换特性。


这个估计量对每个特征进行了缩放和单独转换,使其位于训练集的给定范围内,即在0和1之间。


变换由::


   X_std = (X - X.min(axis=0)) / (X.max(axis=0) - X.min(axis=0))

   X_scaled = X_std * (max - min) + min


其中,min, max = feature_range。


这种转换经常被用来替代零均值,单位方差缩放。


请参阅:ref: ' User Guide  '。</preprocessing_scaler>

   Parameters

   ----------

   feature_range : tuple (min, max), default=(0, 1)

   Desired range of transformed data.

 

   copy : boolean, optional, default True

   Set to False to perform inplace row normalization and avoid a copy (if the input is already a numpy array). 参数


feature_range: tuple (min, max),默认值=(0,1)

所需的转换数据范围。


复制:布尔值,可选,默认为真

设置为False执行插入行规范化并避免复制(如果输入已经是numpy数组)。

   Attributes

   ----------

   min_ : ndarray, shape (n_features,)

   Per feature adjustment for minimum.

 

   scale_ : ndarray, shape (n_features,)

   Per feature relative scaling of the data.

 

   .. versionadded:: 0.17

   *scale_* attribute.

 

   data_min_ : ndarray, shape (n_features,)

   Per feature minimum seen in the data

 

   .. versionadded:: 0.17

   *data_min_*

 

   data_max_ : ndarray, shape (n_features,)

   Per feature maximum seen in the data

 

   .. versionadded:: 0.17

   *data_max_*

 

   data_range_ : ndarray, shape (n_features,)

   Per feature range ``(data_max_ - data_min_)`` seen in the data

 

   .. versionadded:: 0.17

   *data_range_*

属性

----------

min_: ndarray, shape (n_features,)

每个功能调整为最小。


scale_: ndarray, shape (n_features,)

每个特征数据的相对缩放。


. .versionadded:: 0.17

* scale_ *属性。


data_min_: ndarray, shape (n_features,)

每个特征在数据中出现的最小值


. .versionadded:: 0.17

* data_min_ *


data_max_: ndarray, shape (n_features,)

每个特征在数据中出现的最大值


. .versionadded:: 0.17

* data_max_ *

data_range_: ndarray, shape (n_features,)

在数据中看到的每个特性范围' ' (data_max_ - data_min_) ' '


. .versionadded:: 0.17

* data_range_ *


MinMaxScaler底层代码


class MinMaxScaler Found at: sklearn.preprocessing.data

class MinMaxScaler(BaseEstimator, TransformerMixin):

   def __init__(self, feature_range=(0, 1), copy=True):

       self.feature_range = feature_range

       self.copy = copy

 

   def _reset(self):

       """Reset internal data-dependent state of the scaler, if

        necessary.

       __init__ parameters are not touched.

       """

   # Checking one attribute is enough, becase they are all set

    together

   # in partial_fit

       if hasattr(self, 'scale_'):

           del self.scale_

           del self.min_

           del self.n_samples_seen_

           del self.data_min_

           del self.data_max_

           del self.data_range_

 

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

       """Compute the minimum and maximum to be used for later

        scaling.

       Parameters

       ----------

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

           The data used to compute the per-feature minimum and

            maximum

           used for later scaling along the features axis.

       """

       # Reset internal state before fitting

       self._reset()

       return self.partial_fit(X, y)

 

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

       """Online computation of min and max on X for later scaling.

       All of X is processed as a single batch. This is intended for

        cases

       when `fit` is not feasible due to very large number of

        `n_samples`

       or because X is read from a continuous stream.

       Parameters

       ----------

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

           The data used to compute the mean and standard deviation

           used for later scaling along the features axis.

       y : Passthrough for ``Pipeline`` compatibility.

       """

       feature_range = self.feature_range

       if feature_range[0] >= feature_range[1]:

           raise ValueError(

               "Minimum of desired feature range must be smaller"

               " than maximum. Got %s." %

               str(feature_range))

       if sparse.issparse(X):

           raise TypeError("MinMaxScaler does no support sparse

            input. "

               "You may consider to use MaxAbsScaler instead.")

       X = check_array(X, copy=self.copy, warn_on_dtype=True,

        estimator=self, dtype=FLOAT_DTYPES)

       data_min = np.min(X, axis=0)

       data_max = np.max(X, axis=0)

       # First pass

       if not hasattr(self, 'n_samples_seen_'):

           self.n_samples_seen_ = X.shape[0]

       else:

           data_min = np.minimum(self.data_min_, data_min)

           data_max = np.maximum(self.data_max_, data_max)

           self.n_samples_seen_ += X.shape[0] # Next steps

       data_range = data_max - data_min

       self.scale_ = (feature_range[1] - feature_range[0]) /

        _handle_zeros_in_scale(data_range)

       self.min_ = feature_range[0] - data_min * self.scale_

       self.data_min_ = data_min

       self.data_max_ = data_max

       self.data_range_ = data_range

       return self

 

   def transform(self, X):

       """Scaling features of X according to feature_range.

       Parameters

       ----------

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

           Input data that will be transformed.

       """

       check_is_fitted(self, 'scale_')

       X = check_array(X, copy=self.copy, dtype=FLOAT_DTYPES)

       X *= self.scale_

       X += self.min_

       return X

 

   def inverse_transform(self, X):

       """Undo the scaling of X according to feature_range.

       Parameters

       ----------

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

           Input data that will be transformed. It cannot be sparse.

       """

       check_is_fitted(self, 'scale_')

       X = check_array(X, copy=self.copy, dtype=FLOAT_DTYPES)

       X -= self.min_

       X /= self.scale_

       return X


MinMaxScaler的使用方法


1、基础案例


   >>> from sklearn.preprocessing import MinMaxScaler

   >>>

   >>> data = [[-1, 2], [-0.5, 6], [0, 10], [1, 18]]

   >>> scaler = MinMaxScaler()

   >>> print(scaler.fit(data))

   MinMaxScaler(copy=True, feature_range=(0, 1))

   >>> print(scaler.data_max_)

   [  1.  18.]

   >>> print(scaler.transform(data))

   [[ 0.    0.  ]

   [ 0.25  0.25]

   [ 0.5   0.5 ]

   [ 1.    1.  ]]

   >>> print(scaler.transform([[2, 2]]))

   [[ 1.5  0. ]]


 

相关文章
|
JavaScript 前端开发 定位技术
Cesium介绍和入门
这篇文章介绍了Cesium的基本概念及其在Web开发中的应用,包括如何集成Cesium并使用它来创建和展示3D地图。
1116 4
Cesium介绍和入门
|
关系型数据库 MySQL 应用服务中间件
LNMP详解(四)——LNMP原理与简单部署
LNMP详解(四)——LNMP原理与简单部署
607 1
|
弹性计算 网络协议 安全
阿里云添加端口
阿里云添加端口
1035 0
|
机器学习/深度学习 算法 TensorFlow
维特比算法(Viterbi algorithm)
维特比算法(Viterbi algorithm)是一种用于解码隐马尔可夫模型(Hidden Markov Model,HMM)的动态规划算法。它用于找到给定观测序列条件下的最有可能的隐藏状态序列。
850 1
|
机器学习/深度学习 自然语言处理 算法
【文本摘要(1)】抽取式之textrank(无监督学习):生成200字以内摘要
【文本摘要(1)】抽取式之textrank(无监督学习):生成200字以内摘要
374 0
|
监控 算法
偏最小二乘(Partial Least Squares,PLS)原理及模型建立
偏最小二乘(Partial Least Squares,PLS)原理及模型建立
偏最小二乘(Partial Least Squares,PLS)原理及模型建立
|
11月前
|
Python
Python三引号用法与变量详解
本文详细介绍了Python中三引号(`&quot;&quot;&quot;` 或 `&#39;&#39;&#39;`)的用法,包括其基本功能、如何在多行字符串中使用变量(如f-string、str.format()和%操作符),以及实际应用示例,帮助读者更好地理解和运用这一强大工具。
976 2
|
前端开发 安全 JavaScript
Python的Flask框架的学习笔记(前后端变量传送,文件上传,网页返回)内含实战:实现一个简单的登录页面
Python的Flask框架的学习笔记(前后端变量传送,文件上传,网页返回)内含实战:实现一个简单的登录页面
400 0
|
安全 Windows
搜狗输入法双击输入框崩溃问题
【8月更文挑战第27天】搜狗输入法双击输入框崩溃可能由多种因素造成,包括软件冲突、输入法版本问题、系统故障、设置错误及硬件问题。建议检查并解决潜在冲突软件,更新输入法版本,修复系统文件,调整输入法设置,以及确保硬件正常工作。通过逐步排查,通常可定位并解决问题。
557 0