Skip to content

Commit 8638862

Browse files
committed
Merge pull request scikit-learn#4915 from untom/minmax_scale
[MRG+1] ENH add minmax_scale
2 parents d389142 + e7e22d1 commit 8638862

File tree

6 files changed

+73
-9
lines changed

6 files changed

+73
-9
lines changed

doc/modules/classes.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1108,6 +1108,7 @@ See the :ref:`metrics` section of the user guide for further details.
11081108
preprocessing.LabelBinarizer
11091109
preprocessing.LabelEncoder
11101110
preprocessing.MultiLabelBinarizer
1111+
preprocessing.MaxAbsScaler
11111112
preprocessing.MinMaxScaler
11121113
preprocessing.Normalizer
11131114
preprocessing.OneHotEncoder
@@ -1122,10 +1123,14 @@ See the :ref:`metrics` section of the user guide for further details.
11221123
preprocessing.add_dummy_feature
11231124
preprocessing.binarize
11241125
preprocessing.label_binarize
1126+
preprocessing.maxabs_scale
1127+
preprocessing.minmax_scale
11251128
preprocessing.normalize
1129+
preprocessing.robust_scale
11261130
preprocessing.scale
11271131

11281132

1133+
11291134
:mod:`sklearn.qda`: Quadratic Discriminant Analysis
11301135
===================================================
11311136

doc/modules/preprocessing.rst

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -173,9 +173,9 @@ Here is how to use the toy data from the previous example with this scaler::
173173
array([ 2., 1., 2.])
174174

175175

176-
As with :func:`scale`, the module further provides a
177-
convenience function :func:`maxabs_scale` if you don't want to
178-
create an object.
176+
As with :func:`scale`, the module further provides convenience functions
177+
:func:`minmax_scale` and :func:`maxabs_scale` if you don't want to create
178+
an object.
179179

180180

181181
Scaling sparse data
@@ -507,4 +507,4 @@ The features of X have been transformed from :math:`(X_1, X_2, X_3)` to :math:`(
507507

508508
Note that polynomial features are used implicitily in `kernel methods <http://en.wikipedia.org/wiki/Kernel_method>`_ (e.g., :class:`sklearn.svm.SVC`, :class:`sklearn.decomposition.KernelPCA`) when using polynomial :ref:`svm_kernels`.
509509

510-
See :ref:`example_linear_model_plot_polynomial_interpolation.py` for Ridge regression using created polynomial features.
510+
See :ref:`example_linear_model_plot_polynomial_interpolation.py` for Ridge regression using created polynomial features.

doc/whats_new.rst

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ Enhancements
6464
:class:`linear_model.LogisticRegression`, by avoiding loss computation.
6565
By `Mathieu Blondel`_ and `Tom Dupre la Tour`_.
6666

67-
- The ``class_weight="auto"`` heuristic in classifiers supporting
67+
- The ``class_weight="auto"`` heuristic in classifiers supporting
6868
``class_weight`` was deprecated and replaced by the ``class_weight="balanced"``
6969
option, which has a simpler forumlar and interpretation.
7070
By Hanna Wallach and `Andreas Müller`_.
@@ -85,6 +85,9 @@ Enhancements
8585
- Provide an option for sparse output from
8686
:func:`sklearn.metrics.pairwise.cosine_similarity`. By `Jaidev Deshpande`_.
8787

88+
- Add :func:`minmax_scale` to provide a function interface for
89+
:class:`MinMaxScaler`. By `Thomas Unterthiner`_.
90+
8891
Bug fixes
8992
.........
9093

sklearn/preprocessing/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from .data import scale
1717
from .data import robust_scale
1818
from .data import maxabs_scale
19+
from .data import minmax_scale
1920
from .data import OneHotEncoder
2021

2122
from .data import PolynomialFeatures
@@ -47,5 +48,6 @@
4748
'scale',
4849
'robust_scale',
4950
'maxabs_scale',
51+
'minmax_scale',
5052
'label_binarize',
5153
]

sklearn/preprocessing/data.py

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
'scale',
4444
'robust_scale',
4545
'maxabs_scale',
46+
'minmax_scale',
4647
]
4748

4849

@@ -194,20 +195,20 @@ def scale(X, axis=0, with_mean=True, with_std=True, copy=True):
194195

195196

196197
class MinMaxScaler(BaseEstimator, TransformerMixin):
197-
"""Standardizes features by scaling each feature to a given range.
198+
"""Transforms features by scaling each feature to a given range.
198199
199200
This estimator scales and translates each feature individually such
200201
that it is in the given range on the training set, i.e. between
201202
zero and one.
202203
203-
The standardization is given by::
204+
The transformation is given by::
204205
205206
X_std = (X - X.min(axis=0)) / (X.max(axis=0) - X.min(axis=0))
206207
X_scaled = X_std * (max - min) + min
207208
208209
where min, max = feature_range.
209210
210-
This standardization is often used as an alternative to zero mean,
211+
This transformation is often used as an alternative to zero mean,
211212
unit variance scaling.
212213
213214
Read more in the :ref:`User Guide <preprocessing_scaler>`.
@@ -289,6 +290,45 @@ def inverse_transform(self, X):
289290
return X
290291

291292

293+
def minmax_scale(X, feature_range=(0, 1), axis=0, copy=True):
294+
"""Transforms features by scaling each feature to a given range.
295+
296+
This estimator scales and translates each feature individually such
297+
that it is in the given range on the training set, i.e. between
298+
zero and one.
299+
300+
The transformation is given by::
301+
302+
X_std = (X - X.min(axis=0)) / (X.max(axis=0) - X.min(axis=0))
303+
X_scaled = X_std * (max - min) + min
304+
305+
where min, max = feature_range.
306+
307+
This transformation is often used as an alternative to zero mean,
308+
unit variance scaling.
309+
310+
Read more in the :ref:`User Guide <preprocessing_scaler>`.
311+
312+
Parameters
313+
----------
314+
feature_range: tuple (min, max), default=(0, 1)
315+
Desired range of transformed data.
316+
317+
axis : int (0 by default)
318+
axis used to scale along. If 0, independently scale each feature,
319+
otherwise (if 1) scale each sample.
320+
321+
copy : boolean, optional, default is True
322+
Set to False to perform inplace scaling and avoid a copy (if the input
323+
is already a numpy array).
324+
"""
325+
s = MinMaxScaler(feature_range=feature_range, copy=copy)
326+
if axis == 0:
327+
return s.fit_transform(X)
328+
else:
329+
return s.fit_transform(X.T).T
330+
331+
292332
class StandardScaler(BaseEstimator, TransformerMixin):
293333
"""Standardize features by removing the mean and scaling to unit variance
294334
@@ -337,7 +377,7 @@ class StandardScaler(BaseEstimator, TransformerMixin):
337377
The mean value for each feature in the training set.
338378
339379
std_ : array of floats with shape [n_features]
340-
The standard deviation for each feature in the training set.
380+
The standard deviation for each feature in the training set.
341381
Set to one if the standard deviation is zero for a given feature.
342382
343383
See also

sklearn/preprocessing/tests/test_data.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
from sklearn.preprocessing.data import StandardScaler
2828
from sklearn.preprocessing.data import scale
2929
from sklearn.preprocessing.data import MinMaxScaler
30+
from sklearn.preprocessing.data import minmax_scale
3031
from sklearn.preprocessing.data import MaxAbsScaler
3132
from sklearn.preprocessing.data import maxabs_scale
3233
from sklearn.preprocessing.data import RobustScaler
@@ -260,6 +261,19 @@ def test_min_max_scaler_zero_variance_features():
260261
[1., 1., 2.0]]
261262
assert_array_almost_equal(X_trans, X_expected_1_2)
262263

264+
# function interface
265+
X_trans = minmax_scale(X)
266+
assert_array_almost_equal(X_trans, X_expected_0_1)
267+
X_trans = minmax_scale(X, feature_range=(1, 2))
268+
assert_array_almost_equal(X_trans, X_expected_1_2)
269+
270+
271+
def test_minmax_scale_axis1():
272+
X = iris.data
273+
X_trans = minmax_scale(X, axis=1)
274+
assert_array_almost_equal(np.min(X_trans, axis=1), 0)
275+
assert_array_almost_equal(np.max(X_trans, axis=1), 1)
276+
263277

264278
def test_min_max_scaler_1d():
265279
# Test scaling of dataset along single axis

0 commit comments

Comments
 (0)