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

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

目录

MinMaxScaler简介

MinMaxScaler函数解释

MinMaxScaler底层代码

MinMaxScaler的使用方法

1、基础案例

MinMaxScaler简介

MinMaxScaler函数解释

image.png

image.png

image.png

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. ]]



image.pngimage.pngimage.pngimage.png

相关文章
|
3月前
|
机器学习/深度学习 算法 Python
sklearn基础教程
sklearn基础教程
65 0
|
6月前
|
数据可视化 Python
如何使用Sklearn库实现线性回归
使用Sklearn实现线性回归的步骤:导入numpy, matplotlib, LinearRegression, train_test_split和metrics模块;准备数据集;划分训练集和测试集;创建线性回归模型;训练模型并预测;计算MSE和R²评估性能;可视化预测结果。示例代码展示了这些步骤,包括数据生成、模型训练及结果展示。
66 6
|
6月前
|
数据可视化
如何使用Sklearn库实现线性回归?
使用Sklearn实现线性回归的步骤包括导入库、准备数据、划分训练测试集、创建模型、训练预测、评估性能和可视化。
51 0
|
数据可视化 Python
sklearn之XGBModel:XGBModel之feature_importances_、plot_importance的简介、使用方法之详细攻略
sklearn之XGBModel:XGBModel之feature_importances_、plot_importance的简介、使用方法之详细攻略
sklearn:sklearn.preprocessing中的Standardization、Scaling、 Normalization简介、使用方法之详细攻略
sklearn:sklearn.preprocessing中的Standardization、Scaling、 Normalization简介、使用方法之详细攻略
sklearn:sklearn.preprocessing中的Standardization、Scaling、 Normalization简介、使用方法之详细攻略
|
存储 计算机视觉 索引
sklearn:sklearn.GridSearchCV函数的简介、使用方法之详细攻略
sklearn:sklearn.GridSearchCV函数的简介、使用方法之详细攻略
【sklearn】KFold、StratifiedKFold、GroupKFold的区别
【sklearn】KFold、StratifiedKFold、GroupKFold的区别
313 0
Python之sklearn-pmml:sklearn-pmml的简介、安装、使用方法之详细攻略
Python之sklearn-pmml:sklearn-pmml的简介、安装、使用方法之详细攻略
Python之sklearn-pmml:sklearn-pmml的简介、安装、使用方法之详细攻略
|
机器学习/深度学习 Python
Python之sklearn2pmml:sklearn2pmml库函数的简介、安装、使用方法之详细攻略daiding
Python之sklearn2pmml:sklearn2pmml库函数的简介、安装、使用方法之详细攻略daiding
Python之sklearn2pmml:sklearn2pmml库函数的简介、安装、使用方法之详细攻略daiding
|
机器学习/深度学习 算法 异构计算
Py之lightgbm:lightgbm的简介、安装、使用方法之详细攻略(一)
Py之lightgbm:lightgbm的简介、安装、使用方法之详细攻略
Py之lightgbm:lightgbm的简介、安装、使用方法之详细攻略(一)