L iFdZdZgdZddlZddlmZddlmZm Z ddl m Z m Z m Z mZmZdd lmZdd lmZGd d eZd ZGddee ZGddeeZy)z#Compressed Sparse Row matrix formatzrestructuredtext en) csr_array csr_matrixisspmatrix_csrN)spmatrix)_spbasesparray) csr_tocsc csr_tobsrcsr_count_blocksget_csr_submatrixcsr_sample_values)upcast) _cs_matrixceZdZdZdZddZej je_ddZejje_ddZ ejje _ddZ ejje _ddZ ejje _e d Z d Zd Zd Zd ZdZdZdZdZdZdZdZdZdZdZy) _csr_basecsr)rNc||dk7r td|jdk(r|r|jS|S|j\}}|j |j |j |jf||f|S)N)rrzvSparse arrays/matrices do not support an 'axes' parameter because swapping dimensions is the only logical permutation.rshapecopy) ValueErrorndimrr_csc_containerdataindicesindptr)selfaxesrMNs W/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/scipy/sparse/_csr.py transposez_csr_base.transposes  LM M 99>"&499; 0D 0zz1""DIIt||$(KK$19:AT#K Kc|jdk7r td|j|j|j}|j |j |j|j}}}|j|j}}t|jdD];}||} ||dz} || | j||<|| | j||<=|S)Nrz.Cannot convert a 1d sparse array to lil formatdtyperr) rr_lil_containerrr(sum_duplicatesrrrrowsrangetolist) rrlilptrinddatr+rnstartends r#tolilz_csr_base.tolil$s 99>MN N!!$**DJJ!? kk$,,tyyCXXsxxdtzz!}% .AFEac(C%n++-DG%n++-DG  .  r%c*|r|jS|SNr)rrs r#tocsrz_csr_base.tocsr7s 99; Kr%c |jdk7r td|j\}}|j|j|j ft |j|}tj|dz|}tj|j|}tj|jt|j}t|||jj||j j||j||||j|||f|j}d|_|S)Nrz.Cannot convert a 1d sparse array to csc formatmaxvalrr'rT)rrr_get_index_dtyperrmaxnnznpemptyrr(r astyperrhas_sorted_indices) rrr!r" idx_dtyperrrAs r#tocscz_csr_base.tocsc?s  99>MN Nzz1))4;; *E+.txx+;*= !a%y1((48895xxtzz(:;!Q++$$Y/,,%%i0))     w 7tzz  J#r%c |jdk7r td|ddlm}|j ||S|dk(rR|j j ddd|j|jf}|j||j|S|\}}|j\}}|dks|dks||zd k7s||zd k7rtd |t|||||j|j} |j|j|jft||z|  } tj||zdz| } tj| | } tj | ||f|j" } t%|||||jj'| |jj'| |j | | | j) |j| | | f|j S)Nrz.Cannot convert a 1d sparse array to bsr formatr)estimate_blocksize) blocksize)rrrrzinvalid blocksize r;r'r=)rr_spfuncsrItobsrrreshaperr_bsr_containerrr r>r?rArBzerosr(r rCravel)rrJrrIarg1RCr!r"blksrErrrs r#rMz_csr_base.tobsrWs 99>MN N   4::(:4(@:A A % II%%b1-dll4;;GD&&t4::D&I ICAa**CAa1uAQ!q1uz #5i[!ABB#Aa!DKK ED--t{{DLL.I/21a4.@IXXad1fI6Fhht95G88T!AJdjj9D aAqkk((3ll)))4iigtzz|  5 &&w'tzz' r%c|S)zBswap the members of x if this is a column-oriented matrix xs r#_swapz_csr_base._swaps r%c#K|jdk(r|jjd}d}t|j|j D]%\}}t ||z D]}|||dz}'t |jd|z D]}|ytjd|jj}t|tr|jddnd|jdf}d}|jddD]C} | |z |d<|j|| } |j || } |j| | |f|d| }Eyw)Nrrrr'Tr)rr(typeziprrr,rrArPr isinstancer __class__) rzerouvd_rri0i1rrs r#__iter__z_csr_base.__iter__sM 99>::??1%DADLL$))4 1q1uAJE   4::a=1,-    !4;;#4#45",T7"; 12!TZZPQ]AS ++ab/ BRF1Ill2b)G99R#D..$!8D.Q QB  sE Ec |jdk(r5|dvrtd|d|jd|jdfdS|j\}}t |}|dkr||z }|dks||k\rtd|dt |||j |j|j||dzd| \}}}|j|||fd|f|jd S) z]Returns a copy of row i of the matrix, as a (1 x n) CSR matrix (row vector). r)rrKindex () out of rangerTr8Frr(r) r IndexErrorrNrintr rrrr_r(rir!r"rrrs r#_getrowz_csr_base._getrows 99> 71#^!<==<<DJJqM 2<> >zz1 F q5 FA q5AFwqc89 9 1 q$++t||TYY1q5!Q!H~~tWf5aV$(JJU< ST Tzz1 F q5 FA q5AFwqc89 9 1 q$++t||TYY1aQ!H~~tWf5aV$(JJU< rrAasarrayrvr_r(r zeros_likerBrrrrN) rrwrEr!r"rowcolval new_shapes r# _get_arrayz_csr_base._get_arrays))$,,7 jjI. 88q=>>"DJJ>7 7$**Q-1mmCy1jjI.hhsxxtzz2!Q T\\499((Cc 3"%1!1CII !  ~~ckk)455r%cB|j|j|Sr7)rp_minor_index_fancyrrrs r#_get_intXarrayz_csr_base._get_intXarrays||C 33C88r%c |jdvr|j||dS|j\}}|j|\}}}|j||dz\}} |j|| } |j || } |dkDr | |k\| |kz} n | |k| |kDz} t |dkDr| | |z |zdk(z} | | |z |z} | | } tjdt| g} |dkr| ddd} t | ddd} dtdttjt||z |z f}|j| | | f||jd S) Nr{Tr8rrrrKFrk)r}r~rrrrabsrAarraylenr?rmceilfloatr_r()rrrr!r"r3stopstrideiijj row_indicesrow_datar0 row_indptrrs r#_get_intXslicez_csr_base._get_intXslices 88y &&sCd&; ;zz1!kk!ntVSQ'Bll2b) 99R# A:%'K$,>?C%'K$,>?C v;? K%'61Q6 6C"3'%/F: C=XXq#k"234 A:"~Hk$B$/0KC3rwwuTE\':V'CDEFG~~xjA$(JJU<  F "3r%rc"t|tS)aIs `x` of csr_matrix type? Parameters ---------- x object to check for being a csr matrix Returns ------- bool True if `x` is a csr matrix, False otherwise Examples -------- >>> from scipy.sparse import csr_array, csr_matrix, coo_matrix, isspmatrix_csr >>> isspmatrix_csr(csr_matrix([[5]])) True >>> isspmatrix_csr(csr_array([[5]])) False >>> isspmatrix_csr(coo_matrix([[5]])) False )r^rrXs r#rrs. a $$r%ceZdZdZy)ra Compressed Sparse Row array. This can be instantiated in several ways: csr_array(D) where D is a 2-D ndarray csr_array(S) with another sparse array or matrix S (equivalent to S.tocsr()) csr_array((M, N), [dtype]) to construct an empty array with shape (M, N) dtype is optional, defaulting to dtype='d'. csr_array((data, (row_ind, col_ind)), [shape=(M, N)]) where ``data``, ``row_ind`` and ``col_ind`` satisfy the relationship ``a[row_ind[k], col_ind[k]] = data[k]``. csr_array((data, indices, indptr), [shape=(M, N)]) is the standard CSR representation where the column indices for row i are stored in ``indices[indptr[i]:indptr[i+1]]`` and their corresponding values are stored in ``data[indptr[i]:indptr[i+1]]``. If the shape parameter is not supplied, the array dimensions are inferred from the index arrays. Attributes ---------- dtype : dtype Data type of the array shape : 2-tuple Shape of the array ndim : int Number of dimensions (this is always 2) nnz size data CSR format data array of the array indices CSR format index array of the array indptr CSR format index pointer array of the array has_sorted_indices has_canonical_format T Notes ----- Sparse arrays can be used in arithmetic operations: they support addition, subtraction, multiplication, division, and matrix power. Advantages of the CSR format - efficient arithmetic operations CSR + CSR, CSR * CSR, etc. - efficient row slicing - fast matrix vector products Disadvantages of the CSR format - slow column slicing operations (consider CSC) - changes to the sparsity structure are expensive (consider LIL or DOK) Canonical Format - Within each row, indices are sorted by column. - There are no duplicate entries. Examples -------- >>> import numpy as np >>> from scipy.sparse import csr_array >>> csr_array((3, 4), dtype=np.int8).toarray() array([[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]], dtype=int8) >>> row = np.array([0, 0, 1, 2, 2, 2]) >>> col = np.array([0, 2, 2, 0, 1, 2]) >>> data = np.array([1, 2, 3, 4, 5, 6]) >>> csr_array((data, (row, col)), shape=(3, 3)).toarray() array([[1, 0, 2], [0, 0, 3], [4, 5, 6]]) >>> indptr = np.array([0, 2, 3, 6]) >>> indices = np.array([0, 2, 2, 0, 1, 2]) >>> data = np.array([1, 2, 3, 4, 5, 6]) >>> csr_array((data, indices, indptr), shape=(3, 3)).toarray() array([[1, 0, 2], [0, 0, 3], [4, 5, 6]]) Duplicate entries are summed together: >>> row = np.array([0, 1, 2, 0]) >>> col = np.array([0, 1, 1, 0]) >>> data = np.array([1, 2, 4, 8]) >>> csr_array((data, (row, col)), shape=(3, 3)).toarray() array([[9, 0, 0], [0, 2, 0], [0, 4, 0]]) As an example of how to construct a CSR array incrementally, the following snippet builds a term-document array from texts: >>> docs = [["hello", "world", "hello"], ["goodbye", "cruel", "world"]] >>> indptr = [0] >>> indices = [] >>> data = [] >>> vocabulary = {} >>> for d in docs: ... for term in d: ... index = vocabulary.setdefault(term, len(vocabulary)) ... indices.append(index) ... data.append(1) ... indptr.append(len(indices)) ... >>> csr_array((data, indices, indptr), dtype=int).toarray() array([[2, 1, 0, 0], [0, 1, 1, 1]]) NrrrrrWr%r#rr:wr%rceZdZdZy)ra Compressed Sparse Row matrix. This can be instantiated in several ways: csr_matrix(D) where D is a 2-D ndarray csr_matrix(S) with another sparse array or matrix S (equivalent to S.tocsr()) csr_matrix((M, N), [dtype]) to construct an empty matrix with shape (M, N) dtype is optional, defaulting to dtype='d'. csr_matrix((data, (row_ind, col_ind)), [shape=(M, N)]) where ``data``, ``row_ind`` and ``col_ind`` satisfy the relationship ``a[row_ind[k], col_ind[k]] = data[k]``. csr_matrix((data, indices, indptr), [shape=(M, N)]) is the standard CSR representation where the column indices for row i are stored in ``indices[indptr[i]:indptr[i+1]]`` and their corresponding values are stored in ``data[indptr[i]:indptr[i+1]]``. If the shape parameter is not supplied, the matrix dimensions are inferred from the index arrays. Attributes ---------- dtype : dtype Data type of the matrix shape : 2-tuple Shape of the matrix ndim : int Number of dimensions (this is always 2) nnz size data CSR format data array of the matrix indices CSR format index array of the matrix indptr CSR format index pointer array of the matrix has_sorted_indices has_canonical_format T Notes ----- Sparse matrices can be used in arithmetic operations: they support addition, subtraction, multiplication, division, and matrix power. Advantages of the CSR format - efficient arithmetic operations CSR + CSR, CSR * CSR, etc. - efficient row slicing - fast matrix vector products Disadvantages of the CSR format - slow column slicing operations (consider CSC) - changes to the sparsity structure are expensive (consider LIL or DOK) Canonical Format - Within each row, indices are sorted by column. - There are no duplicate entries. Examples -------- >>> import numpy as np >>> from scipy.sparse import csr_matrix >>> csr_matrix((3, 4), dtype=np.int8).toarray() array([[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]], dtype=int8) >>> row = np.array([0, 0, 1, 2, 2, 2]) >>> col = np.array([0, 2, 2, 0, 1, 2]) >>> data = np.array([1, 2, 3, 4, 5, 6]) >>> csr_matrix((data, (row, col)), shape=(3, 3)).toarray() array([[1, 0, 2], [0, 0, 3], [4, 5, 6]]) >>> indptr = np.array([0, 2, 3, 6]) >>> indices = np.array([0, 2, 2, 0, 1, 2]) >>> data = np.array([1, 2, 3, 4, 5, 6]) >>> csr_matrix((data, indices, indptr), shape=(3, 3)).toarray() array([[1, 0, 2], [0, 0, 3], [4, 5, 6]]) Duplicate entries are summed together: >>> row = np.array([0, 1, 2, 0]) >>> col = np.array([0, 1, 1, 0]) >>> data = np.array([1, 2, 4, 8]) >>> csr_matrix((data, (row, col)), shape=(3, 3)).toarray() array([[9, 0, 0], [0, 2, 0], [0, 4, 0]]) As an example of how to construct a CSR matrix incrementally, the following snippet builds a term-document matrix from texts: >>> docs = [["hello", "world", "hello"], ["goodbye", "cruel", "world"]] >>> indptr = [0] >>> indices = [] >>> data = [] >>> vocabulary = {} >>> for d in docs: ... for term in d: ... index = vocabulary.setdefault(term, len(vocabulary)) ... indices.append(index) ... data.append(1) ... indptr.append(len(indices)) ... >>> csr_matrix((data, indices, indptr), dtype=int).toarray() array([[2, 1, 0, 0], [0, 1, 1, 1]]) NrrWr%r#rrrr%r)r __docformat____all__numpyrA_matrixr_baserr _sparsetoolsr r r r r_sputilsr _compressedrrrrrrWr%r#rsb)% 7#AA#J3 J3Z%6x 7xvx9xr%