Repository: scikit-learn/scikit-learn
Pull request: DOC Update docs for StandardScaler.scale_ to include 0 variance
#### Reference Issues/PRs
Fixes #19104

#### What does this implement/fix? Explain your changes.
Updates the docstring of StandardScaler's scale_ attribute to include the scenario when variance is 0, so that scale_ is not sqrt(var).

#### Any other comments?


Problem statement:
Standard Scalar scaler.scale_ = np.sqrt(scaler.var_)
Can someone please double check the code for StandardScaler attributes .scale_ and .var_ attributes? .scale_ is suppose to represent the standard deviation which is the square root of the variance. I used the default settings for Scikit-learn 0.23.2 and found that my .scale_ was 1.0 and my .var_ was 0.

Hints from issue discussion:
>  .scale_ is suppose to represent the standard deviation

Not really, it's supposed to represent the scaling of the data by the standard scaler, which tries to achieve zero mean and unit variance.
If a variance is 0, we can't achieve unit variance, and we leave the data as-is, which means the scaling factor is 1.

If you want to compute the standard deviation, I would suggest using np.std.

Looks like there is an issue with the documentation, though, as that's not mentioned:
https://github.com/scikit-learn/scikit-learn/blob/master/sklearn/preprocessing/_data.py#L619

Commit message: Fixes #19104: Update documentation of StandardScaler.scale_ to include 0
variance

Patch to review:
diff --git a/sklearn/preprocessing/_data.py b/sklearn/preprocessing/_data.py
index 478d41ecc768a..573e9f0dc1805 100644
--- a/sklearn/preprocessing/_data.py
+++ b/sklearn/preprocessing/_data.py
@@ -617,8 +617,11 @@ class StandardScaler(TransformerMixin, BaseEstimator):
     Attributes
     ----------
     scale_ : ndarray of shape (n_features,) or None
-        Per feature relative scaling of the data. This is calculated using
-        `np.sqrt(var_)`. Equal to ``None`` when ``with_std=False``.
+        Per feature relative scaling of the data to achieve zero mean and unit
+        variance. Generally this is calculated using `np.sqrt(var_)`. If a
+        variance is zero, we can't achieve unit variance, and the data is left
+        as-is, giving the scaling factor of 1. Equal to ``None`` when
+        ``with_std=False``.
 
         .. versionadded:: 0.17
            *scale_*


Code review comments:
diff_hunk: @@ -617,8 +617,11 @@ class StandardScaler(TransformerMixin, BaseEstimator):
     Attributes
     ----------
     scale_ : ndarray of shape (n_features,) or None
-        Per feature relative scaling of the data. This is calculated using
-        `np.sqrt(var_)`. Equal to ``None`` when ``with_std=False``.
+        Per feature relative scaling of the data to achieve zero mean and unit
+        variance. Generally this is calculated using `np.sqrt(var_)`. If a
+        variance is zero, we can't achieve unit variance, and the data is left
+        as-is, giving the scaling factor of 1. Equal to ``None`` when
+        ``with_std=False``.
path: sklearn/preprocessing/_data.py
text: @user1:
"Equal to `None`" is now physically further from the noun. We can make it clearer by:

```suggestion
        as-is, giving a scaling factor of 1. `scale_` is equal to `None`
        when `with_std=False`.
```