ó
‡ˆ\c           @   s
  d  d l  j Z d  d l Z d d l m Z m Z	 m
 Z 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 „  Z d „  Z d „  Z d „  Z d „  Z e d „ Z d d d „ Z  d „  Z! d „  Z" d „  Z# d S(   iÿÿÿÿNi   (   t   csr_mean_variance_axis0t   csc_mean_variance_axis0t   incr_mean_variance_axis0c         C   s>   t  j |  ƒ r |  j n	 t |  ƒ } d | } t | ƒ ‚ d S(   s2   Raises a TypeError if X is not a CSR or CSC matrixs,   Expected a CSR or CSC sparse matrix, got %s.N(   t   spt   issparset   formatt   typet	   TypeError(   t   Xt
   input_typet   err(    (    s8   lib/python2.7/site-packages/sklearn/utils/sparsefuncs.pyt   _raise_typeerror   s    $
c         C   s#   |  d k r t  d |  ƒ ‚ n  d  S(   Ni    i   s8   Unknown axis value: %d. Use 0 for rows, or 1 for columns(   i    i   (   t
   ValueError(   t   axis(    (    s8   lib/python2.7/site-packages/sklearn/utils/sparsefuncs.pyt   _raise_error_wrong_axis   s    c         C   sE   | j  d |  j  d k s  t ‚ |  j | j |  j d d ƒ9_ d S(   sË  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 : CSR matrix with shape (n_samples, n_features)
        Matrix to normalize using the variance of the features.

    scale : float array with shape (n_features,)
        Array of precomputed feature-wise values to use for scaling.
    i    i   t   modet   clipN(   t   shapet   AssertionErrort   datat   taket   indices(   R   t   scale(    (    s8   lib/python2.7/site-packages/sklearn/utils/sparsefuncs.pyt   inplace_csr_column_scale   s     c         C   sK   | j  d |  j  d k s  t ‚ |  j t j | t j |  j ƒ ƒ 9_ d S(   s¦   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 : CSR sparse matrix, shape (n_samples, n_features)
        Matrix to be scaled.

    scale : float array with shape (n_samples,)
        Array of precomputed sample-wise values to use for scaling.
    i    N(   R   R   R   t   npt   repeatt   difft   indptr(   R   R   (    (    s8   lib/python2.7/site-packages/sklearn/utils/sparsefuncs.pyt   inplace_csr_row_scale.   s     c         C   sˆ   t  | ƒ t |  t j ƒ rB | d k r2 t |  ƒ St |  j ƒ SnB t |  t j ƒ rz | d k rj t |  ƒ St |  j ƒ Sn
 t |  ƒ d S(   sÈ  Compute mean and variance along an axix on a CSR or CSC matrix

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

    axis : int (either 0 or 1)
        Axis along which the axis should be computed.

    Returns
    -------

    means : float array with shape (n_features,)
        Feature-wise means

    variances : float array with shape (n_features,)
        Feature-wise variances

    i    N(	   R   t
   isinstanceR   t
   csr_matrixt   _csr_mean_var_axis0t   _csc_mean_var_axis0t   Tt
   csc_matrixR   (   R   R   (    (    s8   lib/python2.7/site-packages/sklearn/utils/sparsefuncs.pyt   mean_variance_axis@   s    


c         C   sÐ   t  | ƒ t |  t j ƒ rf | d k rD t |  d | d | d | ƒSt |  j d | d | d | ƒSnf t |  t j ƒ rÂ | d k r  t |  d | d | d | ƒSt |  j d | d | d | ƒSn
 t |  ƒ d S(   s¸  Compute incremental mean and variance along an axix 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, shape (n_samples, n_features)
        Input data.

    axis : int (either 0 or 1)
        Axis along which the axis should be computed.

    last_mean : float array with shape (n_features,)
        Array of feature-wise means to update with the new data X.

    last_var : float array with shape (n_features,)
        Array of feature-wise var to update with the new data X.

    last_n : int with shape (n_features,)
        Number of samples seen so far, excluded X.

    Returns
    -------

    means : float array with shape (n_features,)
        Updated feature-wise means.

    variances : float array with shape (n_features,)
        Updated feature-wise variances.

    n : int with shape (n_features,)
        Updated number of seen samples.

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

    i    t	   last_meant   last_vart   last_nN(   R   R   R   R   t   _incr_mean_var_axis0R!   R"   R   (   R   R   R$   R%   R&   (    (    s8   lib/python2.7/site-packages/sklearn/utils/sparsefuncs.pyt   incr_mean_variance_axise   s    +
c         C   sU   t  |  t j ƒ r% t |  j | ƒ n, t  |  t j ƒ rG t |  | ƒ n
 t |  ƒ d S(   sÖ  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 : CSC or CSR matrix with shape (n_samples, n_features)
        Matrix to normalize using the variance of the features.

    scale : float array with shape (n_features,)
        Array of precomputed feature-wise values to use for scaling.
    N(   R   R   R"   R   R!   R   R   R   (   R   R   (    (    s8   lib/python2.7/site-packages/sklearn/utils/sparsefuncs.pyt   inplace_column_scale¤   s
    c         C   sU   t  |  t j ƒ r% t |  j | ƒ n, t  |  t j ƒ rG t |  | ƒ n
 t |  ƒ d S(   s²   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 : CSR or CSC sparse matrix, shape (n_samples, n_features)
        Matrix to be scaled.

    scale : float array with shape (n_features,)
        Array of precomputed sample-wise values to use for scaling.
    N(   R   R   R"   R   R!   R   R   R   (   R   R   (    (    s8   lib/python2.7/site-packages/sklearn/utils/sparsefuncs.pyt   inplace_row_scaleº   s
    c         C   s®   x5 | | g D]' } t  | t j ƒ r t d ƒ ‚ q q W| d k  rX | |  j d 7} n  | d k  rx | |  j d 7} n  |  j | k } | |  j |  j | k <| |  j | <d S(   s5  
    Swaps two rows of a CSC matrix in-place.

    Parameters
    ----------
    X : scipy.sparse.csc_matrix, shape=(n_samples, n_features)
        Matrix whose two rows are to be swapped.

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

    n : int
        Index of the row of X to be swapped.
    s    m and n should be valid integersi    N(   R   R   t   ndarrayR   R   R   (   R   t   mt   nt   tt   m_mask(    (    s8   lib/python2.7/site-packages/sklearn/utils/sparsefuncs.pyt   inplace_swap_row_cscÐ   s    c         C   sÒ  x5 | | g D]' } t  | t j ƒ r t d ƒ ‚ q q W| d k  rX | |  j d 7} n  | d k  rx | |  j d 7} n  | | k r” | | } } n  |  j } | | } | | d } | | } | | d } | | }	 | | }
 |	 |
 k r4|  j | d | c !|
 |	 7+| |
 |  j | d <| |	 |  j | <n  t j |  j |  |  j | | !|  j | | !|  j | | !|  j | g ƒ |  _ t j |  j |  |  j | | !|  j | | !|  j | | !|  j | g ƒ |  _ d S(   s5  
    Swaps two rows of a CSR matrix in-place.

    Parameters
    ----------
    X : scipy.sparse.csr_matrix, shape=(n_samples, n_features)
        Matrix whose two rows are to be swapped.

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

    n : int
        Index of the row of X to be swapped.
    s    m and n should be valid integersi    i   i   N(	   R   R   R+   R   R   R   t   concatenateR   R   (   R   R,   R-   R.   R   t   m_startt   m_stopt   n_startt   n_stopt   nz_mt   nz_n(    (    s8   lib/python2.7/site-packages/sklearn/utils/sparsefuncs.pyt   inplace_swap_row_csrí   s<    	



c         C   sX   t  |  t j ƒ r% t |  | | ƒ n/ t  |  t j ƒ rJ t |  | | ƒ n
 t |  ƒ d S(   s:  
    Swaps two rows of a CSC/CSR matrix in-place.

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

    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   R"   R0   R   R8   R   (   R   R,   R-   (    (    s8   lib/python2.7/site-packages/sklearn/utils/sparsefuncs.pyt   inplace_swap_row$  s
    c         C   s˜   | d k  r  | |  j  d 7} n  | d k  r@ | |  j  d 7} n  t |  t j ƒ re t |  | | ƒ n/ t |  t j ƒ rŠ t |  | | ƒ n
 t |  ƒ d S(   sF  
    Swaps two columns of a CSC/CSR matrix in-place.

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

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

    n : int
        Index of the column of X to be swapped.
    i    i   N(   R   R   R   R"   R8   R   R0   R   (   R   R,   R-   (    (    s8   lib/python2.7/site-packages/sklearn/utils/sparsefuncs.pyt   inplace_swap_column;  s    c         C   sA   t  j t  j |  j ƒ ƒ } | j |  j |  j | ƒ } | | f S(   N(   R   t   flatnonzeroR   R   t   reduceatR   (   R   t   ufunct   major_indext   value(    (    s8   lib/python2.7/site-packages/sklearn/utils/sparsefuncs.pyt   _minor_reduceV  s    c         C   sy  |  j  | } | d k r( t d ƒ ‚ n  |  j  d | } | d k rQ |  j ƒ  n	 |  j ƒ  } | j ƒ  t | | ƒ \ } } t j | j ƒ | | k  } | | | d ƒ | | <| d k }	 t j	 |	 | ƒ } t j	 |	 | ƒ } | d k r-t
 j | t j t | ƒ ƒ | f f d |  j d d | f ƒ}
 n? t
 j | | t j t | ƒ ƒ f f d |  j d | d f ƒ}
 |
 j j ƒ  S(   Ni    s&   zero-size array to reduction operationi   t   dtypeR   (   R   R   t   tocsct   tocsrt   sum_duplicatesR@   R   R   R   t   compressR   t
   coo_matrixt   zerost   lenRA   t   At   ravel(   R   R   t
   min_or_maxt   Nt   Mt   matR>   R?   t   not_fullt   maskt   res(    (    s8   lib/python2.7/site-packages/sklearn/utils/sparsefuncs.pyt   _min_or_max_axis\  s$    $
''c         C   sé   | d  k r˜ d |  j k r* t d ƒ ‚ n  |  j j d ƒ } |  j d k rO | S| j |  j j ƒ  ƒ } |  j t	 j
 |  j ƒ k r” | | | ƒ } n  | S| d k  r± | d 7} n  | d k sÉ | d k rÙ t |  | | ƒ St d ƒ ‚ d  S(   Ni    s&   zero-size array to reduction operationi   i   s.   invalid axis, use 0 for rows, or 1 for columns(   t   NoneR   R   RA   R   t   nnzt   reduceR   RJ   R   t   productRR   (   R   R   RK   t   zeroR,   (    (    s8   lib/python2.7/site-packages/sklearn/utils/sparsefuncs.pyt   _sparse_min_or_maxs  s    c         C   s(   t  |  | t j ƒ t  |  | t j ƒ f S(   N(   RX   R   t   minimumt   maximum(   R   R   (    (    s8   lib/python2.7/site-packages/sklearn/utils/sparsefuncs.pyt   _sparse_min_max†  s    c         C   s(   t  |  | t j ƒ t  |  | t j ƒ f S(   N(   RX   R   t   fmint   fmax(   R   R   (    (    s8   lib/python2.7/site-packages/sklearn/utils/sparsefuncs.pyt   _sparse_nan_min_max‹  s    c         C   s[   t  |  t j ƒ s$ t  |  t j ƒ rM | r: t |  d | ƒSt |  d | ƒSn
 t |  ƒ d S(   s^  Compute minimum and maximum along an axis on a CSR or CSC matrix and
    optionally ignore NaN values.

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

    axis : int (either 0 or 1)
        Axis along which the axis should be computed.

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

        .. versionadded:: 0.20

    Returns
    -------

    mins : float array with shape (n_features,)
        Feature-wise minima

    maxs : float array with shape (n_features,)
        Feature-wise maxima
    R   N(   R   R   R   R"   R^   R[   R   (   R   R   t
   ignore_nan(    (    s8   lib/python2.7/site-packages/sklearn/utils/sparsefuncs.pyt   min_max_axis  s
    $c         C   sZ  | d k r d } n? | d k r* d } n* |  j  d k rT t d j  |  j  ƒ ƒ ‚ n  | d
 k r’ | d
 k rs |  j St j t j |  j ƒ | ƒ SnÄ | d k rÈ t j |  j ƒ } | d
 k rÀ | S| | S| d k rA| d
 k rý t j |  j	 d |  j
 d ƒSt j | t j |  j ƒ ƒ } t j |  j	 d |  j
 d d | ƒSn t d	 j  | ƒ ƒ ‚ d
 S(   s‘  A variant of X.getnnz() with extension to weighting on axis 0

    Useful in efficiently calculating multilabel metrics.

    Parameters
    ----------
    X : CSR sparse matrix, shape = (n_samples, n_labels)
        Input data.

    axis : None, 0 or 1
        The axis on which the data is aggregated.

    sample_weight : array, shape = (n_samples,), optional
        Weight for each row of X.
    iÿÿÿÿi   iþÿÿÿi    t   csrs#   Expected CSR sparse format, got {0}t	   minlengtht   weightss   Unsupported axis: {0}N(   R   R   RS   RT   R   t   dotR   R   t   bincountR   R   R   R   (   R   R   t   sample_weightt   outRc   (    (    s8   lib/python2.7/site-packages/sklearn/utils/sparsefuncs.pyt   count_nonzero³  s,    		
c         C   s˜   t  |  ƒ | } | s t j St j |  d k  ƒ } t | d ƒ \ } } |  j ƒ  | rj t | |  | | ƒ St | d |  | | ƒ t | |  | | ƒ d S(   s   Compute the median of data with n_zeros additional zeros.

    This function is used to support sparse matrices; it modifies data in-place
    i    i   i   g       @(   RH   R   t   nanRh   t   divmodt   sortt   _get_elem_at_rank(   R   t   n_zerost   n_elemst
   n_negativet   middlet   is_odd(    (    s8   lib/python2.7/site-packages/sklearn/utils/sparsefuncs.pyt   _get_medianã  s    
c         C   s4   |  | k  r | |  S|  | | k  r( d S| |  | S(   s@   Find the value in data augmented with n_zeros for the given ranki    (    (   t   rankR   Ro   Rm   (    (    s8   lib/python2.7/site-packages/sklearn/utils/sparsefuncs.pyRl   ö  s
    c   
      C   sÀ   t  |  t j ƒ s( t d |  j ƒ ‚ n  |  j } |  j \ } } t j | ƒ } xj t	 t
 | d  | d ƒ ƒ D]K \ } \ } } t j |  j | | !ƒ } | | j }	 t | |	 ƒ | | <qm W| S(   s'  Find the median across axis 0 of a CSC matrix.
    It is equivalent to doing np.median(X, axis=0).

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

    Returns
    -------
    median : ndarray, shape (n_features,)
        Median.

    s%   Expected matrix of CSC format, got %siÿÿÿÿi   (   R   R   R"   R   R   R   R   R   RG   t	   enumeratet   zipt   copyR   t   sizeRr   (
   R   R   t	   n_samplest
   n_featurest   mediant   f_indt   startt   endR   t   nz(    (    s8   lib/python2.7/site-packages/sklearn/utils/sparsefuncs.pyt   csc_median_axis_0ÿ  s    	0($   t   scipy.sparset   sparseR   t   numpyR   t   sparsefuncs_fastR    R   R   R    R   R'   R   R   R   R   R#   R(   R)   R*   R0   R8   R9   R:   R@   RR   RX   R[   R^   t   FalseR`   RS   Rh   Rr   Rl   R   (    (    (    s8   lib/python2.7/site-packages/sklearn/utils/sparsefuncs.pyt   <module>   s0   					%	?				7							#0			