Repository: scikit-learn/scikit-learn
File: sklearn/preprocessing/_polynomial.py (Python)

Code before:
            The data to transform.

        Returns
        -------
        XBS : {ndarray, sparse matrix} of shape (n_samples, n_features * n_splines)
            The matrix of features, where n_splines is the number of bases
            elements of the B-splines, n_knots + degree - 1.
        """
        check_is_fitted(self)

        X = validate_data(
            self,
            X,
            reset=False,
            accept_sparse=False,
            ensure_2d=True,
            ensure_all_finite=(self.handle_missing != "zeros"),
        )

        n_samples, n_features = X.shape
        n_splines = self.bsplines_[0].c.shape[1]
        degree = self.degree

        # Note: self.bsplines_[0].extrapolate is True for extrapolation in
        # ["periodic", "continue"]
        use_sparse = self.sparse_output
        kwargs_extrapolate = {"extrapolate": self.bsplines_[0].extrapolate}

        # Note that scipy BSpline returns float64 arrays and converts input
        # x=X[:, i] to c-contiguous float64.
        n_out = self.n_features_out_ + n_features * (1 - self.include_bias)
        if X.dtype in FLOAT_DTYPES:
            dtype = X.dtype
        else:
            dtype = np.float64
        if use_sparse:
            output_list = []
        else:
            XBS = np.zeros((n_samples, n_out), dtype=dtype, order=self.order)

        for feature_idx in range(n_features):
            spl = self.bsplines_[feature_idx]
            # Get indicator for nan values in the current column.
            nan_row_indices = np.flatnonzero(_get_mask(X[:, feature_idx], np.nan))

            if self.extrapolation in ("continue", "error", "periodic"):
                if self.extrapolation == "periodic":
                    # With periodic extrapolation we map x to the segment
                    # [spl.t[k], spl.t[n]].
                    # This is equivalent to BSpline(.., extrapolate="periodic")
                    # for scipy>=1.0.0.

Review comment (suggestion):
I would use `self.sparse_output` everywhere. I guess `use_sparse` was defined only to cover more easily scipy < 1.10 and scipy >= 1.10

Code after:
            The data to transform.

        Returns
        -------
        XBS : {ndarray, sparse matrix} of shape (n_samples, n_features * n_splines)
            The matrix of features, where n_splines is the number of bases
            elements of the B-splines, n_knots + degree - 1.
        """
        check_is_fitted(self)

        X = validate_data(
            self,
            X,
            reset=False,
            accept_sparse=False,
            ensure_2d=True,
            ensure_all_finite=(self.handle_missing != "zeros"),
        )

        n_samples, n_features = X.shape
        n_splines = self.bsplines_[0].c.shape[1]
        degree = self.degree

        # Note that scipy BSpline returns float64 arrays and converts input
        # x=X[:, i] to c-contiguous float64.
        n_out = self.n_features_out_ + n_features * (1 - self.include_bias)
        if X.dtype in FLOAT_DTYPES:
            dtype = X.dtype
        else:
            dtype = np.float64
        if self.sparse_output:
            output_list = []
        else:
            XBS = np.zeros((n_samples, n_out), dtype=dtype, order=self.order)

        for feature_idx in range(n_features):
            spl = self.bsplines_[feature_idx]
            # Get indicator for nan values in the current column.
            nan_row_indices = np.flatnonzero(_get_mask(X[:, feature_idx], np.nan))

            if self.extrapolation in ("continue", "error", "periodic"):
                if self.extrapolation == "periodic":
                    # With periodic extrapolation we map x to the segment
                    # [spl.t[k], spl.t[n]].
                    # This is equivalent to BSpline(.., extrapolate="periodic")
                    # for scipy>=1.0.0.
                    n = spl.t.size - spl.k - 1
                    if spl.t[n] - spl.t[spl.k] > 0:
                        # Assign to new array to avoid inplace operation
                        x = spl.t[spl.k] + (X[:, feature_idx] - spl.t[spl.k]) % (
                            spl.t[n] - spl.t[spl.k]