Skip to content

user warning in test: tests/tests_preprocessing/test_scale.py::ScaleTest::test_to_and_from_json #760

Description

@a-szulc
============================= test session starts ==============================
platform linux -- Python 3.12.3, pytest-8.3.2, pluggy-1.5.0 -- /home/adas/mljar/mljar-supervised/venv/bin/python3
cachedir: .pytest_cache
rootdir: /home/adas/mljar/mljar-supervised
configfile: pytest.ini
plugins: cov-5.0.0
collecting ... collected 1 item

tests/tests_preprocessing/test_scale.py::ScaleTest::test_to_and_from_json FAILED

=================================== FAILURES ===================================
_______________________ ScaleTest.test_to_and_from_json ________________________

self = <tests.tests_preprocessing.test_scale.ScaleTest testMethod=test_to_and_from_json>

    def test_to_and_from_json(self):
        # training data
        d = {
            "col1": [1, 2, 3, 4, 5, 6, 7, 8.0, 9, 10],
            "col2": [21, 22.0, 23, 24, 25, 26, 27, 28, 29, 30],
        }
        df = pd.DataFrame(data=d)
    
        scale = Scale(["col1"])
        scale.fit(df)
        # do not transform
        assert_almost_equal(np.mean(df["col1"]), 5.5)
        assert_almost_equal(np.mean(df["col2"]), 25.5)
        # to and from json
    
        json_data = scale.to_json()
        scale2 = Scale()
        scale2.from_json(json_data)
        # transform with loaded scaler
>       df = scale2.transform(df)

tests/tests_preprocessing/test_scale.py:79: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
supervised/preprocessing/scale.py:33: in transform
    X.loc[:, self.columns] = self.scale.transform(X[self.columns])
venv/lib/python3.12/site-packages/sklearn/utils/_set_output.py:313: in wrapped
    data_to_wrap = f(self, X, *args, **kwargs)
venv/lib/python3.12/site-packages/sklearn/preprocessing/_data.py:1045: in transform
    X = self._validate_data(
venv/lib/python3.12/site-packages/sklearn/base.py:608: in _validate_data
    self._check_feature_names(X, reset=reset)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

self = StandardScaler()
X =    col1
0   1.0
1   2.0
2   3.0
3   4.0
4   5.0
5   6.0
6   7.0
7   8.0
8   9.0
9  10.0

    def _check_feature_names(self, X, *, reset):
        """Set or check the `feature_names_in_` attribute.
    
        .. versionadded:: 1.0
    
        Parameters
        ----------
        X : {ndarray, dataframe} of shape (n_samples, n_features)
            The input samples.
    
        reset : bool
            Whether to reset the `feature_names_in_` attribute.
            If False, the input will be checked for consistency with
            feature names of data provided when reset was last True.
            .. note::
               It is recommended to call `reset=True` in `fit` and in the first
               call to `partial_fit`. All other methods that validate `X`
               should set `reset=False`.
        """
    
        if reset:
            feature_names_in = _get_feature_names(X)
            if feature_names_in is not None:
                self.feature_names_in_ = feature_names_in
            elif hasattr(self, "feature_names_in_"):
                # Delete the attribute when the estimator is fitted on a new dataset
                # that has no feature names.
                delattr(self, "feature_names_in_")
            return
    
        fitted_feature_names = getattr(self, "feature_names_in_", None)
        X_feature_names = _get_feature_names(X)
    
        if fitted_feature_names is None and X_feature_names is None:
            # no feature names seen in fit and in X
            return
    
        if X_feature_names is not None and fitted_feature_names is None:
>           warnings.warn(
                f"X has feature names, but {self.__class__.__name__} was fitted without"
                " feature names"
            )
E           UserWarning: X has feature names, but StandardScaler was fitted without feature names

venv/lib/python3.12/site-packages/sklearn/base.py:486: UserWarning
=========================== short test summary info ============================
FAILED tests/tests_preprocessing/test_scale.py::ScaleTest::test_to_and_from_json
============================== 1 failed in 1.93s ===============================

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions