
    0Ph`                     .   d Z ddlZddlmZmZ ddlmZ ddlmZ ddl	Z
ddlmZ ddlmZ ddlmZ d	d
lmZmZmZ d	dlmZ d	dlmZmZ d	dlmZmZ d	dlmZ d	dlm Z m!Z!m"Z"m#Z#m$Z$ ddl%m&Z&m'Z'm%Z% ddgZ(ddZ) G d dee          Z* G d dee          Z+dS )z>
This file contains preprocessing tools based on polynomials.
    N)chaincombinations)combinations_with_replacement)Integral)sparse)BSplinecomb   )BaseEstimatorTransformerMixin_fit_context)check_array)Interval
StrOptions)parse_version
sp_version_weighted_percentile)FLOAT_DTYPES_check_feature_names_in_check_sample_weightcheck_is_fittedvalidate_data   )_calc_expanded_nnz_calc_total_nnz_csr_polynomial_expansionPolynomialFeaturesSplineTransformerc                     t          | j        ||          }t          |||          }|dk    rdS |dz
  }|}t          j        t          j                  j        }	t          ||          |	k    }
|
rt          j        nt          j        }||z  }t          t          d          k     r|dz
  |	k    r|
st          d          t          j        || j        j                  }t          j        ||          }t          j        | j        j        d         |          }t          | j        | j        | j        | j        d         |||||	  	         t#          j        |||f| j        j        d         dz
  |f| j                  S )zDHelper function for creating and appending sparse expansion matricesr   Nr   1.8.0aX  In scipy versions `<1.8.0`, the function `scipy.sparse.hstack` sometimes produces negative columns when the output shape contains `n_cols` too large to be represented by a 32bit signed integer. To avoid this error, either use a version of scipy `>=1.8.0` or alter the `PolynomialFeatures` transformer to produce fewer than 2^31 output features.shapedtype)r   indptrr   npiinfoint32maxint64r   r   
ValueErroremptydatar%   r$   r   indicesr   
csr_matrix)Xinteraction_onlydeg
n_featurescumulative_size	total_nnzexpanded_colmax_indices
max_indptr	max_int32needs_int64index_dtypeexpanded_dataexpanded_indicesexpanded_indptrs                  a/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/sklearn/preprocessing/_polynomial.py_create_expansionrA   *   s     *:C@@I%j2BCHHLqt "KJ""&Ik:..:K)7"((rxK
 |#O]7++++a)++ , G
 
 	
 H9AFLAAAMxi{CCChQX^A%6kJJJO					

 
 
 	(/:x~a 1$l3g       c                       e Zd ZU dZ eeddd          dgdgdg edd	h          gd
Zee	d<   	 ddddddZ
ed             Zed             Zed             ZddZ ed          dd            Zd Z fdZ xZS )r   a  Generate polynomial and interaction features.

    Generate a new feature matrix consisting of all polynomial combinations
    of the features with degree less than or equal to the specified degree.
    For example, if an input sample is two dimensional and of the form
    [a, b], the degree-2 polynomial features are [1, a, b, a^2, ab, b^2].

    Read more in the :ref:`User Guide <polynomial_features>`.

    Parameters
    ----------
    degree : int or tuple (min_degree, max_degree), default=2
        If a single int is given, it specifies the maximal degree of the
        polynomial features. If a tuple `(min_degree, max_degree)` is passed,
        then `min_degree` is the minimum and `max_degree` is the maximum
        polynomial degree of the generated features. Note that `min_degree=0`
        and `min_degree=1` are equivalent as outputting the degree zero term is
        determined by `include_bias`.

    interaction_only : bool, default=False
        If `True`, only interaction features are produced: features that are
        products of at most `degree` *distinct* input features, i.e. terms with
        power of 2 or higher of the same input feature are excluded:

        - included: `x[0]`, `x[1]`, `x[0] * x[1]`, etc.
        - excluded: `x[0] ** 2`, `x[0] ** 2 * x[1]`, etc.

    include_bias : bool, default=True
        If `True` (default), then include a bias column, the feature in which
        all polynomial powers are zero (i.e. a column of ones - acts as an
        intercept term in a linear model).

    order : {'C', 'F'}, default='C'
        Order of output array in the dense case. `'F'` order is faster to
        compute, but may slow down subsequent estimators.

        .. versionadded:: 0.21

    Attributes
    ----------
    powers_ : ndarray of shape (`n_output_features_`, `n_features_in_`)
        `powers_[i, j]` is the exponent of the jth input in the ith output.

    n_features_in_ : int
        Number of features seen during :term:`fit`.

        .. versionadded:: 0.24

    feature_names_in_ : ndarray of shape (`n_features_in_`,)
        Names of features seen during :term:`fit`. Defined only when `X`
        has feature names that are all strings.

        .. versionadded:: 1.0

    n_output_features_ : int
        The total number of polynomial output features. The number of output
        features is computed by iterating over all suitably sized combinations
        of input features.

    See Also
    --------
    SplineTransformer : Transformer that generates univariate B-spline bases
        for features.

    Notes
    -----
    Be aware that the number of features in the output array scales
    polynomially in the number of features of the input array, and
    exponentially in the degree. High degrees can cause overfitting.

    See :ref:`examples/linear_model/plot_polynomial_interpolation.py
    <sphx_glr_auto_examples_linear_model_plot_polynomial_interpolation.py>`

    Examples
    --------
    >>> import numpy as np
    >>> from sklearn.preprocessing import PolynomialFeatures
    >>> X = np.arange(6).reshape(3, 2)
    >>> X
    array([[0, 1],
           [2, 3],
           [4, 5]])
    >>> poly = PolynomialFeatures(2)
    >>> poly.fit_transform(X)
    array([[ 1.,  0.,  1.,  0.,  0.,  1.],
           [ 1.,  2.,  3.,  4.,  6.,  9.],
           [ 1.,  4.,  5., 16., 20., 25.]])
    >>> poly = PolynomialFeatures(interaction_only=True)
    >>> poly.fit_transform(X)
    array([[ 1.,  0.,  1.,  0.],
           [ 1.,  2.,  3.,  6.],
           [ 1.,  4.,  5., 20.]])
    r   Nleftclosed
array-likebooleanCFdegreer2   include_biasorder_parameter_constraintsr   FT)r2   rM   rN   c                >    || _         || _        || _        || _        d S NrK   )selfrL   r2   rM   rN   s        r@   __init__zPolynomialFeatures.__init__   s&      0(


rB   c                      |rt           nt          t          d|          }t          j         fdt          ||dz             D                       }|r't           t                     d          |          }|S )Nr   c              3   J   K   | ]} t                    |          V  d S rQ   )range).0ir
   r4   s     r@   	<genexpr>z3PolynomialFeatures._combinations.<locals>.<genexpr>   sH       #
 #
+,DDz""A&&#
 #
 #
 #
 #
 #
rB   r   )r   combinations_w_rr*   r   from_iterablerV   )r4   
min_degree
max_degreer2   rM   startiterr
   s   `      @r@   _combinationsz PolynomialFeatures._combinations   s      0E||5EAz""" #
 #
 #
 #
 #
05eZ!^0L0L#
 #
 #
 
 
  	;eJ//33T::DrB   c           
      (    |rIt           fdt          t          d|          t          |           dz             D                       }n>t	           |z   |d          dz
  }|dk    r |dz
  }|t	           |z   |d          dz
  z  }|r|dz  }|S )zCalculate number of terms in polynomial expansion

        This should be equivalent to counting the number of terms returned by
        _combinations(...) but much faster.
        c                 4    g | ]}t          |d           S )Texactr	   )rW   rX   r4   s     r@   
<listcomp>z8PolynomialFeatures._num_combinations.<locals>.<listcomp>   s8        Qd333  rB   r   Trc   r   )sumrV   r*   minr
   )r4   r\   r]   r2   rM   r   ds   `      r@   _num_combinationsz$PolynomialFeatures._num_combinations   s      	H   "3q*#5#5s:z7R7RUV7VWW   LL  
Z 74PPPSTTLA~~NZ!^Qd C C Ca GG 	ALrB   c                      t                                            j         j         j         j         j                  }t          j         fd|D                       S )z.Exponent for each of the inputs in the output.r4   r\   r]   r2   rM   c                 F    g | ]}t          j        |j                   S ))	minlength)r'   bincountn_features_in_)rW   crR   s     r@   re   z.PolynomialFeatures.powers_.<locals>.<listcomp>  s+    QQQqR[d&9:::QQQrB   )	r   r`   ro   _min_degree_max_degreer2   rM   r'   vstack)rR   r   s   ` r@   powers_zPolynomialFeatures.powers_   sx     	))*''!2* * 
 
 yQQQQLQQQ
 
 	
rB   c           	      b   | j         }t          |           g }|D ]x}t          j        |          d         }t	          |          r6d                    fdt          |||                   D                       }nd}|                    |           yt          j        |t                    S )a  Get output feature names for transformation.

        Parameters
        ----------
        input_features : array-like of str or None, default=None
            Input features.

            - If `input_features is None`, then `feature_names_in_` is
              used as feature names in. If `feature_names_in_` is not defined,
              then the following input feature names are generated:
              `["x0", "x1", ..., "x(n_features_in_ - 1)"]`.
            - If `input_features` is an array-like, then `input_features` must
              match `feature_names_in_` if `feature_names_in_` is defined.

        Returns
        -------
        feature_names_out : ndarray of str objects
            Transformed feature names.
        r    c              3   T   K   | ]"\  }}|d k    rd|         |fz  n|         V  #dS )r   z%s^%dN )rW   indexpinput_featuress      r@   rY   z;PolynomialFeatures.get_feature_names_out.<locals>.<genexpr>(  sb           !S !88  >##6"<<<+C0	           rB   1r%   )
rt   r   r'   wherelenjoinzipappendasarrayobject)rR   r{   powersfeature_namesrowindsnames    `     r@   get_feature_names_outz(PolynomialFeatures.get_feature_names_out  s    ( 0~FF 	' 	'C8C==#D4yy 
xx         %(c$i$8$8          &&&&z-v6666rB   prefer_skip_nested_validationc                     t          | |d          j        \  }}t          | j        t                    r5| j        dk    r| j        st          d          d| _        | j        | _        nt          | j        t          j
        j                  rt          | j                  dk    r| j        \  | _        | _        t          | j        t                    r5t          | j        t                    r| j        dk    r| j        | j        k    st          d| j         d          | j        dk    r| j        st          d          nt          d	| j         d          |                     || j        | j        | j        | j        
          | _        | j        t!          j        t           j                  j        k    rd| j         dt!          j                    j        j         d|d| j         d| j         d| j         d| j         d}t           j        t           j        k    r1| j        t!          j        t           j                  j        k    r|dz  }t          |          |                     |d| j        | j        | j        
          | _        | S )al  
        Compute number of output features.

        Parameters
        ----------
        X : {array-like, sparse matrix} of shape (n_samples, n_features)
            The data.

        y : Ignored
            Not used, present here for API consistency by convention.

        Returns
        -------
        self : object
            Fitted transformer.
        T)accept_sparser   zWSetting degree to zero and include_bias to False would result in an empty output array.r   zhdegree=(min_degree, max_degree) must be non-negative integers that fulfil min_degree <= max_degree, got .zoSetting both min_degree and max_degree to zero and include_bias to False would result in an empty output array.zIdegree must be a non-negative int or tuple (min_degree, max_degree), got rk   zGThe output that would result from the current configuration would have z. features which is too large to be indexed by zj. Please change some or all of the following:
- The number of features in the input, currently n_features=z1
- The range of degrees to calculate, currently [z, z9]
- Whether to include only interaction terms, currently z-
- Whether to include a bias term, currently z
Note that the current Python runtime has a limited 32 bit address space and that this configuration would have been admissible if run on a 64 bit Python runtime.)r   r$   
isinstancerL   r   rM   r,   rq   rr   collectionsabcIterabler   ri   r2   n_output_features_r'   r(   intpr*   r%   r   r)   r+   _n_out_full)rR   r1   y_r4   msgs         r@   fitzPolynomialFeatures.fit5  s   $ &dATBBBH:dk8,, #	{a(9 .  
  !D#{Dt{KO$<==	BEdkBRBRVWBWBW15.Dd.4+X66t/:: $))$(888 & {& & &   !Q&&t/@& D  
 ";" " "   #'"8"8!''!2* #9 #
 #
 "RXbg%6%6%:::H0H H!wyy3H H 	H H
 %H H
 *.)9H H 261FH H 483DH H H  28##+rx/A/A/EEED
 S//!  11!'!2* 2 
 
 rB   c           
      j   t          |            t          | |dt          dd          }|j        \  }}t	          j        t          j                  j        }t          j	        |          r|j
        dk    r| j        dk    r9|                     |                                                                          S g }| j        rB|                    t          j        t	          j        |df|j                                       | j        dk    r | j        d	k    r|                    |           t+          d
 |D                       }t-          t          d| j                  | j        dz             D ]B}t/          || j        |||          }|%|                    |           ||j        d         z  }Ct3          |          d	k    rt          j        |d	f|j                  }	nt5          d |D                       }
t6          t9          d          k     r| j        |k    r|
rt=          d          t          j        ||j        d          }	n]t          j	        |          rO|j
        dk    rD| j        dk     r9|                     |                                                                          S t          j	        |          r|                      || j        | j        | j        | j                  }g }|D ]}|r=d}|D ]"}|dd|gf         !                    |          }#|                    |           At          j"        t	          j        |j        d	         df                    }|                    |           t          j        ||j                                                  }	nt	          j#        || j$        f|j        | j%                  }	| j        rd|	ddd	f<   d}nd	}| j        d	k    r|	S ||	dd|||z   f<   tM          t-          |||z                       }||z  }|                    |           t-          d| j        dz             D ]}g }|d         }t-          |          D ]}||         }|                    |           | j        r|||dz            ||         z
  z  }||z   |z
  }||k    r nBt	          j!        |	dd||f         |dd||dz   f         |	dd||f         d           |}|                    |           |}| j        dk    r| j$        | j        }}| j        rJt	          j#        ||f|	j        | j%                  }d|ddd	f<   |	dd||z
  dz   df         |ddddf<   n#|	dd||z
  df         '                                }|}	|	S )a<  Transform data to polynomial features.

        Parameters
        ----------
        X : {array-like, sparse matrix} of shape (n_samples, n_features)
            The data to transform, row by row.

            Prefer CSR over CSC for sparse input (for speed), but CSC is
            required if the degree is 4 or higher. If the degree is less than
            4 and the input format is CSC, it will be converted to CSR, have
            its polynomial features generated, then converted back to CSC.

            If the degree is 2 or 3, the method described in "Leveraging
            Sparsity to Speed Up Polynomial Feature Expansions of CSR Matrices
            Using K-Simplex Numbers" by Andrew Nystrom and John Hughes is
            used, which is much faster than the method used on CSC input. For
            this reason, a CSC input will be converted to CSR, and the output
            will be converted back to CSC prior to being returned, hence the
            preference of CSR.

        Returns
        -------
        XP : {ndarray, sparse matrix} of shape (n_samples, NP)
            The matrix of features, where `NP` is the number of polynomial
            features generated from the combination of inputs. If a sparse
            matrix is provided, it will be converted into a sparse
            `csr_matrix`.
        rJ   F)csrcsc)rN   r%   resetr   r      r   r#   r   c              3   0   K   | ]}|j         d          V  dS )r   N)r$   rW   mats     r@   rY   z/PolynomialFeatures.transform.<locals>.<genexpr>  s(      !C!C3#)A,!C!C!C!C!C!CrB   r   )r1   r2   r3   r4   r5   Nr}   c              3   J   K   | ]}|j         j        t          j        k    V  d S rQ   )r/   r%   r'   r)   r   s     r@   rY   z/PolynomialFeatures.transform.<locals>.<genexpr>  s.      RR# 1RX =RRRRRRrB   1.9.2a  In scipy versions `<1.9.2`, the function `scipy.sparse.hstack` produces negative columns when:
1. The output shape contains `n_cols` too large to be represented by a 32bit signed integer.
2. All sub-matrices to be stacked have indices of dtype `np.int32`.
To avoid this error, either use a version of scipy `>=1.9.2` or alter the `PolynomialFeatures` transformer to produce fewer than 2^31 output features)r%   formatr      rk   )r$   r%   rN   no)outcasting)(r   r   r   r$   r'   r(   r)   r*   r   issparser   rr   	transformtocsctocsrrM   r   r0   onesr%   rq   rf   rV   rA   r2   r   allr   r   r   r,   hstackr`   multiply
csc_matrixr-   r   rN   listcopy)rR   r1   	n_samplesr4   r:   to_stackr5   r3   expandedXP	all_int32r   columnscombiout_colcol_idxbiascurrent_colindexr   	new_indexendfeature_idxr^   next_coln_XPn_XoutXouts                               r@   r   zPolynomialFeatures.transform  s   : 	(
 
 
 !"	:HRX&&*	?1 I	!(e"3"3!##~~aggii0066888H  %bgYN!'&R&R&RSS   1$$)9A)=)="""!!C!C(!C!C!CCCOSD$455t7G!7KLL 
9 
9,%)%:)$3   'OOH---#x~a'88O8}}!!&	1~QWEEE  RRRRRRR	w!7!777/);;! < %R   ]8175III_Q \	AH$5$5$:JQ:N:N>>!'')),,22444_Q Z	--%++!%!6!. .  L G% ) ) )G#( D D"#AAAyL/":":7"C"CNN7++++!,RWagaj!_-E-EFFDNN4((((wag666<<>>BB  $"2317$*  B$    111a41$$	 =>Bqqq+j 8889{K*,DEEFFE:%KLL%%% 1d.233 " "	Bi#(#4#4 + +K!+.E$$[111, M{Q!7%:L!LL*S058H;.. K111eCi<(!!![;?::;qqq+h"667 $	    #+KK  ---!!###/1Hf$ 98(&1  D "#DAJ"$QQQv(9(;(;%;"<DABBKKaaa016688D	rB   c                 `    t                                                      }d|j        _        |S )NT)super__sklearn_tags__
input_tagsr   )rR   tags	__class__s     r@   r   z#PolynomialFeatures.__sklearn_tags__L  s'    ww''))!%rB   )r   rQ   )__name__
__module____qualname____doc__r   r   r   rO   dict__annotations__rS   staticmethodr`   ri   propertyrt   r   r   r   r   r   __classcell__)r   s   @r@   r   r   g   sv        \ \~ 8Haf===|L&K"*c3Z(()	$ $D    ,1C     
 
 \
   \6 
 
 X
%7 %7 %7 %7N \555^ ^ ^ 65^@t t tl        rB   c            	       $   e Zd ZU dZ eeddd          g eeddd          g eddh          d	g eh d
          gdg eddh          gdgdZee	d<   	 	 ddddddddZ
edd            ZddZ ed          d d            Zd ZdS )!r    a  Generate univariate B-spline bases for features.

    Generate a new feature matrix consisting of
    `n_splines=n_knots + degree - 1` (`n_knots - 1` for
    `extrapolation="periodic"`) spline basis functions
    (B-splines) of polynomial order=`degree` for each feature.

    In order to learn more about the SplineTransformer class go to:
    :ref:`sphx_glr_auto_examples_applications_plot_cyclical_feature_engineering.py`

    Read more in the :ref:`User Guide <spline_transformer>`.

    .. versionadded:: 1.0

    Parameters
    ----------
    n_knots : int, default=5
        Number of knots of the splines if `knots` equals one of
        {'uniform', 'quantile'}. Must be larger or equal 2. Ignored if `knots`
        is array-like.

    degree : int, default=3
        The polynomial degree of the spline basis. Must be a non-negative
        integer.

    knots : {'uniform', 'quantile'} or array-like of shape         (n_knots, n_features), default='uniform'
        Set knot positions such that first knot <= features <= last knot.

        - If 'uniform', `n_knots` number of knots are distributed uniformly
          from min to max values of the features.
        - If 'quantile', they are distributed uniformly along the quantiles of
          the features.
        - If an array-like is given, it directly specifies the sorted knot
          positions including the boundary knots. Note that, internally,
          `degree` number of knots are added before the first knot, the same
          after the last knot.

    extrapolation : {'error', 'constant', 'linear', 'continue', 'periodic'},         default='constant'
        If 'error', values outside the min and max values of the training
        features raises a `ValueError`. If 'constant', the value of the
        splines at minimum and maximum value of the features is used as
        constant extrapolation. If 'linear', a linear extrapolation is used.
        If 'continue', the splines are extrapolated as is, i.e. option
        `extrapolate=True` in :class:`scipy.interpolate.BSpline`. If
        'periodic', periodic splines with a periodicity equal to the distance
        between the first and last knot are used. Periodic splines enforce
        equal function values and derivatives at the first and last knot.
        For example, this makes it possible to avoid introducing an arbitrary
        jump between Dec 31st and Jan 1st in spline features derived from a
        naturally periodic "day-of-year" input feature. In this case it is
        recommended to manually set the knot values to control the period.

    include_bias : bool, default=True
        If False, then the last spline element inside the data range
        of a feature is dropped. As B-splines sum to one over the spline basis
        functions for each data point, they implicitly include a bias term,
        i.e. a column of ones. It acts as an intercept term in a linear models.

    order : {'C', 'F'}, default='C'
        Order of output array in the dense case. `'F'` order is faster to compute, but
        may slow down subsequent estimators.

    sparse_output : bool, default=False
        Will return sparse CSR matrix if set True else will return an array. This
        option is only available with `scipy>=1.8`.

        .. versionadded:: 1.2

    Attributes
    ----------
    bsplines_ : list of shape (n_features,)
        List of BSplines objects, one for each feature.

    n_features_in_ : int
        The total number of input features.

    feature_names_in_ : ndarray of shape (`n_features_in_`,)
        Names of features seen during :term:`fit`. Defined only when `X`
        has feature names that are all strings.

        .. versionadded:: 1.0

    n_features_out_ : int
        The total number of output features, which is computed as
        `n_features * n_splines`, where `n_splines` is
        the number of bases elements of the B-splines,
        `n_knots + degree - 1` for non-periodic splines and
        `n_knots - 1` for periodic ones.
        If `include_bias=False`, then it is only
        `n_features * (n_splines - 1)`.

    See Also
    --------
    KBinsDiscretizer : Transformer that bins continuous data into intervals.

    PolynomialFeatures : Transformer that generates polynomial and interaction
        features.

    Notes
    -----
    High degrees and a high number of knots can cause overfitting.

    See :ref:`examples/linear_model/plot_polynomial_interpolation.py
    <sphx_glr_auto_examples_linear_model_plot_polynomial_interpolation.py>`.

    Examples
    --------
    >>> import numpy as np
    >>> from sklearn.preprocessing import SplineTransformer
    >>> X = np.arange(6).reshape(6, 1)
    >>> spline = SplineTransformer(degree=2, n_knots=3)
    >>> spline.fit_transform(X)
    array([[0.5 , 0.5 , 0.  , 0.  ],
           [0.18, 0.74, 0.08, 0.  ],
           [0.02, 0.66, 0.32, 0.  ],
           [0.  , 0.32, 0.66, 0.02],
           [0.  , 0.08, 0.74, 0.18],
           [0.  , 0.  , 0.5 , 0.5 ]])
    r   NrD   rE   r   uniformquantilerG   >   errorlinearconstantcontinueperiodicrH   rI   rJ   n_knotsrL   knotsextrapolationrM   rN   sparse_outputrO      r   r   TF)r   r   rM   rN   r   c                h    || _         || _        || _        || _        || _        || _        || _        d S rQ   r   )rR   r   rL   r   r   rM   rN   r   s           r@   rS   zSplineTransformer.__init__  s>     
*(
*rB   
   c                     |dk    radt          j        dd|t           j                  z  }t          j         |d          }nt          j         fd|D                       }ntt          ddd          ndk    }t          j         |         d          }t          j         |         d          }t          j        |||d	t           j        
          }|S )a  Calculate base knot positions.

        Base knots such that first knot <= feature <= last knot. For the
        B-spline construction with scipy.interpolate.BSpline, 2*degree knots
        beyond the base interval are added.

        Returns
        -------
        knots : ndarray of shape (n_knots, n_features), dtype=np.float64
            Knot positions (points) of base interval.
        r   d   r   r   )r^   stopnumr%   Naxisc                 2    g | ]}t          |          S rx   r   )rW   
percentiler1   sample_weights     r@   re   z>SplineTransformer._get_base_knot_positions.<locals>.<listcomp>  s5       & -QzJJ  rB   T)r^   r   r   endpointr%   )r'   linspacefloat64r   arraysliceaminamax)r1   r   r   r   percentilesmaskx_minx_maxs   `  `    r@   _get_base_knot_positionsz*SplineTransformer._get_base_knot_positions  s    JaWBJ! ! ! K $a1===    *5    ,9+@5tQ'''mVWFWDGAdG!,,,EGAdG!,,,EKj  E rB   c                 ^   t          | d           | j        d         j        j        d         }t	          | |          }g }t          | j                  D ]?}t          |dz
  | j        z             D ]"}|                    ||          d|            #@t          j
        |t                    S )a  Get output feature names for transformation.

        Parameters
        ----------
        input_features : array-like of str or None, default=None
            Input features.

            - If `input_features` is `None`, then `feature_names_in_` is
              used as feature names in. If `feature_names_in_` is not defined,
              then the following input feature names are generated:
              `["x0", "x1", ..., "x(n_features_in_ - 1)"]`.
            - If `input_features` is an array-like, then `input_features` must
              match `feature_names_in_` if `feature_names_in_` is defined.

        Returns
        -------
        feature_names_out : ndarray of str objects
            Transformed feature names.
        ro   r   r   _sp_r}   )r   	bsplines_rp   r$   r   rV   ro   rM   r   r'   r   r   )rR   r{   	n_splinesr   rX   js         r@   r   z'SplineTransformer.get_feature_names_out  s    ( 	.///N1%'-a0	0~FFt*++ 	D 	DA9q=4+<<== D D$$q(9%B%Bq%B%BCCCCDz-v6666rB   r   c           	      B    t           |dddd          }|t          |||j                  }|j        \  }}t	           j        t                    r$                     | j         j        |          }nt           j        t          j                  }|j        d         dk     rt          d	          |j        d
         |k    rt          d          t          j        t          j        |d          dk              st          d           j        r0t           t#          d          k     rt          dt            d          |j        d         } j        dk    r&| j        k    rt          d| d j         d           j        dk    r| j        z   d
z
  }n|d
z
  } j        }	||z  }
 j        dk    rD|d         |d         z
  }t          j        ||	d
z    d         |z
  ||d
|	d
z            |z   f         n|d
         |d         z
  }|d         |d         z
  }t          j        t          j        |d         |	|z  z
  |d         |z
  |	          |t          j        |d         |z   |d         |	|z  z   |	          f         t          j        |t          j                   j        dk    r"t          j        d|	ddf         f           j        dv  fdt1          |          D             }| _        |
|d
 j        z
  z  z
   _         S )aE  Compute knot positions of splines.

        Parameters
        ----------
        X : array-like of shape (n_samples, n_features)
            The data.

        y : None
            Ignored.

        sample_weight : array-like of shape (n_samples,), default = None
            Individual weights for each sample. Used to calculate quantiles if
            `knots="quantile"`. For `knots="uniform"`, zero weighted
            observations are ignored for finding the min and max of `X`.

        Returns
        -------
        self : object
            Fitted transformer.
        TFr   )r   r   ensure_min_samples	ensure_2dNr}   )r   r   r   r   z.Number of knots, knots.shape[0], must be >= 2.r   z)knots.shape[1] == n_features is violated.r   z(knots must be sorted without duplicates.r"   zOOption sparse_output=True is only available with scipy>=1.8.0, but here scipy==z	 is used.r   z7Periodic splines require degree < n_knots. Got n_knots=z and degree=r   r   )r   )r   r   c                 ^    g | ])}t          j        d d |f         j                  *S )N)extrapolate)r   construct_fastrL   )rW   rX   coefr  r   rR   s     r@   re   z)SplineTransformer.fit.<locals>.<listcomp>  sT     
 
 
  "aaadT4;K  
 
 
rB   )r   r   r%   r$   r   r   strr   r   r   r'   r   r,   r   diffr   r   r   r   rL   r_r   eyeconcatenaterV   r   rM   n_features_out_)rR   r1   r   r   r   r4   
base_knotsr   r   rL   n_outperioddist_mindist_maxbsplinesr	  r  r   s   `              @@@r@   r   zSplineTransformer.fit9  s   ,  
 
 
 $0QQQM:dj#&& 	M664<tz 7  JJ %TZrzBBBJ"Q&& !QRRR!!$
22 !LMMMVBGJQ777!;<< M !KLLL 	*}W/E/E"E"E9#-9 9 9   "1%++4;0F0F77 7(,7 7 7   ++$+-1II  !IY& ++  ^jm3FEVaZ=2-.71
+,v57EE& "!}z!}4H!"~
26HEqMFX$55qMH,  
 rNX-rNVh%66  
E" virz222++>4gvgqqqj)9":;;D(,DD
 
 
 
 
 
 
 :&&	
 
 
 "$zQ9J5J'KKrB   c           
           t          |            t          | |ddd          }|j        \  }}| j        d         j        j        d          | j        }t          t          d          k    }|r| j        }d| j        d         j	        i}n(| j        o| j        d         j	         }t                      }| j        |d| j        z
  z  z   }|j        t          v r|j        }	nt          j        }	|rg }
nt          j        ||f|	| j                  }t'          |          D ]}| j        |         }| j        d	v r| j        d
k    rk|j        j        |j        z
  dz
  }|j        |j                 |dd|f         |j        |j                 z
  |j        |         |j        |j                 z
  z  z   }n|dd|f         }|rqt1          j        ||j        |j        fi |}| j        d
k    rF|                                }|ddd|fxx         |dd| df         z  cc<   |ddd| f         }n! ||          |dd| z  |dz    z  f<   n|j        |         |j        | dz
           }} ||           ||          }}||dd|f         k    |dd|f         |k    z  }|r| }|dd|f                                         }|j        | j                 ||<   t1          j        ||j        |j                  }t          j        |          r|                                }d||ddf<   n# ||||f                   ||| z  |dz    z  f<   | j        dk    r{|r+t          j        t          j        |j                            s=|sJt          j        t          j        |dd| z  |dz    z  f                             rt?          d          n| j        dk    r|dd|f         |k     }t          j        |          rB|r&|                                }|d|         ||d|f<   n|d|         ||| z  | z  |z   f<   |dd|f         |k    }t          j        |          rL|r)|                                }|| d         ||| df<   nY|| d         |||dz    z  |z
  |dz    z  f<   n6| j        dk    r* ||d           ||d          }}|dk    r|dz  }t'          |          D ]}|dd|f         |k     }t          j        |          rJ||         |||f         |z
  ||         z  z   }|r|                                }||||f<   n|||| z  |z   f<   |dd|f         |k    }t          j        |          ra dz
  |z
  }||         |||f         |z
  ||         z  z   }|r+|                                }|dddf         ||||dz   f<   |||| z  |z   f<   |r)|                                 }|
!                    |           |rt          j"        t          j#                  j$        }d}|
D ]}||j%        j        t          j#        k    z  } t          t          d          k     r| j        |k    r|rt?          d          tM          j'        |
d          }n| j        rtM          j(        |          }| j        r|S  fdt'          |j        d                   D             }|dd|f         S )a  Transform each feature data to B-splines.

        Parameters
        ----------
        X : array-like of shape (n_samples, n_features)
            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.
        FT)r   r   r  r   r   z1.10.0r  )r%   rN   )r   r   r   r   Nr   z1X contains values beyond the limits of the knots.r   r   )nur   a  In scipy versions `<1.9.2`, the function `scipy.sparse.hstack` produces negative columns when:
1. The output shape contains `n_cols` too large to be represented by a 32bit signed integer.
. All sub-matrices to be stacked have indices of dtype `np.int32`.
To avoid this error, either use a version of scipy `>=1.9.2` or alter the `SplineTransformer` transformer to produce fewer than 2^31 output featuresr   )r   c                 ,    g | ]}|d z   z  dk    |S )r   r   rx   )rW   r  r   s     r@   re   z/SplineTransformer.transform.<locals>.<listcomp>  s-    RRRQ!a%99LPQ9Q9Qq9Q9Q9QrB   ))r   r   r$   r   rp   rL   r   r   r   r  r   r  rM   r%   r   r'   r   zerosrN   rV   r   tsizekr   design_matrixtolilr   anyisnanr.   r,   r   r   r(   r)   r*   r/   r   r   r0   )!rR   r1   r   r4   rL   
scipy_1_10
use_sparsekwargs_extrapolater  r%   output_listXBSrX   splnx
XBS_sparsexminxmaxf_minf_maxr   mask_invfp_minfp_maxr  linear_extrr  r:   r   r   r/   r   s!                                   @r@   r   zSplineTransformer.transform  s{    	$etTTT !	:N1%'-a0	
  =#:#::
  	(+J"/1B1N!O+QDN14E4Q0QJ!% $zQ9J5J'KK7l""GEEJE 	NKK(Iu-U$*MMMCz"" G	/ G	/A.#C!%FFF%33
 
SU*Q.Ace!!!Q$#%,(>a35</( AA !!!Q$A M!(!635#%" "+=" "J )Z77
 &0%5%5%7%7
"111gvg:...*QQQ[2II...%/8VG8%<
FIc!ffCA	Mq1u	.ABBCC U6]CE6'A+,>d"s4yy##d))u!!!Q$AaaadGtO< Y $uH!!!Q$A #&%"4AhK!(!6q#%!G!GJ
 vh'' 4%/%5%5%7%7
23
8QQQ;/ILQtUVwZCq9}!a%91DEEF !W,,  26"(:?*C*C#D#D "QQQYAEY;N(O%O!PQQ  %K   #z11 Aw~6$<< 	! %/%5%5%7%7
49'6'N
4&=11 QV#VGQD1y=Q]V5K"LLM Aw~6$<< 	,! ,%/%5%5%7%7
5:F788_
4&>22
 "6'((O  !ey069q1u	>QRT
 #x// "%Ta##dq///Q;; aKFv G GAQQQT7T>Dvd|| G&+Ah!D!G*t2Cvay1P&P% G)3)9)9););J2=JtQw//;FCa)ma&7 78QQQT7T>Dvd|| G%MA-&+Ah!D!G*t2Cvay1P&P% G)3)9)9););J:Eaaag:NJtQQY77;FCa)ma&7 78 /'--//
"":... 	)
 **.II" ; ;S[."(::		]73333(944 5 !N   -E:::CC 	)#C((C 	#J SRRR%	!"5"5RRRGqqq'z?"rB   )r   r   )r   r   NrQ   )NN)r   r   r   r   r   r   r   rO   r   r   rS   r   r   r   r   r   r   rx   rB   r@   r    r    R  sx        x xv HXq$v>>>?8Haf===>*i455|DJNNNOO
 #*c3Z(()#
$ 
$D 
 
 
 +
  + + + + +& , , , \,\7 7 7 7< \555D D D 65DLZ# Z# Z# Z# Z#rB   )r   ),r   r   	itertoolsr   r   r   rZ   numbersr   numpyr'   scipyr   scipy.interpolater   scipy.specialr
   baser   r   r   utilsr   utils._param_validationr   r   utils.fixesr   r   utils.statsr   utils.validationr   r   r   r   r   r   r   r   __all__rA   r   r    rx   rB   r@   <module>r?     s         ) ) ) ) ) ) ) ) G G G G G G                 % % % % % %       @ @ @ @ @ @ @ @ @ @       : : : : : : : : 3 3 3 3 3 3 3 3 . . . . . .                       : : : :zh h h h h)= h h hVH	# H	# H	# H	# H	#(- H	# H	# H	# H	# H	#rB   