Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5965d49

Browse files
committed
Improved documentation
1 parent ef95761 commit 5965d49

File tree

3 files changed

+23
-13
lines changed

3 files changed

+23
-13
lines changed

doc/modules/preprocessing.rst

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ Scaling features to a range
103103

104104
An alternative standardization is scaling features to
105105
lie between a given minimum and maximum value, often between zero and one,
106-
or so that the maximum value of each feature is scaled to unit size.
106+
or so that the maximum absolute value of each feature is scaled to unit size.
107107
This can be achieved using :class:`MinMaxScaler` or :class:`MaxAbsScaler`,
108108
respectively.
109109

@@ -150,8 +150,7 @@ full formula is::
150150

151151
:class:`MaxAbsScaler` works in a very similar fashion, but scales data so
152152
it lies within the range ``[-1, 1]``, and is meant for data
153-
that is already centered at zero. In particular, this scaler is very well
154-
suited for sparse data.
153+
that is already centered at zero or sparse data.
155154

156155
Here is how to use the toy data from the previous example with this scaler::
157156

@@ -173,9 +172,9 @@ Here is how to use the toy data from the previous example with this scaler::
173172
array([ 2., 1., 2.])
174173

175174

176-
As with :func:`scale`, the ``preprocessing`` module further provides a
177-
convenience function function :func:`maxabs_scale` if you don't want to use
178-
the `Transformer` API.
175+
As with :func:`scale`, the module further provides a
176+
convenience function function :func:`maxabs_scale` if you don't want to
177+
create an object.
179178

180179

181180
Scaling sparse data
@@ -191,8 +190,8 @@ matrices as input, as long as ``with_centering=False`` is explicitly passed
191190
to the constructor. Otherwise a ``ValueError`` will be raised as
192191
silently centering would break the sparsity and would often crash the
193192
execution by allocating excessive amounts of memory unintentionally.
194-
:class:`RobustScaler` cannot be `fit`ted to sparse inputs, but you can use the
195-
`transform` method on sparse inputs.
193+
:class:`RobustScaler` cannot be fited to sparse inputs, but you can use
194+
the ``transform`` method on sparse inputs.
196195

197196
Note that the scalers accept both Compressed Sparse Rows and Compressed
198197
Sparse Columns format (see ``scipy.sparse.csr_matrix`` and

sklearn/preprocessing/data.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -441,11 +441,12 @@ def inverse_transform(self, X, copy=None):
441441

442442

443443
class MaxAbsScaler(BaseEstimator, TransformerMixin):
444-
"""Scale each feature to the [-1, 1] range without breaking the sparsity.
444+
"""Scale each feature by its maximum absolute value.
445445
446446
This estimator scales and translates each feature individually such
447447
that the maximal absolute value of each feature in the
448-
training set will be 1.0.
448+
training set will be 1.0. It does not shift/center the data, and
449+
thus does not destroy any sparsity.
449450
450451
This scaler can also be applied to sparse CSR or CSC matrices.
451452
@@ -501,7 +502,7 @@ def transform(self, X, y=None):
501502
if sparse.issparse(X):
502503
if X.shape[0] == 1:
503504
inplace_row_scale(X, 1.0 / self.scale_)
504-
elif self.axis == 0:
505+
else:
505506
inplace_column_scale(X, 1.0 / self.scale_)
506507
else:
507508
X /= self.scale_
@@ -532,7 +533,7 @@ def inverse_transform(self, X):
532533
def maxabs_scale(X, axis=0, copy=True):
533534
"""Scale each feature to the [-1, 1] range without breaking the sparsity.
534535
535-
This estimator scales and translates each feature individually such
536+
This estimator scales each feature individually such
536537
that the maximal absolute value of each feature in the
537538
training set will be 1.0.
538539

sklearn/preprocessing/tests/test_data.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -573,7 +573,6 @@ def test_maxabs_scaler_zero_variance_features():
573573
[0., 1., +1.5],
574574
[0., 0., +0.0]]
575575

576-
# default params
577576
scaler = MaxAbsScaler()
578577
X_trans = scaler.fit_transform(X)
579578
X_expected = [[0., 1., 1.0 / 3.0],
@@ -595,6 +594,17 @@ def test_maxabs_scaler_zero_variance_features():
595594

596595
assert_array_almost_equal(X_trans_new, X_expected_new, decimal=2)
597596

597+
# sparse data
598+
X_csr = sparse.csr_matrix(X)
599+
X_trans = scaler.fit_transform(X_csr)
600+
X_expected = [[0., 1., 1.0 / 3.0],
601+
[0., 1., -0.2],
602+
[0., 1., 1.0],
603+
[0., 0., 0.0]]
604+
assert_array_almost_equal(X_trans.A, X_expected)
605+
X_trans_inv = scaler.inverse_transform(X_trans)
606+
assert_array_almost_equal(X, X_trans_inv.A)
607+
598608

599609
def test_maxabs_scaler_large_negative_value():
600610
"""Check MaxAbsScaler on toy data with a large negative value"""

0 commit comments

Comments
 (0)