§
    rŠtj;a  ã                   óþ   — d Z ddlZddlZddlmZ ddlmZ ddl	m
Z
mZ ddlmZ ddlmZ ddlmZ ddlmZ dd	lmZ d
„ Zd„ Zd„ Zd„ Zdd„Zddœd„Zd„ Zd„ Zd„ Zd„ Zd„ Z d„ Z!d d„Z"d!d„Z#d„ Z$d„ Z%d„ Z&d„ Z'd"d„Z(dS )#zBA collection of utilities to work with sparse matrices and arrays.é    N)ÚLinearOperator)Ú_sparse_min_maxÚ_sparse_nan_min_max)Úcsc_mean_variance_axis0)Úcsr_matmul_csr_to_dense)Úcsr_mean_variance_axis0)Úincr_mean_variance_axis0)Ú_check_sample_weightc                 ó~   — t          j        | ¦  «        r| j        nt          | ¦  «        }d|z  }t	          |¦  «        ‚)z2Raises a TypeError if X is not a CSR or CSC matrixz,Expected a CSR or CSC sparse matrix, got %s.)ÚspÚissparseÚformatÚtypeÚ	TypeError)ÚXÚ
input_typeÚerrs      úW/var/www/html/CA-Chatbot/venv/lib/python3.11/site-packages/sklearn/utils/sparsefuncs.pyÚ_raise_typeerrorr      s6   € åœ[¨™^œ^Ð8�”�µ°a±´€JØ
8¸:Ñ
E€CÝ
�C‰.Œ.Ðó    c                 ó2   — | dvrt          d| z  ¦  «        ‚d S )N)r   é   z8Unknown axis value: %d. Use 0 for rows, or 1 for columns)Ú
ValueError©Úaxiss    r   Ú_raise_error_wrong_axisr   #   s/   € Ø�6ÐÐÝØFÈÑMñ
ô 
ð 	
ð Ðr   c                 ó–   — |j         d         | j         d         k    sJ ‚| xj        |                     | j        d¬¦  «        z  c_        dS )aˆ  Inplace column scaling of a CSR matrix.

    Scale each feature of the data matrix by multiplying with specific scale
    provided by the caller assuming a (n_samples, n_features) shape.

    Parameters
    ----------
    X : sparse matrix of shape (n_samples, n_features)
        Matrix to normalize using the variance of the features.
        It should be of CSR format.

    scale : ndarray of shape (n_features,), dtype={np.float32, np.float64}
        Array of precomputed feature-wise values to use for scaling.

    Examples
    --------
    >>> from sklearn.utils import sparsefuncs
    >>> from scipy import sparse
    >>> import numpy as np
    >>> indptr = np.array([0, 3, 4, 4, 4])
    >>> indices = np.array([0, 1, 2, 2])
    >>> data = np.array([8, 1, 2, 5])
    >>> scale = np.array([2, 3, 2])
    >>> csr = sparse.csr_array((data, indices, indptr))
    >>> csr.todense()
    array([[8, 1, 2],
           [0, 0, 5],
           [0, 0, 0],
           [0, 0, 0]])
    >>> sparsefuncs.inplace_csr_column_scale(csr, scale)
    >>> csr.todense()
    array([[16,  3,  4],
           [ 0,  0, 10],
           [ 0,  0,  0],
           [ 0,  0,  0]])
    r   r   Úclip)ÚmodeN)ÚshapeÚdataÚtakeÚindices©r   Úscales     r   Úinplace_csr_column_scaler&   *   sH   € ðJ Œ;�qŒ>˜QœW QœZÒ'Ð'Ð'Ð'Ø€F„Fˆe�jŠj˜œ¨ˆjÑ0Ô0Ñ0€F„F€F€Fr   c                 ó¶   — |j         d         | j         d         k    sJ ‚| xj        t          j        |t          j        | j        ¦  «        ¦  «        z  c_        dS )aÂ  Inplace row scaling of a CSR matrix.

    Scale each sample of the data matrix by multiplying with specific scale
    provided by the caller assuming a (n_samples, n_features) shape.

    Parameters
    ----------
    X : sparse matrix of shape (n_samples, n_features)
        Matrix to be scaled. It should be of CSR format.

    scale : ndarray of float of shape (n_samples,)
        Array of precomputed sample-wise values to use for scaling.
    r   N)r    r!   ÚnpÚrepeatÚdiffÚindptrr$   s     r   Úinplace_csr_row_scaler,   S   sM   € ð Œ;�qŒ>˜QœW QœZÒ'Ð'Ð'Ð'Ø€F„F�bŒi˜�rœw q¤xÑ0Ô0Ñ1Ô1Ñ1€F„F€F€Fr   Fc                 óz  — t          |¦  «         t          j        | ¦  «        r:| j        dk    r/|dk    rt	          | ||¬¦  «        S t          | j        ||¬¦  «        S t          j        | ¦  «        r:| j        dk    r/|dk    rt          | ||¬¦  «        S t	          | j        ||¬¦  «        S t          | ¦  «         dS )av  Compute mean and variance along an axis on a CSR or CSC matrix.

    Parameters
    ----------
    X : sparse matrix of shape (n_samples, n_features)
        Input data. It can be of CSR or CSC format.

    axis : {0, 1}
        Axis along which the axis should be computed.

    weights : ndarray of shape (n_samples,) or (n_features,), default=None
        If axis is set to 0 shape is (n_samples,) or
        if axis is set to 1 shape is (n_features,).
        If it is set to None, then samples are equally weighted.

        .. versionadded:: 0.24

    return_sum_weights : bool, default=False
        If True, returns the sum of weights seen for each feature
        if `axis=0` or each sample if `axis=1`.

        .. versionadded:: 0.24

    Returns
    -------

    means : ndarray of shape (n_features,), dtype=floating
        Feature-wise means.

    variances : ndarray of shape (n_features,), dtype=floating
        Feature-wise variances.

    sum_weights : ndarray of shape (n_features,), dtype=floating
        Returned if `return_sum_weights` is `True`.

    Examples
    --------
    >>> from sklearn.utils import sparsefuncs
    >>> from scipy import sparse
    >>> import numpy as np
    >>> indptr = np.array([0, 3, 4, 4, 4])
    >>> indices = np.array([0, 1, 2, 2])
    >>> data = np.array([8, 1, 2, 5])
    >>> scale = np.array([2, 3, 2])
    >>> csr = sparse.csr_array((data, indices, indptr))
    >>> csr.todense()
    array([[8, 1, 2],
           [0, 0, 5],
           [0, 0, 0],
           [0, 0, 0]])
    >>> sparsefuncs.mean_variance_axis(csr, axis=0)
    (array([2.  , 0.25, 1.75]), array([12.    ,  0.1875,  4.1875]))
    Úcsrr   )ÚweightsÚreturn_sum_weightsÚcscN)r   r   r   r   Ú_csr_mean_var_axis0Ú_csc_mean_var_axis0ÚTr   )r   r   r/   r0   s       r   Úmean_variance_axisr5   e   sù   € õl ˜DÑ!Ô!Ð!å	„{�1�~„~ð ˜!œ( eÒ+Ð+Ø�1Š9ˆ9Ý&Ø˜7Ð7Iðñ ô ð õ 'Ø”˜WÐ9Kðñ ô ð õ 
Œ�Q‰Œð 
˜AœH¨Ò-Ð-Ø�1Š9ˆ9Ý&Ø˜7Ð7Iðñ ô ð õ 'Ø”˜WÐ9Kðñ ô ð õ 	˜ÑÔÐÐÐr   )r/   c                óp  — t          |¦  «         t          j        | ¦  «        r	| j        dv st	          | ¦  «         t          j        |¦  «        dk    r!t          j        |j        ||j	        ¬¦  «        }t          j        |¦  «        t          j        |¦  «        cxk    rt          j        |¦  «        k    sn t          d¦  «        ‚|dk    rWt          j        |¦  «        | j        d         k    r3t          d| j        d         › dt          j        |¦  «        › d�¦  «        ‚nVt          j        |¦  «        | j        d         k    r3t          d	| j        d         › dt          j        |¦  «        › d�¦  «        ‚|dk    r| j        n| } |�t          || | j	        ¬¦  «        }t          | ||||¬¦  «        S )aÝ  Compute incremental mean and variance along an axis on a CSR or CSC matrix.

    last_mean, last_var are the statistics computed at the last step by this
    function. Both must be initialized to 0-arrays of the proper size, i.e.
    the number of features in X. last_n is the number of samples encountered
    until now.

    Parameters
    ----------
    X : CSR or CSC sparse matrix of shape (n_samples, n_features)
        Input data.

    axis : {0, 1}
        Axis along which the axis should be computed.

    last_mean : ndarray of shape (n_features,) or (n_samples,), dtype=floating
        Array of means to update with the new data X.
        Should be of shape (n_features,) if axis=0 or (n_samples,) if axis=1.

    last_var : ndarray of shape (n_features,) or (n_samples,), dtype=floating
        Array of variances to update with the new data X.
        Should be of shape (n_features,) if axis=0 or (n_samples,) if axis=1.

    last_n : float or ndarray of shape (n_features,) or (n_samples,),             dtype=floating
        Sum of the weights seen so far, excluding the current weights
        If not float, it should be of shape (n_features,) if
        axis=0 or (n_samples,) if axis=1. If float it corresponds to
        having same weights for all samples (or features).

    weights : ndarray of shape (n_samples,) or (n_features,), default=None
        If axis is set to 0 shape is (n_samples,) or
        if axis is set to 1 shape is (n_features,).
        If it is set to None, then samples are equally weighted.

        .. versionadded:: 0.24

    Returns
    -------
    means : ndarray of shape (n_features,) or (n_samples,), dtype=floating
        Updated feature-wise means if axis = 0 or
        sample-wise means if axis = 1.

    variances : ndarray of shape (n_features,) or (n_samples,), dtype=floating
        Updated feature-wise variances if axis = 0 or
        sample-wise variances if axis = 1.

    n : ndarray of shape (n_features,) or (n_samples,), dtype=integral
        Updated number of seen samples per feature if axis=0
        or number of seen features per sample if axis=1.

        If weights is not None, n is a sum of the weights of the seen
        samples or features instead of the actual number of seen
        samples or features.

    Notes
    -----
    NaNs are ignored in the algorithm.

    Examples
    --------
    >>> from sklearn.utils import sparsefuncs
    >>> from scipy import sparse
    >>> import numpy as np
    >>> indptr = np.array([0, 3, 4, 4, 4])
    >>> indices = np.array([0, 1, 2, 2])
    >>> data = np.array([8, 1, 2, 5])
    >>> scale = np.array([2, 3, 2])
    >>> csr = sparse.csr_array((data, indices, indptr))
    >>> csr.todense()
    array([[8, 1, 2],
           [0, 0, 5],
           [0, 0, 0],
           [0, 0, 0]])
    >>> sparsefuncs.incr_mean_variance_axis(
    ...     csr, axis=0, last_mean=np.zeros(3), last_var=np.zeros(3), last_n=2
    ... )
    (array([1.33, 0.167, 1.17]), array([8.88, 0.139, 3.47]),
    array([6., 6., 6.]))
    ©r1   r.   r   ©Údtypez8last_mean, last_var, last_n do not have the same shapes.r   zHIf axis=1, then last_mean, last_n, last_var should be of size n_samples z (Got z).zIIf axis=0, then last_mean, last_n, last_var should be of size n_features N)Ú	last_meanÚlast_varÚlast_nr/   )r   r   r   r   r   r(   ÚsizeÚfullr    r9   r   r4   r
   Ú_incr_mean_var_axis0)r   r   r:   r;   r<   r/   s         r   Úincr_mean_variance_axisr@   ³   så  € õb ˜DÑ!Ô!Ð!åŒK˜‰NŒNð ˜qœx¨>Ð9Ð9Ý˜ÑÔÐå	„wˆv�„˜!ÒÐÝ”˜œ¨&¸	¼ÐHÑHÔHˆåŒG�IÑÔ¥"¤'¨(Ñ"3Ô"3ÐFÐFÒFÐFµr´w¸v±´ÒFÐFÐFÐFÝÐSÑTÔTÐTàˆq‚y€yÝŒ7�9ÑÔ ¤¨¤Ò+Ð+ÝðKØ"#¤'¨!¤*ðKð KÝ46´G¸IÑ4FÔ4FðKð Kð Kñô ð ð ,õ Œ7�9ÑÔ ¤¨¤Ò+Ð+ÝðLØ#$¤7¨1¤:ðLð LÝ57´W¸YÑ5GÔ5GðLð Lð Lñô ð ð
 �qŠyˆyˆŒˆ˜a€AàÐÝ& w°¸¼ÐAÑAÔAˆåØ	�Y¨¸&È'ðñ ô ð r   c                 óò   — t          j        | ¦  «        r"| j        dk    rt          | j        |¦  «         dS t          j        | ¦  «        r| j        dk    rt          | |¦  «         dS t          | ¦  «         dS )a�  Inplace column scaling of a CSC/CSR matrix.

    Scale each feature of the data matrix by multiplying with specific scale
    provided by the caller assuming a (n_samples, n_features) shape.

    Parameters
    ----------
    X : sparse matrix of shape (n_samples, n_features)
        Matrix to normalize using the variance of the features. It should be
        of CSC or CSR format.

    scale : ndarray of shape (n_features,), dtype={np.float32, np.float64}
        Array of precomputed feature-wise values to use for scaling.

    Examples
    --------
    >>> from sklearn.utils import sparsefuncs
    >>> from scipy import sparse
    >>> import numpy as np
    >>> indptr = np.array([0, 3, 4, 4, 4])
    >>> indices = np.array([0, 1, 2, 2])
    >>> data = np.array([8, 1, 2, 5])
    >>> scale = np.array([2, 3, 2])
    >>> csr = sparse.csr_array((data, indices, indptr))
    >>> csr.todense()
    array([[8, 1, 2],
           [0, 0, 5],
           [0, 0, 0],
           [0, 0, 0]])
    >>> sparsefuncs.inplace_column_scale(csr, scale)
    >>> csr.todense()
    array([[16,  3,  4],
           [ 0,  0, 10],
           [ 0,  0,  0],
           [ 0,  0,  0]])
    r1   r.   N)r   r   r   r,   r4   r&   r   r$   s     r   Úinplace_column_scalerB   &  s   € õJ 
„{�1�~„~ð ˜!œ( eÒ+Ð+Ý˜aœc 5Ñ)Ô)Ð)Ð)Ð)Ý	Œ�Q‰Œð ˜AœH¨Ò-Ð-Ý   EÑ*Ô*Ð*Ð*Ð*å˜ÑÔÐÐÐr   c                 óò   — t          j        | ¦  «        r"| j        dk    rt          | j        |¦  «         dS t          j        | ¦  «        r| j        dk    rt          | |¦  «         dS t          | ¦  «         dS )a…  Inplace row scaling of a CSR or CSC matrix.

    Scale each row of the data matrix by multiplying with specific scale
    provided by the caller assuming a (n_samples, n_features) shape.

    Parameters
    ----------
    X : sparse matrix of shape (n_samples, n_features)
        Matrix to be scaled. It should be of CSR or CSC format.

    scale : ndarray of shape (n_features,), dtype={np.float32, np.float64}
        Array of precomputed sample-wise values to use for scaling.

    Examples
    --------
    >>> from sklearn.utils import sparsefuncs
    >>> from scipy import sparse
    >>> import numpy as np
    >>> indptr = np.array([0, 2, 3, 4, 5])
    >>> indices = np.array([0, 1, 2, 3, 3])
    >>> data = np.array([8, 1, 2, 5, 6])
    >>> scale = np.array([2, 3, 4, 5])
    >>> csr = sparse.csr_array((data, indices, indptr))
    >>> csr.todense()
    array([[8, 1, 0, 0],
           [0, 0, 2, 0],
           [0, 0, 0, 5],
           [0, 0, 0, 6]])
    >>> sparsefuncs.inplace_row_scale(csr, scale)
    >>> csr.todense()
     array([[16,  2,  0,  0],
            [ 0,  0,  6,  0],
            [ 0,  0,  0, 20],
            [ 0,  0,  0, 30]])
    r1   r.   N)r   r   r   r&   r4   r,   r   r$   s     r   Úinplace_row_scalerD   S  s   € õH 
„{�1�~„~ð ˜!œ( eÒ+Ð+Ý  ¤ eÑ,Ô,Ð,Ð,Ð,Ý	Œ�Q‰Œð ˜AœH¨Ò-Ð-Ý˜a Ñ'Ô'Ð'Ð'Ð'å˜ÑÔÐÐÐr   c                 ó  — ||fD ]+}t          |t          j        ¦  «        rt          d¦  «        ‚Œ,|dk     r|| j        d         z  }|dk     r|| j        d         z  }| j        |k    }|| j        | j        |k    <   || j        |<   dS )aK  Swap two rows of a CSC matrix in-place.

    Parameters
    ----------
    X : sparse matrix of shape (n_samples, n_features)
        Matrix whose two rows are to be swapped. It should be of
        CSC format.

    m : int
        Index of the row of X to be swapped.

    n : int
        Index of the row of X to be swapped.
    ú m and n should be valid integersr   N)Ú
isinstancer(   Úndarrayr   r    r#   )r   ÚmÚnÚtÚm_masks        r   Úinplace_swap_row_cscrM     s¤   € ð �ˆVð @ð @ˆÝ�a�œÑ$Ô$ð 	@ÝÐ>Ñ?Ô?Ð?ð	@ð 	ˆ1‚u€uØ	ˆQŒW�QŒZ‰ˆØˆ1‚u€uØ	ˆQŒW�QŒZ‰ˆàŒY˜!Š^€FØ !€A„IˆaŒi˜1ŠnÑØ€A„IˆfÑÐÐr   c           	      ó<  — ||fD ]+}t          |t          j        ¦  «        rt          d¦  «        ‚Œ,|dk     r|| j        d         z  }|dk     r|| j        d         z  }||k    r||}}| j        }||         }||dz            }||         }||dz            }||z
  }	||z
  }
|	|
k    r:| j        |dz   |…xx         |
|	z
  z  cc<   ||
z   | j        |dz   <   ||	z
  | j        |<   t          j        | j        d|…         | j        ||…         | j        ||…         | j        ||…         | j        |d…         g¦  «        | _        t          j        | j        d|…         | j        ||…         | j        ||…         | j        ||…         | j        |d…         g¦  «        | _        dS )aK  Swap two rows of a CSR matrix in-place.

    Parameters
    ----------
    X : sparse matrix of shape (n_samples, n_features)
        Matrix whose two rows are to be swapped. It should be of
        CSR format.

    m : int
        Index of the row of X to be swapped.

    n : int
        Index of the row of X to be swapped.
    rF   r   r   é   N)	rG   r(   rH   r   r    r+   Úconcatenater#   r!   )r   rI   rJ   rK   r+   Úm_startÚm_stopÚn_startÚn_stopÚnz_mÚnz_ns              r   Úinplace_swap_row_csrrW   œ  sæ  € ð �ˆVð @ð @ˆÝ�a�œÑ$Ô$ð 	@ÝÐ>Ñ?Ô?Ð?ð	@ð 	ˆ1‚u€uØ	ˆQŒW�QŒZ‰ˆØˆ1‚u€uØ	ˆQŒW�QŒZ‰ˆð 	ˆ1‚u€uØ�!ˆ1ˆàŒX€FØ�QŒi€GØ�A˜‘EŒ]€FØ�QŒi€GØ�A˜‘EŒ]€FØ�GÑ€DØ�GÑ€Dàˆt‚|€|à	Œ��Q‘˜�ÐÐÔ˜t d™{Ñ*ÐÐÑØ! D™.ˆŒ��Q‘‰Ø˜t‘mˆŒ�‰å”àŒI�h�w�hÔØŒI�g˜f�nÔ%ØŒI�f˜W�nÔ%ØŒI�g˜f�nÔ%ØŒI�f�g�gÔð	
ñô €A„Iõ Œ^àŒF�8�G�8ÔØŒF�7˜6�>Ô"ØŒF�6˜'�>Ô"ØŒF�7˜6�>Ô"ØŒF�6�7�7ŒOð	
ñô €A„F€F€Fr   c                 óì   — t          j        | ¦  «        r| j        dk    rt          | ||¦  «         dS t          j        | ¦  «        r| j        dk    rt	          | ||¦  «         dS t          | ¦  «         dS )aš  
    Swap two rows of a CSC/CSR matrix in-place.

    Parameters
    ----------
    X : sparse matrix of shape (n_samples, n_features)
        Matrix whose two rows are to be swapped. It should be of CSR or
        CSC format.

    m : int
        Index of the row of X to be swapped.

    n : int
        Index of the row of X to be swapped.

    Examples
    --------
    >>> from sklearn.utils import sparsefuncs
    >>> from scipy import sparse
    >>> import numpy as np
    >>> indptr = np.array([0, 2, 3, 3, 3])
    >>> indices = np.array([0, 2, 2])
    >>> data = np.array([8, 2, 5])
    >>> csr = sparse.csr_array((data, indices, indptr))
    >>> csr.todense()
    array([[8, 0, 2],
           [0, 0, 5],
           [0, 0, 0],
           [0, 0, 0]])
    >>> sparsefuncs.inplace_swap_row(csr, 0, 1)
    >>> csr.todense()
    array([[0, 0, 5],
           [8, 0, 2],
           [0, 0, 0],
           [0, 0, 0]])
    r1   r.   N)r   r   r   rM   rW   r   ©r   rI   rJ   s      r   Úinplace_swap_rowrZ   Û  s�   € õJ 
„{�1�~„~ð ˜!œ( eÒ+Ð+Ý˜Q  1Ñ%Ô%Ð%Ð%Ð%Ý	Œ�Q‰Œð ˜AœH¨Ò-Ð-Ý˜Q  1Ñ%Ô%Ð%Ð%Ð%å˜ÑÔÐÐÐr   c                 óD  — |dk     r|| j         d         z  }|dk     r|| j         d         z  }t          j        | ¦  «        r| j        dk    rt	          | ||¦  «         dS t          j        | ¦  «        r| j        dk    rt          | ||¦  «         dS t          | ¦  «         dS )a©  
    Swap two columns of a CSC/CSR matrix in-place.

    Parameters
    ----------
    X : sparse matrix of shape (n_samples, n_features)
        Matrix whose two columns are to be swapped. It should be of
        CSR or CSC format.

    m : int
        Index of the column of X to be swapped.

    n : int
        Index of the column of X to be swapped.

    Examples
    --------
    >>> from sklearn.utils import sparsefuncs
    >>> from scipy import sparse
    >>> import numpy as np
    >>> indptr = np.array([0, 2, 3, 3, 3])
    >>> indices = np.array([0, 2, 2])
    >>> data = np.array([8, 2, 5])
    >>> csr = sparse.csr_array((data, indices, indptr))
    >>> csr.todense()
    array([[8, 0, 2],
           [0, 0, 5],
           [0, 0, 0],
           [0, 0, 0]])
    >>> sparsefuncs.inplace_swap_column(csr, 0, 1)
    >>> csr.todense()
    array([[0, 8, 2],
           [0, 0, 5],
           [0, 0, 0],
           [0, 0, 0]])
    r   r   r1   r.   N)r    r   r   r   rW   rM   r   rY   s      r   Úinplace_swap_columnr\     s±   € ðJ 	ˆ1‚u€uØ	ˆQŒW�QŒZ‰ˆØˆ1‚u€uØ	ˆQŒW�QŒZ‰ˆÝ	„{�1�~„~ð ˜!œ( eÒ+Ð+Ý˜Q  1Ñ%Ô%Ð%Ð%Ð%Ý	Œ�Q‰Œð ˜AœH¨Ò-Ð-Ý˜Q  1Ñ%Ô%Ð%Ð%Ð%å˜ÑÔÐÐÐr   c                 ó¦   — t          j        | ¦  «        r-| j        dv r$|rt          | |¬¦  «        S t	          | |¬¦  «        S t          | ¦  «         dS )a�  Compute minimum and maximum along an axis on a CSR or CSC matrix.

     Optionally ignore NaN values.

    Parameters
    ----------
    X : sparse matrix of shape (n_samples, n_features)
        Input data. It should be of CSR or CSC format.

    axis : {0, 1}
        Axis along which the axis should be computed.

    ignore_nan : bool, default=False
        Ignore or passing through NaN values.

        .. versionadded:: 0.20

    Returns
    -------

    mins : ndarray of shape (n_features,), dtype={np.float32, np.float64}
        Feature-wise minima.

    maxs : ndarray of shape (n_features,), dtype={np.float32, np.float64}
        Feature-wise maxima.
    )r.   r1   r   N)r   r   r   r   r   r   )r   r   Ú
ignore_nans      r   Úmin_max_axisr_   9  sb   € õ6 
„{�1�~„~ð ˜!œ( nÐ4Ð4Øð 	1Ý& q¨tÐ4Ñ4Ô4Ð4å" 1¨4Ð0Ñ0Ô0Ð0å˜ÑÔÐÐÐr   c                 ó´  — |dk    rd}n;|dk    rd}n2| j         dk    r't          d                      | j         ¦  «        ¦  «        ‚|€5|€| j        S t          j        t          j        | j        ¦  «        |¦  «        S |dk    r5t          j        | j        ¦  «        }|€|                     d¦  «        S ||z  S |dk    r{|€&t          j        | j	        | j
        d         ¬	¦  «        S t          j        |t          j        | j        ¦  «        ¦  «        }t          j        | j	        | j
        d         |¬
¦  «        S t          d                      |¦  «        ¦  «        ‚)a¾  A variant of X.getnnz() with extension to weighting on axis 0.

    Useful in efficiently calculating multilabel metrics.

    Parameters
    ----------
    X : sparse matrix of shape (n_samples, n_labels)
        Input data. It should be of CSR format.

    axis : {0, 1}, default=None
        The axis on which the data is aggregated.

    sample_weight : array-like of shape (n_samples,), default=None
        Weight for each row of X.

    Returns
    -------
    nnz : int, float, ndarray of shape (n_samples,) or ndarray of shape (n_features,)
        Number of non-zero values in the array along a given axis. Otherwise,
        the total number of non-zero values in the array is returned.
    éÿÿÿÿr   éþÿÿÿr   r.   z#Expected CSR sparse format, got {0}NÚintp)Ú	minlength)rd   r/   zUnsupported axis: {0})r   r   Únnzr(   Údotr*   r+   ÚastypeÚbincountr#   r    r)   r   )r   r   Úsample_weightÚoutr/   s        r   Úcount_nonzerork   ]  sD  € ð, ˆr‚z€zØˆˆØ	�ŠˆØˆˆØ	
Œ�UÒ	Ð	ÝÐ=×DÒDÀQÄXÑNÔNÑOÔOÐOð €|ØÐ Ø”5ˆLå”6�"œ' !¤(Ñ+Ô+¨]Ñ;Ô;Ð;Ø	�ŠˆÝŒg�a”hÑÔˆØÐ à—:’:˜fÑ%Ô%Ð%Ø�]Ñ"Ð"Ø	�ŠˆØÐ Ý”;˜qœy°A´G¸A´JÐ?Ñ?Ô?Ð?å”i ­r¬w°q´xÑ/@Ô/@ÑAÔAˆGÝ”;˜qœy°A´G¸A´JÈÐPÑPÔPÐPåÐ0×7Ò7¸Ñ=Ô=Ñ>Ô>Ð>r   c                 ó>  — t          | ¦  «        |z   }|st          j        S t          j        | dk     ¦  «        }t	          |d¦  «        \  }}|                      ¦   «          |rt          || ||¦  «        S t          |dz
  | ||¦  «        t          || ||¦  «        z   dz  S )z”Compute the median of data with n_zeros additional zeros.

    This function is used to support sparse matrices; it modifies data
    in-place.
    r   rO   r   g       @)Úlenr(   Únanrk   ÚdivmodÚsortÚ_get_elem_at_rank)r!   Ún_zerosÚn_elemsÚ
n_negativeÚmiddleÚis_odds         r   Ú_get_medianrw   “  s®   € õ �$‰iŒi˜'Ñ!€GØð ÝŒvˆÝÔ! $¨¢(Ñ+Ô+€JÝ˜G QÑ'Ô'�N€FˆFØ‡I‚I�K„K€Kàð DÝ  ¨¨z¸7ÑCÔCÐCõ 	˜& 1™* d¨J¸Ñ@Ô@Ý
˜F D¨*°gÑ
>Ô
>ñ	?àñð r   c                 óJ   — | |k     r||          S | |z
  |k     rdS || |z
           S )z@Find the value in data augmented with n_zeros for the given rankr   © )Úrankr!   rt   rr   s       r   rq   rq   ©  s;   € àˆjÒÐØ�DŒzÐØˆjÑ˜7Ò"Ð"ØˆqØ��w‘ÔÐr   c                 óŒ  — t          j        | ¦  «        r| j        dk    st          d| j        z  ¦  «        ‚| j        }| j        \  }}t          j        |¦  «        }t          t          j
        |¦  «        ¦  «        D ]F\  }\  }}t          j        | j        ||…         ¦  «        }||j        z
  }	t          ||	¦  «        ||<   ŒG|S )aC  Find the median across axis 0 of a CSC matrix.

    It is equivalent to doing np.median(X, axis=0).

    Parameters
    ----------
    X : sparse matrix of shape (n_samples, n_features)
        Input data. It should be of CSC format.

    Returns
    -------
    median : ndarray of shape (n_features,)
        Median.
    r1   z%Expected matrix of CSC format, got %s)r   r   r   r   r+   r    r(   ÚzerosÚ	enumerateÚ	itertoolsÚpairwiseÚcopyr!   r=   rw   )
r   r+   Ú	n_samplesÚ
n_featuresÚmedianÚf_indÚstartÚendr!   Únzs
             r   Úcsc_median_axis_0rˆ   ²  sÄ   € õ ŒK˜‰NŒNð L˜qœx¨5Ò0Ð0ÝÐ?À!Ä(ÑJÑKÔKÐKàŒX€FØœGÑ€IˆzÝŒX�jÑ!Ô!€Få(­Ô);¸FÑ)CÔ)CÑDÔDð .ð .Ñˆ‰|��såŒw�q”v˜e C˜iÔ(Ñ)Ô)ˆØ˜œÑ"ˆÝ# D¨"Ñ-Ô-ˆˆu‰ˆà€Mr   c                 óŒ   ‡ ‡‡— ‰ddd…f         Š‰ j         Št          ˆ ˆfd„ˆ ˆfd„ˆˆfd„ˆˆfd„‰ j        ‰ j        ¬¦  «        S )aA  Create an implicitly offset linear operator.

    This is used by PCA on sparse data to avoid densifying the whole data
    matrix.

    Params
    ------
        X : sparse matrix of shape (n_samples, n_features)
        offset : ndarray of shape (n_features,)

    Returns
    -------
    centered : LinearOperator
    Nc                 ó   •— ‰| z  ‰| z  z
  S ©Nry   ©Úxr   Úoffsets    €€r   ú<lambda>z)_implicit_column_offset.<locals>.<lambda>ã  ó   ø€ ˜˜Q™ ¨!¡Ñ+€ r   c                 ó   •— ‰| z  ‰| z  z
  S r‹   ry   rŒ   s    €€r   r�   z)_implicit_column_offset.<locals>.<lambda>ä  r�   r   c                 ó>   •— ‰| z  ‰|                       ¦   «         z  z
  S r‹   )Úsum©r�   ÚXTrŽ   s    €€r   r�   z)_implicit_column_offset.<locals>.<lambda>å  s   ø€ ˜"˜q™& F¨Q¯UªU©W¬WÑ$4Ñ5€ r   c                 ó`   •— ‰| z  ‰j         |                      d¬¦  «        d d d …f         z  z
  S )Nr   r   )r4   r“   r”   s    €€r   r�   z)_implicit_column_offset.<locals>.<lambda>æ  s0   ø€ ˜"˜q™& 6¤8¨a¯eªe¸¨e©m¬m¸DÀ!À!À!¸GÔ.DÑ#DÑD€ r   )ÚmatvecÚmatmatÚrmatvecÚrmatmatr9   r    )r4   r   r9   r    )r   rŽ   r•   s   ``@r   Ú_implicit_column_offsetr›   Ñ  s|   øøø€ ð �D˜!˜!˜!�GŒ_€FØ	
Œ€BÝØ+Ð+Ð+Ð+Ð+Ø+Ð+Ð+Ð+Ð+Ø5Ð5Ð5Ð5Ð5ØDÐDÐDÐDÐDØŒgØŒgðñ ô ð r   c                 óf  — t          j        | ¦  «        r| j        dv r| j        dk    st	          d¦  «        ‚t          j        |¦  «        r|j        dv r|j        dk    st	          d¦  «        ‚| j        d         |j        d         k    r.d| j        d         › d|j        d         › d	�}t	          |¦  «        ‚| j        \  }}|j        d         }| j        |j        k    s| j        t          j        t          j	        fvrd
}t	          |¦  «        ‚|€#t          j
        ||f| j        j        ¬¦  «        }nU|j        d         |k    s|j        d         |k    rt	          d¦  «        ‚|j        | j        j        k    rt	          d¦  «        ‚d}| j        dk    r<|j        dk    rd}|j        | j        |j        }}} ||}}n4|                      ¦   «         } n|j        dk    r|                     ¦   «         }t          | j        | j        | j        |j        |j        |j        ||||¦
  «
         |r|j        }|S )aÜ  Compute A @ B for sparse and 2-dim A and B while returning an ndarray.

    Parameters
    ----------
    A : sparse matrix of shape (n1, n2) and format CSC or CSR
        Left-side input matrix.
    B : sparse matrix of shape (n2, n3) and format CSC or CSR
        Right-side input matrix.
    out : ndarray of shape (n1, n3) or None
        Optional ndarray into which the result is written.

    Returns
    -------
    out
        An ndarray, new created if out=None.
    r7   rO   z2Input 'A' must be a sparse 2-dim CSC or CSR array.z2Input 'B' must be a sparse 2-dim CSC or CSR array.r   r   z1Shapes must fulfil A.shape[1] == B.shape[0], got z == ú.zBDtype of A and B must be the same, either both float32 or float64.Nr8   z3Shape of out must be ({n1}, {n3}), got {out.shape}.z(Dtype of out must match that of input A.Fr1   T)r   r   r   Úndimr   r    r9   r(   Úfloat32Úfloat64Úemptyr!   r4   Útocsrr   r#   r+   )ÚAÚBrj   ÚmsgÚn1Ún2Ún3Útranspose_outs           r   Úsparse_matmul_to_denserª   ì  s>  € õ" ŒK˜‰NŒNð O˜qœx¨>Ð9Ð9¸a¼fÈºk¸kÝÐMÑNÔNÐNÝŒK˜‰NŒNð O˜qœx¨>Ð9Ð9¸a¼fÈºk¸kÝÐMÑNÔNÐNØ„wˆq„z�Q”W˜Q”ZÒÐð1Ø”7˜1”:ð1ð 1Ø#$¤7¨1¤:ð1ð 1ð 1ð 	õ ˜‰oŒoÐØŒW�F€BˆØ	
Œ�Œ€BØ„w�!”'ÒÐ˜QœW­R¬Z½¼Ð,DÐDÐDØRˆÝ˜‰oŒoÐØ
€{ÝŒh˜˜B�x q¤v¤|Ð4Ñ4Ô4ˆˆàŒ9�QŒ<˜2ÒÐ ¤¨1¤°Ò!3Ð!3ÝÐRÑSÔSÐSØŒ9˜œœÒ$Ð$ÝÐGÑHÔHÐHà€MØ„x�5ÒÐØŒ8�uÒÐà ˆMØœ˜QœS #¤%�#ˆqˆAØ˜�ˆBˆBð —’‘	”	ˆAˆAØ	
Œ�UÒ	Ð	à�GŠG‰IŒIˆåØ	Œ�”	˜1œ8 Q¤V¨Q¬Y¸¼À#ÀrÈ2Èrñô ð ð ð ØŒeˆà€Jr   )NF)F)NNr‹   ))Ú__doc__r~   Únumpyr(   Úscipy.sparseÚsparser   Úscipy.sparse.linalgr   Úsklearn.utils.fixesr   r   Úsklearn.utils.sparsefuncs_fastr   r3   r   r   r2   r	   r?   Úsklearn.utils.validationr
   r   r   r&   r,   r5   r@   rB   rD   rM   rW   rZ   r\   r_   rk   rw   rq   rˆ   r›   rª   ry   r   r   ú<module>r³      sL  ðØ HÐ Hð
 Ð Ð Ð à Ð Ð Ð Ø Ð Ð Ð Ð Ð Ø .Ð .Ð .Ð .Ð .Ð .à DÐ DÐ DÐ DÐ DÐ DÐ DÐ Dðð ð ð ð ð ðð ð ð ð ð ðð ð ð ð ð ðð ð ð ð ð ð :Ð 9Ð 9Ð 9Ð 9Ð 9ðð ð ð
ð 
ð 
ð&1ð &1ð &1ðR2ð 2ð 2ð$Kð Kð Kð Kð\ NRð pð pð pð pð pðf*ð *ð *ðZ)ð )ð )ðXð ð ð:<ð <ð <ð~*ð *ð *ðZ.ð .ð .ðb!ð !ð !ð !ðH3?ð 3?ð 3?ð 3?ðlð ð ð, ð  ð  ðð ð ð>ð ð ð6<ð <ð <ð <ð <ð <r   