� K�g@X����dZddlZddlmZddlmZddlm Z m Z ddl m Z ddl mZdd l mZdd l mZd �Zd �Zd �Zd�Zdd�Zdd�d�Zd�Zd�Zd�Zd�Zd�Zd�Zd d�Z d!d�Z!d�Z"d�Z#d�Z$d�Z%dS)"zBA collection of utilities to work with sparse matrices and arrays.�N)�LinearOperator�)�_sparse_min_max�_sparse_nan_min_max)�_check_sample_weight�)�csc_mean_variance_axis0)�csr_mean_variance_axis0)�incr_mean_variance_axis0c�~�tj|��r|jnt|��}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 �i/home/asafur/pinokio/api/open-webui.git/app/env/lib/python3.11/site-packages/sklearn/utils/sparsefuncs.py�_raise_typeerrorrs6���[��^�^�8�����a���J� 8�:� E�C� �C�.�.��c�2�|dvrtd|z���dS)N)rrz8Unknown axis value: %d. Use 0 for rows, or 1 for columns)� ValueError��axiss r�_raise_error_wrong_axisrs/�� �6���� F�� M� � � ��rc��|jd|jdksJ�|xj|�|jd���zc_dS)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_matrix((data, indices, indptr)) >>> csr.todense() matrix([[8, 1, 2], [0, 0, 5], [0, 0, 0], [0, 0, 0]]) >>> sparsefuncs.inplace_csr_column_scale(csr, scale) >>> csr.todense() matrix([[16, 3, 4], [ 0, 0, 10], [ 0, 0, 0], [ 0, 0, 0]]) rr�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�Frc��|jd|jdksJ�|xjtj|tj|j����zc_dS)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. rN)r r!�np�repeat�diff�indptrr$s r�inplace_csr_row_scaler,NsM�� �;�q�>�Q�W�Q�Z� '� '� '� '��F�F�b�i��r�w�q�x�0�0�1�1�1�F�F�F�FrFc�z�t|��tj|��r:|jdkr/|dkrt |||���St |j||���Stj|��r:|jdkr/|dkrt |||���St |j||���St|��dS)a{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_matrix((data, indices, indptr)) >>> csr.todense() matrix([[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)rr rr�_csr_mean_var_axis0�_csc_mean_var_axis0�Tr)rrr/r0s r�mean_variance_axisr5`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|��tj|��r |jdvst |��t j|��dkr!t j|j||j ���}t j|��t j|��cxkrt j|��ksntd���|dkrWt j|��|jdkr3td|jd�dt j|���d����nVt j|��|jdkr3td |jd�dt j|���d����|dkr|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_matrix((data, indices, indptr)) >>> csr.todense() matrix([[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.3..., 0.1..., 1.1...]), array([8.8..., 0.1..., 3.4...]), array([6., 6., 6.])) )r1r.r)�dtypez8last_mean, last_var, last_n do not have the same shapes.rzHIf 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/)rr rrrr(�size�fullr r7rr4r�_incr_mean_var_axis0)rrr8r9r: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��&�'� � � �rc���tj|��r"|jdkrt|j|��dStj|��r|jdkrt ||��dSt |��dS)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_matrix((data, indices, indptr)) >>> csr.todense() matrix([[8, 1, 2], [0, 0, 5], [0, 0, 0], [0, 0, 0]]) >>> sparsefuncs.inplace_column_scale(csr, scale) >>> csr.todense() matrix([[16, 3, 4], [ 0, 0, 10], [ 0, 0, 0], [ 0, 0, 0]]) r1r.N)r rrr,r4r&rr$s r�inplace_column_scaler@!s��J �{�1�~�~��!�(�e�+�+��a�c�5�)�)�)�)�)� ��Q����A�H��-�-� ��E�*�*�*�*�*�������rc���tj|��r"|jdkrt|j|��dStj|��r|jdkrt ||��dSt |��dS)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_matrix((data, indices, indptr)) >>> csr.todense() matrix([[8, 1, 0, 0], [0, 0, 2, 0], [0, 0, 0, 5], [0, 0, 0, 6]]) >>> sparsefuncs.inplace_row_scale(csr, scale) >>> csr.todense() matrix([[16, 2, 0, 0], [ 0, 0, 6, 0], [ 0, 0, 0, 20], [ 0, 0, 0, 30]]) r1r.N)r rrr&r4r,rr$s r�inplace_row_scalerBNs��H �{�1�~�~��!�(�e�+�+� ���e�,�,�,�,�,� ��Q����A�H��-�-��a��'�'�'�'�'�������rc��||fD]+}t|tj��rtd����,|dkr||jdz }|dkr||jdz }|j|k}||j|j|k<||j|<dS)aKSwap 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 integersrN)� isinstancer(�ndarrayrr r#)r�m�n�t�m_masks r�inplace_swap_row_cscrKzs�����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���rc �<�||fD]+}t|tj��rtd����,|dkr||jdz }|dkr||jdz }||kr||}}|j}||}||dz}||}||dz}||z } ||z } | | kr:|j|dz|�xx| | z z cc<|| z|j|dz<|| z |j|<tj|jd|�|j||�|j||�|j||�|j|d�g��|_tj|jd|�|j||�|j||�|j||�|j|d�g��|_dS)aKSwap 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. rDrrrN) rEr(rFrr r+� concatenater#r!) rrGrHrIr+�m_start�m_stop�n_start�n_stop�nz_m�nz_ns r�inplace_swap_row_csrrT�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�Frc���tj|��r|jdkrt|||��dStj|��r|jdkrt |||��dSt |��dS)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_matrix((data, indices, indptr)) >>> csr.todense() matrix([[8, 0, 2], [0, 0, 5], [0, 0, 0], [0, 0, 0]]) >>> sparsefuncs.inplace_swap_row(csr, 0, 1) >>> csr.todense() matrix([[0, 0, 5], [8, 0, 2], [0, 0, 0], [0, 0, 0]]) r1r.N)r rrrKrTr�rrGrHs r�inplace_swap_rowrW�s���J �{�1�~�~��!�(�e�+�+��Q��1�%�%�%�%�%� ��Q����A�H��-�-��Q��1�%�%�%�%�%�������rc�D�|dkr||jdz }|dkr||jdz }tj|��r|jdkrt |||��dStj|��r|jdkrt |||��dSt |��dS)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_matrix((data, indices, indptr)) >>> csr.todense() matrix([[8, 0, 2], [0, 0, 5], [0, 0, 0], [0, 0, 0]]) >>> sparsefuncs.inplace_swap_column(csr, 0, 1) >>> csr.todense() matrix([[0, 8, 2], [0, 0, 5], [0, 0, 0], [0, 0, 0]]) rrr1r.N)r r rrrTrKrrVs r�inplace_swap_columnrYs���J �1�u�u� �Q�W�Q�Z����1�u�u� �Q�W�Q�Z��� �{�1�~�~��!�(�e�+�+��Q��1�%�%�%�%�%� ��Q����A�H��-�-��Q��1�%�%�%�%�%�������rc��tj|��r-|jdvr$|rt||���St ||���St |��dS)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.r1rN)r rrrrr)rr� ignore_nans r� min_max_axisr\4sb��6 �{�1�~�~��!�(�n�4�4� � 1�&�q�t�4�4�4� 4�"�1�4�0�0�0� 0�������rc��|dkrd}n;|dkrd}n2|jdkr'td�|j�����|�5|�|jStjtj|j��|��S|dkr5tj|j��}|�|�d��S||zS|dkr{|�&tj|j |j d� ��Stj |tj|j����}tj|j |j d|� ��Std �|�����) 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�����rr.z#Expected CSR sparse format, got {0}N�intp)� minlength)rar/zUnsupported axis: {0}) rr�nnzr(�dotr*r+�astype�bincountr#r r)r)rr� sample_weight�outr/s r� count_nonzerorhXsD��, �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��=�=�>�>�>rc�>�t|��|z}|s tjStj|dk��}t |d��\}}|���|rt ||||��St |dz |||��t ||||��zdz 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. rrrg@)�lenr(�nanrh�divmod�sort�_get_elem_at_rank)r!�n_zeros�n_elems� n_negative�middle�is_odds r� _get_medianrt�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� >� >� ?� � � rc�J�||kr||S||z |krdS|||z S)z@Find the value in data augmented with n_zeros for the given rankr�)�rankr!rqros rrnrn�s;�� �j����D�z�� �j��7�"�"��q� ��w�� �rc��tj|��r |jdkstd|jz���|j}|j\}}t j|��}tt|dd�|dd�����D]F\}\}}t j |j ||���}||j z } t|| ��||<�G|S)aCFind 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. r1z%Expected matrix of CSC format, got %sNr^r)r rrrr+r r(�zeros� enumerate�zip�copyr!r;rt) rr+� 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�(��V�C�R�C�[�&����*�)E�)E�F�F�.�.���|��s��w�q�v�e�C�i�(�)�)�� ��� "��#�D�"�-�-��u� � � �Mrc������ddd�f��j�t��fd���fd���fd���fd��j�j���S)aACreate 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�|zz S�Nrv��xr�offsets ��r�<lambda>z)_implicit_column_offset.<locals>.<lambda>������Q���!��+�rc����|z�|zz Sr�rvr�s ��rr�z)_implicit_column_offset.<locals>.<lambda>�r�rc�>���|z�|���zz Sr�)�sum�r��XTr�s ��rr�z)_implicit_column_offset.<locals>.<lambda>�s���"�q�&�F�Q�U�U�W�W�$4�5�rc�`���|z�j|�d���ddd�fzz S)Nrr)r4r�r�s ��rr�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�rmatmatr7r )r4rr7r )rr�r�s``@r�_implicit_column_offsetr��s|������D�!�!�!�G�_�F� ��B� �+�+�+�+�+�+�+�+�+�+�5�5�5�5�5�D�D�D�D�D��g��g�  � � �r)NF)F)NN)&�__doc__�numpyr(� scipy.sparse�sparser �scipy.sparse.linalgr� utils.fixesrr�utils.validationr�sparsefuncs_fastr r3r r2r r=rrr&r,r5r>r@rBrKrTrWrYr\rhrtrnr�r�rvrr�<module>r�s ��H�H� ����������.�.�.�.�.�.�>�>�>�>�>�>�>�>�3�3�3�3�3�3������������������� ��� � � �&1�&1�&1�R2�2�2�$K�K�K�K�\NR�p�p�p�p�p�f*�*�*�Z)�)�)�X���:<�<�<�~*�*�*�Z.�.�.�b!�!�!�!�H3?�3?�3?�3?�l � � �, � � ����>����r
Memory