scikit-learn/scikit-learn — pull request #3639 by untom (2014-09-05 08:18:24+00:00)
Title: [WIP] ENH Refactor scaler code

This PR refactors the scalers in `preprocessing` to use a common baseclass (`BaseScaler`). This is done to share some common functionality / avoid code duplication. This PR is part of a series of PR that split up #2514 , and other PRs in this series intend to introduce new scalers that will make use of `BaseScaler` (e.g. a scaler that uses robust statistics).

There is one issues with this PR that needs to be discussed:

All scalers (both the existing ones and the one's I'm going to introduce) do center the data and then scale it to fall into some range. So in essence, all layers do

(x - some_centering_statistic) / some_scaling_statistic

for each feature-column x. The existing scalers already have attributes that expose their centering/scaling statistic, however those are not uniformly named. I propose that the `StandardScaler.mean_`/`StandardScaler.std_`/`MinMaxScaler.scale_` and `MinMaxScaler.min_` properties are being deprecated and replaced with `*.center_`/`*.scale_`, and the `with_mean`/`with_std` constructer arguments be renamed to `with_centering`/`with_scaling`.

@larsmans already said back in #2514 that he is against renaming the `mean_`/`std_` attributes, since the scaler API should be stable. An additional problem is that my proposal would change the meaning of `MinMaxScaler.scale_` (as the previous implementation used a slightly different math to arrive at the same scaling result). This will of course break user code that uses `MinMaxScaler.scale_` and relies on its current implementation. (NOTE: I had overlooked this when proposing #2514 and just noticed it when preparing this PR)

There are several ways to deal with this:

* don't expose the new `scale_`/`center_` arguments in StandardScaler/MinMaxScaler, and just keep the old `mean_`/`std_`/`scale_` arguments.
* deprecate `mean_`/`std_` and remove them after a few releases. Print a warning whenever a user uses `scale_` on `MinMaxScaler` that the value has changed.
* ???

I would really appreciate any input on what the right thing to do here is!

*NOTE:* As side-effect of this PR, `StandardScaler` and `MinMaxScaler` gain the ability to scale on other rows/samples instead of just columns/features (i.e., it is possible to use `axis=1`). However, no tests for this new functionality is included in this PR. This is because I intend to send another PR tomorrow that refactors the tests in this module (and adds the tests for `axis = 1`). I just thought that splitting up the tests makes it easier to review the changes (but I can add that other PR to this one if you wish).