
     hK                         d dl mZ d dlZddlmZmZ	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 Zd Zd Zd Zd ZddZd dZ d Z!d Z"d Z#dS )!    N   )csr_mean_variance_axis0csc_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      U/var/www/html/Sam_Eipo/venv/lib/python3.11/site-packages/sklearn/utils/sparsefuncs.py_raise_typeerrorr      s6    [^^8aJ
8:
EC
C..    c                 2    | dvrt          d| z            d S )N)r   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.
    r   r   clip)modeN)shapedatatakeindicesr   scales     r   inplace_csr_column_scaler#      sG     ;q>QWQZ''''FFejjj000FFFFr   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)   2   sM     ;q>QWQZ''''FFbirwqx00111FFFFr   Fc                 f   t          |           t          | t          j                  r/|dk    rt	          | ||          S t          | j        ||          S t          | t          j                  r/|dk    rt          | ||          S t	          | j        ||          S t          |            dS )a4  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`.
    r   )weightsreturn_sum_weightsN)	r   
isinstancer
   
csr_matrix_csr_mean_var_axis0_csc_mean_var_axis0T
csc_matrixr   )r   r   r+   r,   s       r   mean_variance_axisr3   D   s    H D!!!!R]## 199&77I    'W9K    
Ar}	%	% 
199&77I    'W9K    	r   )r+   c                   t          |           t          | t          j        t          j        f          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 )a7	  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.
    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.   r2   r   r%   sizefullr   r5   r   r1   r   _incr_mean_var_axis0)r   r   r6   r7   r8   r+   s         r   incr_mean_variance_axisr<      s   x D!!!a"-788 	wv!&	HHHGI"'("3"3FFFFrwvFFFFSTTTqyy79++K"#'!*K K46GI4F4FK K K   , 79++L#$71:L L57WY5G5GL L L  
 qyyaA&wAAA	Y&'   r   c                     t          | t          j                  rt          | j        |           dS t          | t          j                  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.
    N)r-   r
   r2   r)   r1   r.   r#   r   r!   s     r   inplace_column_scaler>      sn     !R]## ac5)))))	Ar}	%	%  E*****r   c                     t          | t          j                  rt          | j        |           dS t          | t          j                  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.
    N)r-   r
   r2   r#   r1   r.   r)   r   r!   s     r   inplace_row_scaler@      sn     !R]##  e,,,,,	Ar}	%	% 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)r-   r%   ndarrayr   r   r    )r   mntm_masks        r   inplace_swap_row_cscrH     s     V @ @a$$ 	@>???	@ 	1uu	QWQZ1uu	QWQZY!^F !AIai1nAIf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.
    rB   r   r   r   N)	r-   r%   rC   r   r   r(   concatenater    r   )r   rD   rE   rF   r(   m_startm_stopn_startn_stopnz_mnz_ns              r   inplace_swap_row_csrrQ   (  s    V @ @a$$ 	@>???	@ 	1uu	QWQZ1uu	QWQZ 	1uu!1XFQiGAE]FQiGAE]FGDGDt||	Qtd{*!D.QtmIhwhIgfn%IfWn%Igfn%Ifgg	
 AI ^F8G8F76>"F6'>"F76>"F677O	
 AFFFr   c                     t          | t          j                  rt          | ||           dS t          | t          j                  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.
    N)r-   r
   r2   rH   r.   rQ   r   r   rD   rE   s      r   inplace_swap_rowrT   g  sp      !R]## Q1%%%%%	Ar}	%	% Q1%%%%%r   c                 0   |dk     r|| j         d         z  }|dk     r|| j         d         z  }t          | t          j                  rt	          | ||           dS t          | t          j                  rt          | ||           dS t          |            dS )ag  
    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.
    r   r   N)r   r-   r
   r2   rQ   r.   rH   r   rS   s      r   inplace_swap_columnrV     s      	1uu	QWQZ1uu	QWQZ!R]## Q1%%%%%	Ar}	%	% Q1%%%%%r   c                    t          j        t          j        | j                            } t	          |           | j        | j        | j        f| j                  } |                    | j        | j        |                   }||fS )N)r   )	r%   flatnonzeror'   r(   r   r   r    r   reduceat)r   ufuncmajor_indexvalues       r   _minor_reducer]     so    .!2!233K
 	QAH-QW===ANN1618K#899Er   c           	      $   | j         |         }|dk    rt          d          | j         d|z
           }|dk    r|                                 n|                                 }|                                 t          ||          \  }}t          j        |j                  |         |k     } |||         d          ||<   |dk    }	t          j	        |	|          }t          j	        |	|          }|dk    rBt          j        |t          j        t          |                    |ff| j        d|f          }
nAt          j        ||t          j        t          |                    ff| j        |df          }
|
j                                        S )Nr   &zero-size array to reduction operationr   )r5   r   )r   r   tocsctocsrsum_duplicatesr]   r%   r'   r(   compressr
   
coo_matrixzeroslenr5   Aravel)r   r   
min_or_maxNMmatr[   r\   not_fullmaskress              r   _min_or_max_axisrp     sx   	AAvvABBB	DAqyy!'')))aggiiC&sJ77Kwsz"";/!3H jx!44E(OA:D+dK00KKe$$EqyymRXc%jj));78PQSTv
 
 
 m["(3u::"6"678PQSTv
 
 
 5;;==r   c                    |d| j         v rt          d          | j                            d          }| j        dk    r|S |                    | j                                                  }| j        t          j	        | j                   k    r |||          }|S |dk     r|dz  }|dk    s|dk    rt          | ||          S t          d          )Nr   r_   r   r   z.invalid axis, use 0 for rows, or 1 for columns)r   r   r5   r   nnzreducer   rh   r%   productrp   )r   r   ri   zerorD   s        r   _sparse_min_or_maxrv     s    |<<EFFFw||A5A::Kafllnn--5BJqw''''
4##Aaxx			tqyy4444IJJJr   c                 n    t          | |t          j                  t          | |t          j                  fS N)rv   r%   minimummaximumr   r   s     r   _sparse_min_maxr|     s.    1dBJ//1dBJ// r   c                 n    t          | |t          j                  t          | |t          j                  fS rx   )rv   r%   fminfmaxr{   s     r   _sparse_nan_min_maxr     s*    q$002DQbg2V2VWWr   c                     t          | t          j        t          j        f          r$|rt	          | |          S t          | |          S t          |            dS )a  Compute minimium 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   N)r-   r
   r.   r2   r   r|   r   )r   r   
ignore_nans      r   min_max_axisr     sa    6 !bmR]344  	1&qt4444"14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   csrz#Expected CSR sparse format, got {0}Nintp)	minlength)r   r+   zUnsupported axis: {0})r   r   rr   r%   dotr'   r(   astypebincountr    r   r&   r   )r   r   sample_weightoutr+   s        r   count_nonzeror     sD   , rzz		
U		=DDQXNNOOO | 5L6"'!(++];;;	gah ::f%%%]""	 ;qyAGAJ????irwqx/@/@AAG;qyAGAJ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 )zCompute the median of data with n_zeros additional zeros.

    This function is used to support sparse matrices; it modifies data
    in-place.
    r   r   r   g       @)rf   r%   nanr   divmodsort_get_elem_at_rank)r   n_zerosn_elems
n_negativemiddleis_odds         r   _get_medianr   7  s     $ii'!G v!$(++JGQ''NFFIIKKK D z7CCC 	&1*dJ@@
FD*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   r   r   s       r   r   r   M  s;    jDzj7""qwr   c                    t          | t          j                  st          d| j        z            | j        }| j        \  }}t          j        |          }t          t          |dd         |dd                             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.
    z%Expected matrix of CSC format, got %sNr   r   )r-   r
   r2   r   r   r(   r   r%   re   	enumeratezipcopyr   r9   r   )
r   r(   	n_samples
n_featuresmedianf_indstartendr   nzs
             r   csc_median_axis_0r   V  s     a'' L?!(JKKKXFGIzXj!!F(VCRC[&*)E)EFF . .|s wqveCi())"#D"--uMr   )NF)F)NN)$scipy.sparsesparser
   numpyr%   sparsefuncs_fastr   r/   r   r0   r   r;   utils.validationr   r   r   r#   r)   r3   r<   r>   r@   rH   rQ   rT   rV   r]   rp   rv   r|   r   r   r   r   r   r   r   r   r   <module>r      s                     
 4 3 3 3 3 3  
 
 
1 1 1&2 2 2$9 9 9 9x NR [ [ [ [ [|  .  ,  :< < <~  0  8    2K K K&  X X X! ! ! !H3? 3? 3? 3?l  ,         r   