sklearn:sklearn.preprocessing的MinMaxScaler简介、使用方法之详细攻略
sklearn:sklearn.preprocessing的MinMaxScaler簡介、使用方法之詳細攻略
?
目錄
MinMaxScaler簡介
MinMaxScaler函數解釋
MinMaxScaler底層代碼
MinMaxScaler的使用方法
1、基礎案例
?
?
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_* | 屬性
|
?
MinMaxScaler底層代碼
class MinMaxScaler Found at: sklearn.preprocessing.dataclass MinMaxScaler(BaseEstimator, TransformerMixin):def __init__(self, feature_range=(0, 1), copy=True):self.feature_range = feature_rangeself.copy = copydef _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_fitif 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 maximumused for later scaling along the features axis."""# Reset internal state before fittingself._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 caseswhen `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 deviationused for later scaling along the features axis.y : Passthrough for ``Pipeline`` compatibility."""feature_range = self.feature_rangeif 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 passif 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 stepsdata_range = data_max - data_minself.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_minself.data_max_ = data_maxself.data_range_ = data_rangereturn selfdef 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 Xdef 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. ]]?
?
?
?
?
?
?
?
?
?
?
總結
以上是生活随笔為你收集整理的sklearn:sklearn.preprocessing的MinMaxScaler简介、使用方法之详细攻略的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Python之nyoka:nyoka库函
- 下一篇: Python语言学习:三种随机函数ran