
    0Ph
                     X   d Z ddlZddlmZmZ ddlZddl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 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 ddlm Z m!Z!m"Z"  G d deee          Z# G d deee          Z$ G d deee          Z% G d dee          Z& G d deee          Z'dS )zOApproximate kernel feature maps based on Fourier transforms and count sketches.    N)IntegralReal)fftifft)svd   )BaseEstimatorClassNamePrefixFeaturesOutMixinTransformerMixin_fit_context)KERNEL_PARAMSPAIRWISE_KERNEL_FUNCTIONSpairwise_kernels)check_random_state)Interval
StrOptions)safe_sparse_dot)_check_feature_names_incheck_is_fittedvalidate_datac            	            e Zd ZU dZ eeddd          g eeddd          g eeddd          g eeddd          gdgd	Zee	d
<   dddddd	dZ
 ed          dd            Zd Z fdZ xZS )PolynomialCountSketcha  Polynomial kernel approximation via Tensor Sketch.

    Implements Tensor Sketch, which approximates the feature map
    of the polynomial kernel::

        K(X, Y) = (gamma * <X, Y> + coef0)^degree

    by efficiently computing a Count Sketch of the outer product of a
    vector with itself using Fast Fourier Transforms (FFT). Read more in the
    :ref:`User Guide <polynomial_kernel_approx>`.

    .. versionadded:: 0.24

    Parameters
    ----------
    gamma : float, default=1.0
        Parameter of the polynomial kernel whose feature map
        will be approximated.

    degree : int, default=2
        Degree of the polynomial kernel whose feature map
        will be approximated.

    coef0 : int, default=0
        Constant term of the polynomial kernel whose feature map
        will be approximated.

    n_components : int, default=100
        Dimensionality of the output feature space. Usually, `n_components`
        should be greater than the number of features in input samples in
        order to achieve good performance. The optimal score / run time
        balance is typically achieved around `n_components` = 10 * `n_features`,
        but this depends on the specific dataset being used.

    random_state : int, RandomState instance, default=None
        Determines random number generation for indexHash and bitHash
        initialization. Pass an int for reproducible results across multiple
        function calls. See :term:`Glossary <random_state>`.

    Attributes
    ----------
    indexHash_ : ndarray of shape (degree, n_features), dtype=int64
        Array of indexes in range [0, n_components) used to represent
        the 2-wise independent hash functions for Count Sketch computation.

    bitHash_ : ndarray of shape (degree, n_features), dtype=float32
        Array with random entries in {+1, -1}, used to represent
        the 2-wise independent hash functions for Count Sketch computation.

    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

    See Also
    --------
    AdditiveChi2Sampler : Approximate feature map for additive chi2 kernel.
    Nystroem : Approximate a kernel map using a subset of the training data.
    RBFSampler : Approximate a RBF kernel feature map using random Fourier
        features.
    SkewedChi2Sampler : Approximate feature map for "skewed chi-squared" kernel.
    sklearn.metrics.pairwise.kernel_metrics : List of built-in kernels.

    Examples
    --------
    >>> from sklearn.kernel_approximation import PolynomialCountSketch
    >>> from sklearn.linear_model import SGDClassifier
    >>> X = [[0, 0], [1, 1], [1, 0], [0, 1]]
    >>> y = [0, 0, 1, 1]
    >>> ps = PolynomialCountSketch(degree=3, random_state=1)
    >>> X_features = ps.fit_transform(X)
    >>> clf = SGDClassifier(max_iter=10, tol=1e-3)
    >>> clf.fit(X_features, y)
    SGDClassifier(max_iter=10)
    >>> clf.score(X_features, y)
    1.0

    For a more detailed example of usage, see
    :ref:`sphx_glr_auto_examples_kernel_approximation_plot_scalable_poly_kernels.py`
    r   Nleftclosedr   neitherrandom_stategammadegreecoef0n_componentsr   _parameter_constraints      ?   d   c                L    || _         || _        || _        || _        || _        d S Nr   )selfr   r    r!   r"   r   s         \/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/sklearn/kernel_approximation.py__init__zPolynomialCountSketch.__init__   s/     

((    Tprefer_skip_nested_validationc                 @   t          | |d          }t          | j                  }|j        d         }| j        dk    r|dz  }|                    d| j        | j        |f          | _        |	                    ddg| j        |f          | _
        | j        | _        | S )a  Fit the model with X.

        Initializes the internal variables. The method needs no information
        about the distribution of data, so we only care about n_features in X.

        Parameters
        ----------
        X : {array-like, sparse matrix} of shape (n_samples, n_features)
            Training data, where `n_samples` is the number of samples
            and `n_features` is the number of features.

        y : array-like of shape (n_samples,) or (n_samples, n_outputs),                 default=None
            Target values (None for unsupervised transformations).

        Returns
        -------
        self : object
            Returns the instance itself.
        cscaccept_sparser   r   )highsize)ar4   )r   r   r   shaper!   randintr"   r    
indexHash_choicebitHash__n_features_out)r)   Xyr   
n_featuress        r*   fitzPolynomialCountSketch.fit   s    , $777)$*;<<WQZ
:??!OJ&..D%T[*,E / 
 
 %++r1gT[*<U+VV#0r,   c           	         t          |            t          | |dd          }t          j        | j                  |z  }t          j        |          r]| j        dk    rRt          j        |t          j        | j                  t          j	        |j
        d         df          z  gd          }nnt          j        |          sZ| j        dk    rOt          j        |t          j        | j                  t          j	        |j
        d         df          z  g          }|j
        d         | j        j
        d         k    rt          d          t          j        |j
        d         | j        | j        f          }t          j        |          rt!          |j
        d                   D ]~}t!          | j                  D ]g}| j        ||f         }| j        ||f         }|dd||fxx         ||dd|gf         z                                                                  z  cc<   hntt!          |j
        d                   D ]Y}t!          | j                  D ]B}| j        ||f         }| j        ||f         }|dd||fxx         ||dd|f         z  z  cc<   CZt)          |d	d
          }t          j        |d          }	t          j        t/          |	d
                    }
|
S )a  Generate the feature map approximation for X.

        Parameters
        ----------
        X : {array-like}, shape (n_samples, n_features)
            New data, where `n_samples` is the number of samples
            and `n_features` is the number of features.

        Returns
        -------
        X_new : array-like, shape (n_samples, n_components)
            Returns the instance itself.
        r0   Fr2   resetr   r   )formatzKNumber of features of test samples does not match that of training samples.Nr%   T)axisoverwrite_x)rE   )rF   )r   r   npsqrtr   spissparser!   hstackonesr7   r9   
ValueErrorzerosr    r"   ranger;   toarrayravelr   prodrealr   )r)   r=   X_gammacount_sketchesjd
iHashIndexiHashBitcount_sketches_fftcount_sketches_fft_proddata_sketchs              r*   	transformzPolynomialCountSketch.transform   s    	$eDDD'$*%%);w 		DJ!OOi"'$*--q9I18M0N0NNO  GG
 W%% 	$*//i"'$*--q9I18M0N0NNO G =t4Q7773  
 7=#3T[$BS"TUU;w 	Q7=+,,  t{++  A!%A!6J#}QT2H"111a#3444!GAAAsFO3<<>>DDFF4444 7=+,, Q Qt{++ Q QA!%A!6J#}QT2H"111a#34447111a4=8PP4444Q !aTJJJ"$'*<1"E"E"Egd#:MMMNNr,   c                 `    t                                                      }d|j        _        |S )NT)super__sklearn_tags__
input_tagssparser)   tags	__class__s     r*   r`   z&PolynomialCountSketch.__sklearn_tags__   s'    ww''))!%r,   r(   __name__
__module____qualname____doc__r   r   r   r#   dict__annotations__r+   r   r@   r]   r`   __classcell__re   s   @r*   r   r      s6        U Up (4D88898Haf===>(4tI>>>?!(AtFCCCD'($ $D    1ACd) ) ) ) ) \555" " " 65"H= = =~        r,   r   c                        e Zd ZU dZ edh           eeddd          g eeddd          gdgd	Ze	e
d
<   dddd	dZ ed          dd            Zd Z fdZ xZS )
RBFSamplera  Approximate a RBF kernel feature map using random Fourier features.

    It implements a variant of Random Kitchen Sinks.[1]

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

    Parameters
    ----------
    gamma : 'scale' or float, default=1.0
        Parameter of RBF kernel: exp(-gamma * x^2).
        If ``gamma='scale'`` is passed then it uses
        1 / (n_features * X.var()) as value of gamma.

        .. versionadded:: 1.2
           The option `"scale"` was added in 1.2.

    n_components : int, default=100
        Number of Monte Carlo samples per original feature.
        Equals the dimensionality of the computed feature space.

    random_state : int, RandomState instance or None, default=None
        Pseudo-random number generator to control the generation of the random
        weights and random offset when fitting the training data.
        Pass an int for reproducible output across multiple function calls.
        See :term:`Glossary <random_state>`.

    Attributes
    ----------
    random_offset_ : ndarray of shape (n_components,), dtype={np.float64, np.float32}
        Random offset used to compute the projection in the `n_components`
        dimensions of the feature space.

    random_weights_ : ndarray of shape (n_features, n_components),        dtype={np.float64, np.float32}
        Random projection directions drawn from the Fourier transform
        of the RBF kernel.

    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

    See Also
    --------
    AdditiveChi2Sampler : Approximate feature map for additive chi2 kernel.
    Nystroem : Approximate a kernel map using a subset of the training data.
    PolynomialCountSketch : Polynomial kernel approximation via Tensor Sketch.
    SkewedChi2Sampler : Approximate feature map for
        "skewed chi-squared" kernel.
    sklearn.metrics.pairwise.kernel_metrics : List of built-in kernels.

    Notes
    -----
    See "Random Features for Large-Scale Kernel Machines" by A. Rahimi and
    Benjamin Recht.

    [1] "Weighted Sums of Random Kitchen Sinks: Replacing
    minimization with randomization in learning" by A. Rahimi and
    Benjamin Recht.
    (https://people.eecs.berkeley.edu/~brecht/papers/08.rah.rec.nips.pdf)

    Examples
    --------
    >>> from sklearn.kernel_approximation import RBFSampler
    >>> from sklearn.linear_model import SGDClassifier
    >>> X = [[0, 0], [1, 1], [1, 0], [0, 1]]
    >>> y = [0, 0, 1, 1]
    >>> rbf_feature = RBFSampler(gamma=1, random_state=1)
    >>> X_features = rbf_feature.fit_transform(X)
    >>> clf = SGDClassifier(max_iter=5, tol=1e-3)
    >>> clf.fit(X_features, y)
    SGDClassifier(max_iter=5)
    >>> clf.score(X_features, y)
    1.0
    scale        Nr   r   r   r   r   r"   r   r#   r$   r&   c                0    || _         || _        || _        d S r(   rs   )r)   r   r"   r   s       r*   r+   zRBFSampler.__init__P  s    
((r,   Tr-   c                 0   t          | |d          }t          | j                  }|j        d         }t	          j        |          }| j        dk    rk|r?|                    |                                          |                                dz  z
  n|	                                }|dk    rd||z  z  nd| _
        n| j        | _
        d| j
        z  d	z  |                    || j        f
          z  | _        |                    ddt          j        z  | j        
          | _        |j        t          j        k    rL| j                            |j        d          | _        | j                            |j        d          | _        | j        | _        | S )aM  Fit the model with X.

        Samples random projection according to n_features.

        Parameters
        ----------
        X : {array-like, sparse matrix}, shape (n_samples, n_features)
            Training data, where `n_samples` is the number of samples
            and `n_features` is the number of features.

        y : array-like, shape (n_samples,) or (n_samples, n_outputs),                 default=None
            Target values (None for unsupervised transformations).

        Returns
        -------
        self : object
            Returns the instance itself.
        csrr1   r   rq   r%   r   r$          @      ?r4   Fcopy)r   r   r   r7   rI   rJ   r   multiplymeanvar_gammanormalr"   random_weights_uniformrG   pirandom_offset_dtypefloat32astyper<   )r)   r=   r>   r   r?   rb   X_vars          r*   r@   zRBFSampler.fitU  sy   * $777)$*;<<WQZ
Q:  @FSQZZ]]((**affhh1_<<AEEGGE8=

#e!344DKK*DK #dk 1c9L<O<Od/0 =P =
 =
  
 +221a"%idFW2XX7bj   $(#7#>#>qwU#>#S#SD "&"5"<"<QW5"<"Q"QD#0r,   c                     t          |            t          | |dd          }t          || j                  }|| j        z  }t          j        ||           |d| j        z  dz  z  }|S )a  Apply the approximate feature map to X.

        Parameters
        ----------
        X : {array-like, sparse matrix}, shape (n_samples, n_features)
            New data, where `n_samples` is the number of samples
            and `n_features` is the number of features.

        Returns
        -------
        X_new : array-like, shape (n_samples, n_components)
            Returns the instance itself.
        rv   FrB   rw   rx   )r   r   r   r   r   rG   cosr"   r)   r=   
projections      r*   r]   zRBFSampler.transform  sv     	$eDDD$Q(<==
d))

z:&&&sT..366
r,   c                 |    t                                                      }d|j        _        ddg|j        _        |S NTfloat64r   r_   r`   ra   rb   transformer_tagspreserves_dtyperc   s     r*   r`   zRBFSampler.__sklearn_tags__  7    ww''))!%1:I0F-r,   r(   )rg   rh   ri   rj   r   r   r   r   r#   rk   rl   r+   r   r@   r]   r`   rm   rn   s   @r*   rp   rp      s        P Ph Jy!!HT3V444
 "(AtFCCCD'($ $D    !$#D ) ) ) ) )
 \555+ + + 65+Z  .        r,   rp   c                        e Zd ZU dZ eeddd          g eeddd          gdgdZee	d	<   d
ddddZ
 ed          dd            Zd Z fdZ xZS )SkewedChi2Samplera
  Approximate feature map for "skewed chi-squared" kernel.

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

    Parameters
    ----------
    skewedness : float, default=1.0
        "skewedness" parameter of the kernel. Needs to be cross-validated.

    n_components : int, default=100
        Number of Monte Carlo samples per original feature.
        Equals the dimensionality of the computed feature space.

    random_state : int, RandomState instance or None, default=None
        Pseudo-random number generator to control the generation of the random
        weights and random offset when fitting the training data.
        Pass an int for reproducible output across multiple function calls.
        See :term:`Glossary <random_state>`.

    Attributes
    ----------
    random_weights_ : ndarray of shape (n_features, n_components)
        Weight array, sampled from a secant hyperbolic distribution, which will
        be used to linearly transform the log of the data.

    random_offset_ : ndarray of shape (n_features, n_components)
        Bias term, which will be added to the data. It is uniformly distributed
        between 0 and 2*pi.

    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

    See Also
    --------
    AdditiveChi2Sampler : Approximate feature map for additive chi2 kernel.
    Nystroem : Approximate a kernel map using a subset of the training data.
    RBFSampler : Approximate a RBF kernel feature map using random Fourier
        features.
    SkewedChi2Sampler : Approximate feature map for "skewed chi-squared" kernel.
    sklearn.metrics.pairwise.chi2_kernel : The exact chi squared kernel.
    sklearn.metrics.pairwise.kernel_metrics : List of built-in kernels.

    References
    ----------
    See "Random Fourier Approximations for Skewed Multiplicative Histogram
    Kernels" by Fuxin Li, Catalin Ionescu and Cristian Sminchisescu.

    Examples
    --------
    >>> from sklearn.kernel_approximation import SkewedChi2Sampler
    >>> from sklearn.linear_model import SGDClassifier
    >>> X = [[0, 0], [1, 1], [1, 0], [0, 1]]
    >>> y = [0, 0, 1, 1]
    >>> chi2_feature = SkewedChi2Sampler(skewedness=.01,
    ...                                  n_components=10,
    ...                                  random_state=0)
    >>> X_features = chi2_feature.fit_transform(X, y)
    >>> clf = SGDClassifier(max_iter=10, tol=1e-3)
    >>> clf.fit(X_features, y)
    SGDClassifier(max_iter=10)
    >>> clf.score(X_features, y)
    1.0
    Nr   r   r   r   r   
skewednessr"   r   r#   r$   r&   c                0    || _         || _        || _        d S r(   r   )r)   r   r"   r   s       r*   r+   zSkewedChi2Sampler.__init__  s    $((r,   Tr-   c                 p   t          | |          }t          | j                  }|j        d         }|                    || j        f          }dt          j        z  t          j        t          j	        t          j        dz  |z                      z  | _
        |                    ddt          j        z  | j                  | _        |j        t          j        k    rL| j
                            |j        d          | _
        | j                            |j        d          | _        | j        | _        | S )	a<  Fit the model with X.

        Samples random projection according to n_features.

        Parameters
        ----------
        X : array-like, shape (n_samples, n_features)
            Training data, where `n_samples` is the number of samples
            and `n_features` is the number of features.

        y : array-like, shape (n_samples,) or (n_samples, n_outputs),                 default=None
            Target values (None for unsupervised transformations).

        Returns
        -------
        self : object
            Returns the instance itself.
        r   ry   r$   rw   r   r%   Frz   )r   r   r   r7   r   r"   rG   r   logtanr   r   r   r   r   r<   )r)   r=   r>   r   r?   r   s         r*   r@   zSkewedChi2Sampler.fit  s   * $"")$*;<<WQZ
&&Z9J,K&LL"RU{RVBF253;;P4Q4Q-R-RR*221a"%idFW2XX7bj   $(#7#>#>qwU#>#S#SD "&"5"<"<QW5"<"Q"QD#0r,   c                    t          |            t          | |dt          j        t          j        gd          }|| j         k                                    rt          d          || j        z  }t          j        ||           t          || j
                  }|| j        z  }t          j        ||           |t          j        d          t          j        | j                  z  z  }|S )a  Apply the approximate feature map to X.

        Parameters
        ----------
        X : array-like, shape (n_samples, n_features)
            New data, where `n_samples` is the number of samples
            and `n_features` is the number of features. All values of X must be
            strictly greater than "-skewedness".

        Returns
        -------
        X_new : array-like, shape (n_samples, n_components)
            Returns the instance itself.
        TF)r{   r   rC   z3X may not contain entries smaller than -skewedness.rw   )r   r   rG   r   r   r   anyrM   r   r   r   r   r   rH   r"   r   s      r*   r]   zSkewedChi2Sampler.transform  s     	!$rz2:&>e
 
 
 $/!!&&(( 	TRSSS	T_
q!$Q(<==
d))

z:&&&bgcllRWT->%?%???
r,   c                 d    t                                                      }ddg|j        _        |S )Nr   r   )r_   r`   r   r   rc   s     r*   r`   z"SkewedChi2Sampler.__sklearn_tags__;  s-    ww''))1:I0F-r,   r(   rf   rn   s   @r*   r   r     s         F FR  xdDCCCD!(AtFCCCD'($ $D    &)s ) ) ) ) )
 \555# # # 65#J  <        r,   r   c                        e Zd ZU dZ eeddd          g eeddd          dgdZee	d<   d	ddd
Z
 ed          dd            Zd ZddZed             Zed             Z fdZ xZS )AdditiveChi2Samplera
  Approximate feature map for additive chi2 kernel.

    Uses sampling the fourier transform of the kernel characteristic
    at regular intervals.

    Since the kernel that is to be approximated is additive, the components of
    the input vectors can be treated separately.  Each entry in the original
    space is transformed into 2*sample_steps-1 features, where sample_steps is
    a parameter of the method. Typical values of sample_steps include 1, 2 and
    3.

    Optimal choices for the sampling interval for certain data ranges can be
    computed (see the reference). The default values should be reasonable.

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

    Parameters
    ----------
    sample_steps : int, default=2
        Gives the number of (complex) sampling points.

    sample_interval : float, default=None
        Sampling interval. Must be specified when sample_steps not in {1,2,3}.

    Attributes
    ----------
    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

    See Also
    --------
    SkewedChi2Sampler : A Fourier-approximation to a non-additive variant of
        the chi squared kernel.

    sklearn.metrics.pairwise.chi2_kernel : The exact chi squared kernel.

    sklearn.metrics.pairwise.additive_chi2_kernel : The exact additive chi
        squared kernel.

    Notes
    -----
    This estimator approximates a slightly different version of the additive
    chi squared kernel then ``metric.additive_chi2`` computes.

    This estimator is stateless and does not need to be fitted. However, we
    recommend to call :meth:`fit_transform` instead of :meth:`transform`, as
    parameter validation is only performed in :meth:`fit`.

    References
    ----------
    See `"Efficient additive kernels via explicit feature maps"
    <http://www.robots.ox.ac.uk/~vedaldi/assets/pubs/vedaldi11efficient.pdf>`_
    A. Vedaldi and A. Zisserman, Pattern Analysis and Machine Intelligence,
    2011

    Examples
    --------
    >>> from sklearn.datasets import load_digits
    >>> from sklearn.linear_model import SGDClassifier
    >>> from sklearn.kernel_approximation import AdditiveChi2Sampler
    >>> X, y = load_digits(return_X_y=True)
    >>> chi2sampler = AdditiveChi2Sampler(sample_steps=2)
    >>> X_transformed = chi2sampler.fit_transform(X, y)
    >>> clf = SGDClassifier(max_iter=5, random_state=0, tol=1e-3)
    >>> clf.fit(X_transformed, y)
    SGDClassifier(max_iter=5, random_state=0)
    >>> clf.score(X_transformed, y)
    0.9499...
    r   Nr   r   r   sample_stepssample_intervalr#   r%   c                "    || _         || _        d S r(   r   )r)   r   r   s      r*   r+   zAdditiveChi2Sampler.__init__  s    (.r,   Tr-   c                 j    t          | |dd          }| j        | j        dvrt          d          | S )a  Only validates estimator's parameters.

        This method allows to: (i) validate the estimator's parameters and
        (ii) be consistent with the scikit-learn transformer API.

        Parameters
        ----------
        X : array-like, shape (n_samples, n_features)
            Training data, where `n_samples` is the number of samples
            and `n_features` is the number of features.

        y : array-like, shape (n_samples,) or (n_samples, n_outputs),                 default=None
            Target values (None for unsupervised transformations).

        Returns
        -------
        self : object
            Returns the transformer.
        rv   T)r2   ensure_non_negativeN)r   r%      HIf sample_steps is not in [1, 2, 3], you need to provide sample_interval)r   r   r   rM   )r)   r=   r>   s      r*   r@   zAdditiveChi2Sampler.fit  sN    , $DQQQ'D,=Y,N,N7  
 r,   c                 $   t          | |ddd          }t          j        |          }| j        9| j        dk    rd}n2| j        dk    rd	}n$| j        d
k    rd}nt          d          | j        }|r| j        n| j        } ||| j        |          S )a  Apply approximate feature map to X.

        Parameters
        ----------
        X : {array-like, sparse matrix}, shape (n_samples, n_features)
            Training data, where `n_samples` is the number of samples
            and `n_features` is the number of features.

        Returns
        -------
        X_new : {ndarray, sparse matrix},                shape = (n_samples, n_features * (2*sample_steps - 1))
            Whether the return value is an array or sparse matrix depends on
            the type of the input X.
        rv   FT)r2   rC   r   Nr   g?r%   rx   r   g?r   )r   rI   rJ   r   r   rM   _transform_sparse_transform_dense)r)   r=   rb   r   transfs        r*   r]   zAdditiveChi2Sampler.transform  s      !54
 
 
 Q'
  A%%"%"a''"%"a''"% ;  
 #2O
 ,2L''t7Lva*O<<<r,   c                 v   t          | d           t          | |d          }| j        j                                        fd|D             }t          d| j                  D ]8fd|D             }fd|D             }|                    ||z              9t          j	        |t          	          S )
al  Get output feature names for transformation.

        Parameters
        ----------
        input_features : array-like of str or None, default=None
            Only used to validate feature names with the names seen in :meth:`fit`.

        Returns
        -------
        feature_names_out : ndarray of str objects
            Transformed feature names.
        n_features_in_)
attributesT)generate_namesc                      g | ]
} d | dS )__sqrt ).0nameest_names     r*   
<listcomp>z=AdditiveChi2Sampler.get_feature_names_out.<locals>.<listcomp>  s*    KKKD//D///KKKr,   r   c                 $    g | ]} d | d S )r   _cosr   r   r   r   rV   s     r*   r   z=AdditiveChi2Sampler.get_feature_names_out.<locals>.<listcomp>   /    PPPH44t4444PPPr,   c                 $    g | ]} d | d S )r   _sinr   r   s     r*   r   z=AdditiveChi2Sampler.get_feature_names_out.<locals>.<listcomp>  r   r,   )r   )r   r   re   rg   lowerrO   r   extendrG   asarrayobject)r)   input_features
names_list	cos_names	sin_namesr   rV   s        @@r*   get_feature_names_outz)AdditiveChi2Sampler.get_feature_names_out  s      	)9::::0.
 
 
 >*0022KKKKNKKK
q$+,, 	5 	5APPPPPPPPIPPPPPPPPIi)34444z*F3333r,   c                    | dk    }| |         }t          j        |           }t          j        ||z            ||<   |g}|t          j        |          z  }d|z  |z  }t	          d|          D ]}	t          j        |t          j        t           j        |	z  |z            z            }
t          j        |           }|
t          j        |	|z            z  ||<   |                    |           t          j        |           }|
t          j	        |	|z            z  ||<   |                    |           t          j
        |          S )Nrr   r%   r   )rG   
zeros_likerH   r   rO   coshr   r   appendsinrK   )r=   r   r   non_zeroX_nzX_stepX_newlog_step_nzstep_nzrV   	factor_nzs              r*   r   z$AdditiveChi2Sampler._transform_dense  s4   8{q!!74/#9::x%t4d(_,q,'' 		! 		!A"'"%!)o2M*N*N NOOI]1%%F(26!k/+B+BBF8LL   ]1%%F(26!k/+B+BBF8LL    yr,   c                 P   | j                                         }| j                                        }t          j        | j        |z            }t          j        |||f| j        | j	        d          }|g}|t          j
        | j                  z  }d| j        z  |z  }	t          d|          D ]}
t          j        |	t          j        t          j        |
z  |z            z            }|t          j        |
|z            z  }t          j        |||f| j        | j	        d          }|                    |           |t          j        |
|z            z  }t          j        |||f| j        | j	        d          }|                    |           t          j        |          S )NF)r7   r   r{   r%   r   )indicesr{   indptrrG   rH   datarI   
csr_matrixr7   r   r   rO   r   r   r   r   r   rK   )r=   r   r   r   r   	data_stepr   r   r   r   rV   r   s               r*   r   z%AdditiveChi2Sampler._transform_sparse   s   )..""GAF_455	(qwU
 
 
 %qv6af*.q,'' 	! 	!A"'"%!)o2M*N*N NOOI!BF1{?$;$;;I]GV,AG17QV  F LL   !BF1{?$;$;;I]GV,AG17QV  F LL    yr,   c                     t                                                      }d|_        d|j        _        d|j        _        |S )NFT)r_   r`   requires_fitra   positive_onlyrb   rc   s     r*   r`   z$AdditiveChi2Sampler.__sklearn_tags__?  s9    ww''))!(,%!%r,   r(   )rg   rh   ri   rj   r   r   r   r#   rk   rl   r+   r   r@   r]   r   staticmethodr   r   r`   rm   rn   s   @r*   r   r   A  sC        L L^ "(AtFCCCD$HT1d6BBBDI$ $D   
 ()$ / / / / / \555   65>,= ,= ,=\4 4 4 4>     \ 2     \ <        r,   r   c                   b    e Zd ZU dZ e e ej                              dhz            eg e	e
ddd          dg e	e
ddd          dg e	e
ddd          dgedg e	eddd          gd	gedgd
Zeed<   	 ddddddddddZ ed          dd            Zd Zd Z fdZ xZS )Nystroema  Approximate a kernel map using a subset of the training data.

    Constructs an approximate feature map for an arbitrary kernel
    using a subset of the data as basis.

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

    .. versionadded:: 0.13

    Parameters
    ----------
    kernel : str or callable, default='rbf'
        Kernel map to be approximated. A callable should accept two arguments
        and the keyword arguments passed to this object as `kernel_params`, and
        should return a floating point number.

    gamma : float, default=None
        Gamma parameter for the RBF, laplacian, polynomial, exponential chi2
        and sigmoid kernels. Interpretation of the default value is left to
        the kernel; see the documentation for sklearn.metrics.pairwise.
        Ignored by other kernels.

    coef0 : float, default=None
        Zero coefficient for polynomial and sigmoid kernels.
        Ignored by other kernels.

    degree : float, default=None
        Degree of the polynomial kernel. Ignored by other kernels.

    kernel_params : dict, default=None
        Additional parameters (keyword arguments) for kernel function passed
        as callable object.

    n_components : int, default=100
        Number of features to construct.
        How many data points will be used to construct the mapping.

    random_state : int, RandomState instance or None, default=None
        Pseudo-random number generator to control the uniform sampling without
        replacement of `n_components` of the training data to construct the
        basis kernel.
        Pass an int for reproducible output across multiple function calls.
        See :term:`Glossary <random_state>`.

    n_jobs : int, default=None
        The number of jobs to use for the computation. This works by breaking
        down the kernel matrix into `n_jobs` even slices and computing them in
        parallel.

        ``None`` means 1 unless in a :obj:`joblib.parallel_backend` context.
        ``-1`` means using all processors. See :term:`Glossary <n_jobs>`
        for more details.

        .. versionadded:: 0.24

    Attributes
    ----------
    components_ : ndarray of shape (n_components, n_features)
        Subset of training points used to construct the feature map.

    component_indices_ : ndarray of shape (n_components)
        Indices of ``components_`` in the training set.

    normalization_ : ndarray of shape (n_components, n_components)
        Normalization matrix needed for embedding.
        Square root of the kernel matrix on ``components_``.

    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

    See Also
    --------
    AdditiveChi2Sampler : Approximate feature map for additive chi2 kernel.
    PolynomialCountSketch : Polynomial kernel approximation via Tensor Sketch.
    RBFSampler : Approximate a RBF kernel feature map using random Fourier
        features.
    SkewedChi2Sampler : Approximate feature map for "skewed chi-squared" kernel.
    sklearn.metrics.pairwise.kernel_metrics : List of built-in kernels.

    References
    ----------
    * Williams, C.K.I. and Seeger, M.
      "Using the Nystroem method to speed up kernel machines",
      Advances in neural information processing systems 2001

    * T. Yang, Y. Li, M. Mahdavi, R. Jin and Z. Zhou
      "Nystroem Method vs Random Fourier Features: A Theoretical and Empirical
      Comparison",
      Advances in Neural Information Processing Systems 2012

    Examples
    --------
    >>> from sklearn import datasets, svm
    >>> from sklearn.kernel_approximation import Nystroem
    >>> X, y = datasets.load_digits(n_class=9, return_X_y=True)
    >>> data = X / 16.
    >>> clf = svm.LinearSVC()
    >>> feature_map_nystroem = Nystroem(gamma=.2,
    ...                                 random_state=1,
    ...                                 n_components=300)
    >>> data_transformed = feature_map_nystroem.fit_transform(data)
    >>> clf.fit(data_transformed, y)
    LinearSVC()
    >>> clf.score(data_transformed, y)
    0.9987...
    precomputedr   Nr   r   r   r   r   kernelr   r!   r    kernel_paramsr"   r   n_jobsr#   rbfr&   )r   r!   r    r   r"   r   r   c                v    || _         || _        || _        || _        || _        || _        || _        || _        d S r(   r   )	r)   r   r   r!   r    r   r"   r   r   s	            r*   r+   zNystroem.__init__  sC     

*((r,   Tr-   c                 ^   t          | |d          }t          | j                  }|j        d         }| j        |k    r|}t          j        d           n| j        }t          ||          }|                    |          }|d|         }||         }t          |f| j
        d| j        d|                                 }	t          |	          \  }
}}t          j        |d          }t          j        |
t          j        |          z  |          | _        || _        || _        || _        | S )	as  Fit estimator to data.

        Samples a subset of training points, computes kernel
        on these and computes normalization matrix.

        Parameters
        ----------
        X : array-like, shape (n_samples, n_features)
            Training data, where `n_samples` is the number of samples
            and `n_features` is the number of features.

        y : array-like, shape (n_samples,) or (n_samples, n_outputs),                 default=None
            Target values (None for unsupervised transformations).

        Returns
        -------
        self : object
            Returns the instance itself.
        rv   r1   r   zn_components > n_samples. This is not possible.
n_components was set to n_samples, which results in inefficient evaluation of the full kernel.NTmetricfilter_paramsr   g-q=)r   r   r   r7   r"   warningswarnminpermutationr   r   r   _get_kernel_paramsr   rG   maximumdotrH   normalization_components_component_indices_r<   )r)   r=   r>   rnd	n_samplesr"   inds
basis_indsbasisbasis_kernelUSVs                r*   r@   zNystroem.fit  sJ   , $777 !233GAJ	 y(($LMA     ,L9l33y))-<-(
*'
;;	
 

 %%''
 
 l##1aJq%   fQ^Q77 ",+r,   c                     t          |            t          | |dd          }|                                 }t          || j        f| j        d| j        d|}t          j        || j	        j
                  S )a  Apply feature map to X.

        Computes an approximate feature map using the kernel
        between some training points and X.

        Parameters
        ----------
        X : array-like of shape (n_samples, n_features)
            Data to transform.

        Returns
        -------
        X_transformed : ndarray of shape (n_samples, n_components)
            Transformed data.
        rv   FrB   Tr   )r   r   r   r   r   r   r   rG   r   r   T)r)   r=   r   embeddeds       r*   r]   zNystroem.transform  s      	$eDDD//11#
 ;;
 
 
 
 vh 3 5666r,   c                    | j         }|i }t          | j                  sD| j        dk    r9t          | j                 D ]%}t	          | |          t	          | |          ||<   &n$| j        | j        | j        t          d          |S )Nr   zWDon't pass gamma, coef0 or degree to Nystroem if using a callable or precomputed kernel)	r   callabler   r   getattrr   r!   r    rM   )r)   paramsparams      r*   r   zNystroem._get_kernel_params8  s    #>F$$ 	)E)E&t{3 9 94''3$+D%$8$8F5M9
 
&:);* ,   r,   c                 |    t                                                      }d|j        _        ddg|j        _        |S r   r   rc   s     r*   r`   zNystroem.__sklearn_tags__N  r   r,   )r   r(   )rg   rh   ri   rj   r   setr   keysr  r   r   rk   r   r#   rl   r+   r   r@   r]   r   r`   rm   rn   s   @r*   r   r   G  s        q qj Jss949;;<<NOO
 (4D888$?(4tI>>>E8D!T&9994@!(AtFCCCD'(T"$ $D          * \5559 9 9 659v7 7 7<  ,        r,   r   )(rj   r   numbersr   r   numpyrG   scipy.sparserb   rI   	scipy.fftr   r   scipy.linalgr   baser	   r
   r   r   metrics.pairwiser   r   r   utilsr   utils._param_validationr   r   utils.extmathr   utils.validationr   r   r   r   rp   r   r   r   r   r,   r*   <module>r     sf   U U
  " " " " " " " "                                    Y X X X X X X X X X % % % % % % 9 9 9 9 9 9 9 9 * * * * * *         R R R R R#%5}R R Rjj j j j j02BM j j jZ] ] ] ] ]#%5}] ] ]@C C C C C*M C C CLK K K K K.0@- K K K K Kr,   