scipy.sparse.csr_matrix 谁借莪1个温暖的怀抱¢ 2022-05-17 05:35 199阅读 0赞 地址:[https://docs.scipy.org/doc/scipy-0.18.1/reference/generated/scipy.sparse.csr\_matrix.html][https_docs.scipy.org_doc_scipy-0.18.1_reference_generated_scipy.sparse.csr_matrix.html] # scipy.sparse.csr\_matrix # *class *scipy.sparse.csr\_matrix(*arg1*, *shape=None*, *dtype=None*, *copy=False*)[\[source\]][source] Compressed Sparse Row matrix This can be instantiated in several ways: csr\_matrix(D) with a dense matrix or rank-2 ndarray D csr\_matrix(S) with another sparse 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. 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) 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]]) 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]]) Attributes <table> <tbody> <tr> <td><a href="https://docs.scipy.org/doc/scipy-0.18.1/reference/generated/scipy.sparse.csr_matrix.nnz.html#scipy.sparse.csr_matrix.nnz" rel="nofollow">nnz</a></td> <td>Number of stored values, including explicit zeros.</td> </tr> <tr> <td><a href="https://docs.scipy.org/doc/scipy-0.18.1/reference/generated/scipy.sparse.csr_matrix.has_sorted_indices.html#scipy.sparse.csr_matrix.has_sorted_indices" rel="nofollow">has_sorted_indices</a></td> <td>Determine whether the matrix has sorted indices</td> </tr> </tbody> </table> <table> <tbody> <tr> <td>dtype</td> <td>(dtype) Data type of the matrix</td> </tr> <tr> <td>shape</td> <td>(2-tuple) Shape of the matrix</td> </tr> <tr> <td>ndim</td> <td>(int) Number of dimensions (this is always 2)</td> </tr> <tr> <td>data</td> <td>CSR format data array of the matrix</td> </tr> <tr> <td>indices</td> <td>CSR format index array of the matrix</td> </tr> <tr> <td>indptr</td> <td>CSR format index pointer array of the matrix</td> </tr> </tbody> </table> Methods <table> <tbody> <tr> <td><a href="https://docs.scipy.org/doc/scipy-0.18.1/reference/generated/scipy.sparse.csr_matrix.arcsin.html#scipy.sparse.csr_matrix.arcsin" rel="nofollow">arcsin</a>()</td> <td>Element-wise arcsin.</td> </tr> <tr> <td><a href="https://docs.scipy.org/doc/scipy-0.18.1/reference/generated/scipy.sparse.csr_matrix.arcsinh.html#scipy.sparse.csr_matrix.arcsinh" rel="nofollow">arcsinh</a>()</td> <td>Element-wise arcsinh.</td> </tr> <tr> <td><a href="https://docs.scipy.org/doc/scipy-0.18.1/reference/generated/scipy.sparse.csr_matrix.arctan.html#scipy.sparse.csr_matrix.arctan" rel="nofollow">arctan</a>()</td> <td>Element-wise arctan.</td> </tr> <tr> <td><a href="https://docs.scipy.org/doc/scipy-0.18.1/reference/generated/scipy.sparse.csr_matrix.arctanh.html#scipy.sparse.csr_matrix.arctanh" rel="nofollow">arctanh</a>()</td> <td>Element-wise arctanh.</td> </tr> <tr> <td><a href="https://docs.scipy.org/doc/scipy-0.18.1/reference/generated/scipy.sparse.csr_matrix.asformat.html#scipy.sparse.csr_matrix.asformat" rel="nofollow">asformat</a>(format)</td> <td>Return this matrix in a given sparse format</td> </tr> <tr> <td><a href="https://docs.scipy.org/doc/scipy-0.18.1/reference/generated/scipy.sparse.csr_matrix.asfptype.html#scipy.sparse.csr_matrix.asfptype" rel="nofollow">asfptype</a>()</td> <td>Upcast matrix to a floating point format (if necessary)</td> </tr> <tr> <td><a href="https://docs.scipy.org/doc/scipy-0.18.1/reference/generated/scipy.sparse.csr_matrix.astype.html#scipy.sparse.csr_matrix.astype" rel="nofollow">astype</a>(t)</td> <td> </td> </tr> <tr> <td><a href="https://docs.scipy.org/doc/scipy-0.18.1/reference/generated/scipy.sparse.csr_matrix.ceil.html#scipy.sparse.csr_matrix.ceil" rel="nofollow">ceil</a>()</td> <td>Element-wise ceil.</td> </tr> <tr> <td><a href="https://docs.scipy.org/doc/scipy-0.18.1/reference/generated/scipy.sparse.csr_matrix.check_format.html#scipy.sparse.csr_matrix.check_format" rel="nofollow">check_format</a>([full_check])</td> <td>check whether the matrix format is valid</td> </tr> <tr> <td><a href="https://docs.scipy.org/doc/scipy-0.18.1/reference/generated/scipy.sparse.csr_matrix.conj.html#scipy.sparse.csr_matrix.conj" rel="nofollow">conj</a>()</td> <td> </td> </tr> <tr> <td><a href="https://docs.scipy.org/doc/scipy-0.18.1/reference/generated/scipy.sparse.csr_matrix.conjugate.html#scipy.sparse.csr_matrix.conjugate" rel="nofollow">conjugate</a>()</td> <td> </td> </tr> <tr> <td><a href="https://docs.scipy.org/doc/scipy-0.18.1/reference/generated/scipy.sparse.csr_matrix.copy.html#scipy.sparse.csr_matrix.copy" rel="nofollow">copy</a>()</td> <td> </td> </tr> <tr> <td><a href="https://docs.scipy.org/doc/scipy-0.18.1/reference/generated/scipy.sparse.csr_matrix.count_nonzero.html#scipy.sparse.csr_matrix.count_nonzero" rel="nofollow">count_nonzero</a>()</td> <td>Number of non-zero entries, equivalent to</td> </tr> <tr> <td><a href="https://docs.scipy.org/doc/scipy-0.18.1/reference/generated/scipy.sparse.csr_matrix.deg2rad.html#scipy.sparse.csr_matrix.deg2rad" rel="nofollow">deg2rad</a>()</td> <td>Element-wise deg2rad.</td> </tr> <tr> <td><a href="https://docs.scipy.org/doc/scipy-0.18.1/reference/generated/scipy.sparse.csr_matrix.diagonal.html#scipy.sparse.csr_matrix.diagonal" rel="nofollow">diagonal</a>()</td> <td>Returns the main diagonal of the matrix</td> </tr> <tr> <td><a href="https://docs.scipy.org/doc/scipy-0.18.1/reference/generated/scipy.sparse.csr_matrix.dot.html#scipy.sparse.csr_matrix.dot" rel="nofollow">dot</a>(other)</td> <td>Ordinary dot product</td> </tr> <tr> <td><a href="https://docs.scipy.org/doc/scipy-0.18.1/reference/generated/scipy.sparse.csr_matrix.eliminate_zeros.html#scipy.sparse.csr_matrix.eliminate_zeros" rel="nofollow">eliminate_zeros</a>()</td> <td>Remove zero entries from the matrix</td> </tr> <tr> <td><a href="https://docs.scipy.org/doc/scipy-0.18.1/reference/generated/scipy.sparse.csr_matrix.expm1.html#scipy.sparse.csr_matrix.expm1" rel="nofollow">expm1</a>()</td> <td>Element-wise expm1.</td> </tr> <tr> <td><a href="https://docs.scipy.org/doc/scipy-0.18.1/reference/generated/scipy.sparse.csr_matrix.floor.html#scipy.sparse.csr_matrix.floor" rel="nofollow">floor</a>()</td> <td>Element-wise floor.</td> </tr> <tr> <td><a href="https://docs.scipy.org/doc/scipy-0.18.1/reference/generated/scipy.sparse.csr_matrix.getH.html#scipy.sparse.csr_matrix.getH" rel="nofollow">getH</a>()</td> <td> </td> </tr> <tr> <td><a href="https://docs.scipy.org/doc/scipy-0.18.1/reference/generated/scipy.sparse.csr_matrix.get_shape.html#scipy.sparse.csr_matrix.get_shape" rel="nofollow">get_shape</a>()</td> <td> </td> </tr> <tr> <td><a href="https://docs.scipy.org/doc/scipy-0.18.1/reference/generated/scipy.sparse.csr_matrix.getcol.html#scipy.sparse.csr_matrix.getcol" rel="nofollow">getcol</a>(i)</td> <td>Returns a copy of column i of the matrix, as a (m x 1) CSR matrix (column vector).</td> </tr> <tr> <td><a href="https://docs.scipy.org/doc/scipy-0.18.1/reference/generated/scipy.sparse.csr_matrix.getformat.html#scipy.sparse.csr_matrix.getformat" rel="nofollow">getformat</a>()</td> <td> </td> </tr> <tr> <td><a href="https://docs.scipy.org/doc/scipy-0.18.1/reference/generated/scipy.sparse.csr_matrix.getmaxprint.html#scipy.sparse.csr_matrix.getmaxprint" rel="nofollow">getmaxprint</a>()</td> <td> </td> </tr> <tr> <td><a href="https://docs.scipy.org/doc/scipy-0.18.1/reference/generated/scipy.sparse.csr_matrix.getnnz.html#scipy.sparse.csr_matrix.getnnz" rel="nofollow">getnnz</a>([axis])</td> <td>Number of stored values, including explicit zeros.</td> </tr> <tr> <td><a href="https://docs.scipy.org/doc/scipy-0.18.1/reference/generated/scipy.sparse.csr_matrix.getrow.html#scipy.sparse.csr_matrix.getrow" rel="nofollow">getrow</a>(i)</td> <td>Returns a copy of row i of the matrix, as a (1 x n) CSR matrix (row vector).</td> </tr> <tr> <td><a href="https://docs.scipy.org/doc/scipy-0.18.1/reference/generated/scipy.sparse.csr_matrix.log1p.html#scipy.sparse.csr_matrix.log1p" rel="nofollow">log1p</a>()</td> <td>Element-wise log1p.</td> </tr> <tr> <td><a href="https://docs.scipy.org/doc/scipy-0.18.1/reference/generated/scipy.sparse.csr_matrix.max.html#scipy.sparse.csr_matrix.max" rel="nofollow">max</a>([axis, out])</td> <td>Return the maximum of the matrix or maximum along an axis.</td> </tr> <tr> <td><a href="https://docs.scipy.org/doc/scipy-0.18.1/reference/generated/scipy.sparse.csr_matrix.maximum.html#scipy.sparse.csr_matrix.maximum" rel="nofollow">maximum</a>(other)</td> <td> </td> </tr> <tr> <td><a href="https://docs.scipy.org/doc/scipy-0.18.1/reference/generated/scipy.sparse.csr_matrix.mean.html#scipy.sparse.csr_matrix.mean" rel="nofollow">mean</a>([axis, dtype, out])</td> <td>Compute the arithmetic mean along the specified axis.</td> </tr> <tr> <td><a href="https://docs.scipy.org/doc/scipy-0.18.1/reference/generated/scipy.sparse.csr_matrix.min.html#scipy.sparse.csr_matrix.min" rel="nofollow">min</a>([axis, out])</td> <td>Return the minimum of the matrix or maximum along an axis.</td> </tr> <tr> <td><a href="https://docs.scipy.org/doc/scipy-0.18.1/reference/generated/scipy.sparse.csr_matrix.minimum.html#scipy.sparse.csr_matrix.minimum" rel="nofollow">minimum</a>(other)</td> <td> </td> </tr> <tr> <td><a href="https://docs.scipy.org/doc/scipy-0.18.1/reference/generated/scipy.sparse.csr_matrix.multiply.html#scipy.sparse.csr_matrix.multiply" rel="nofollow">multiply</a>(other)</td> <td>Point-wise multiplication by another matrix, vector, or scalar.</td> </tr> <tr> <td><a href="https://docs.scipy.org/doc/scipy-0.18.1/reference/generated/scipy.sparse.csr_matrix.nonzero.html#scipy.sparse.csr_matrix.nonzero" rel="nofollow">nonzero</a>()</td> <td>nonzero indices</td> </tr> <tr> <td><a href="https://docs.scipy.org/doc/scipy-0.18.1/reference/generated/scipy.sparse.csr_matrix.power.html#scipy.sparse.csr_matrix.power" rel="nofollow">power</a>(n[, dtype])</td> <td>This function performs element-wise power.</td> </tr> <tr> <td><a href="https://docs.scipy.org/doc/scipy-0.18.1/reference/generated/scipy.sparse.csr_matrix.prune.html#scipy.sparse.csr_matrix.prune" rel="nofollow">prune</a>()</td> <td>Remove empty space after all non-zero elements.</td> </tr> <tr> <td><a href="https://docs.scipy.org/doc/scipy-0.18.1/reference/generated/scipy.sparse.csr_matrix.rad2deg.html#scipy.sparse.csr_matrix.rad2deg" rel="nofollow">rad2deg</a>()</td> <td>Element-wise rad2deg.</td> </tr> <tr> <td><a href="https://docs.scipy.org/doc/scipy-0.18.1/reference/generated/scipy.sparse.csr_matrix.reshape.html#scipy.sparse.csr_matrix.reshape" rel="nofollow">reshape</a>(shape[, order])</td> <td>Gives a new shape to a sparse matrix without changing its data.</td> </tr> <tr> <td><a href="https://docs.scipy.org/doc/scipy-0.18.1/reference/generated/scipy.sparse.csr_matrix.rint.html#scipy.sparse.csr_matrix.rint" rel="nofollow">rint</a>()</td> <td>Element-wise rint.</td> </tr> <tr> <td><a href="https://docs.scipy.org/doc/scipy-0.18.1/reference/generated/scipy.sparse.csr_matrix.set_shape.html#scipy.sparse.csr_matrix.set_shape" rel="nofollow">set_shape</a>(shape)</td> <td> </td> </tr> <tr> <td><a href="https://docs.scipy.org/doc/scipy-0.18.1/reference/generated/scipy.sparse.csr_matrix.setdiag.html#scipy.sparse.csr_matrix.setdiag" rel="nofollow">setdiag</a>(values[, k])</td> <td>Set diagonal or off-diagonal elements of the array.</td> </tr> <tr> <td><a href="https://docs.scipy.org/doc/scipy-0.18.1/reference/generated/scipy.sparse.csr_matrix.sign.html#scipy.sparse.csr_matrix.sign" rel="nofollow">sign</a>()</td> <td>Element-wise sign.</td> </tr> <tr> <td><a href="https://docs.scipy.org/doc/scipy-0.18.1/reference/generated/scipy.sparse.csr_matrix.sin.html#scipy.sparse.csr_matrix.sin" rel="nofollow">sin</a>()</td> <td>Element-wise sin.</td> </tr> <tr> <td><a href="https://docs.scipy.org/doc/scipy-0.18.1/reference/generated/scipy.sparse.csr_matrix.sinh.html#scipy.sparse.csr_matrix.sinh" rel="nofollow">sinh</a>()</td> <td>Element-wise sinh.</td> </tr> <tr> <td><a href="https://docs.scipy.org/doc/scipy-0.18.1/reference/generated/scipy.sparse.csr_matrix.sort_indices.html#scipy.sparse.csr_matrix.sort_indices" rel="nofollow">sort_indices</a>()</td> <td>Sort the indices of this matrix <em>in place</em></td> </tr> <tr> <td><a href="https://docs.scipy.org/doc/scipy-0.18.1/reference/generated/scipy.sparse.csr_matrix.sorted_indices.html#scipy.sparse.csr_matrix.sorted_indices" rel="nofollow">sorted_indices</a>()</td> <td>Return a copy of this matrix with sorted indices</td> </tr> <tr> <td><a href="https://docs.scipy.org/doc/scipy-0.18.1/reference/generated/scipy.sparse.csr_matrix.sqrt.html#scipy.sparse.csr_matrix.sqrt" rel="nofollow">sqrt</a>()</td> <td>Element-wise sqrt.</td> </tr> <tr> <td><a href="https://docs.scipy.org/doc/scipy-0.18.1/reference/generated/scipy.sparse.csr_matrix.sum.html#scipy.sparse.csr_matrix.sum" rel="nofollow">sum</a>([axis, dtype, out])</td> <td>Sum the matrix elements over a given axis.</td> </tr> <tr> <td><a href="https://docs.scipy.org/doc/scipy-0.18.1/reference/generated/scipy.sparse.csr_matrix.sum_duplicates.html#scipy.sparse.csr_matrix.sum_duplicates" rel="nofollow">sum_duplicates</a>()</td> <td>Eliminate duplicate matrix entries by adding them together</td> </tr> <tr> <td><a href="https://docs.scipy.org/doc/scipy-0.18.1/reference/generated/scipy.sparse.csr_matrix.tan.html#scipy.sparse.csr_matrix.tan" rel="nofollow">tan</a>()</td> <td>Element-wise tan.</td> </tr> <tr> <td><a href="https://docs.scipy.org/doc/scipy-0.18.1/reference/generated/scipy.sparse.csr_matrix.tanh.html#scipy.sparse.csr_matrix.tanh" rel="nofollow">tanh</a>()</td> <td>Element-wise tanh.</td> </tr> <tr> <td><a href="https://docs.scipy.org/doc/scipy-0.18.1/reference/generated/scipy.sparse.csr_matrix.toarray.html#scipy.sparse.csr_matrix.toarray" rel="nofollow">toarray</a>([order, out])</td> <td>See the docstring for <a href="https://docs.scipy.org/doc/scipy-0.18.1/reference/generated/scipy.sparse.spmatrix.toarray.html#scipy.sparse.spmatrix.toarray" rel="nofollow">spmatrix.toarray</a>.</td> </tr> <tr> <td><a href="https://docs.scipy.org/doc/scipy-0.18.1/reference/generated/scipy.sparse.csr_matrix.tobsr.html#scipy.sparse.csr_matrix.tobsr" rel="nofollow">tobsr</a>([blocksize, copy])</td> <td>Convert this matrix to Block Sparse Row format.</td> </tr> <tr> <td><a href="https://docs.scipy.org/doc/scipy-0.18.1/reference/generated/scipy.sparse.csr_matrix.tocoo.html#scipy.sparse.csr_matrix.tocoo" rel="nofollow">tocoo</a>([copy])</td> <td>Convert this matrix to COOrdinate format.</td> </tr> <tr> <td><a href="https://docs.scipy.org/doc/scipy-0.18.1/reference/generated/scipy.sparse.csr_matrix.tocsc.html#scipy.sparse.csr_matrix.tocsc" rel="nofollow">tocsc</a>([copy])</td> <td> </td> </tr> <tr> <td><a href="https://docs.scipy.org/doc/scipy-0.18.1/reference/generated/scipy.sparse.csr_matrix.tocsr.html#scipy.sparse.csr_matrix.tocsr" rel="nofollow">tocsr</a>([copy])</td> <td>Convert this matrix to Compressed Sparse Row format.</td> </tr> <tr> <td><a href="https://docs.scipy.org/doc/scipy-0.18.1/reference/generated/scipy.sparse.csr_matrix.todense.html#scipy.sparse.csr_matrix.todense" rel="nofollow">todense</a>([order, out])</td> <td>Return a dense matrix representation of this matrix.</td> </tr> <tr> <td><a href="https://docs.scipy.org/doc/scipy-0.18.1/reference/generated/scipy.sparse.csr_matrix.todia.html#scipy.sparse.csr_matrix.todia" rel="nofollow">todia</a>([copy])</td> <td>Convert this matrix to sparse DIAgonal format.</td> </tr> <tr> <td><a href="https://docs.scipy.org/doc/scipy-0.18.1/reference/generated/scipy.sparse.csr_matrix.todok.html#scipy.sparse.csr_matrix.todok" rel="nofollow">todok</a>([copy])</td> <td>Convert this matrix to Dictionary Of Keys format.</td> </tr> <tr> <td><a href="https://docs.scipy.org/doc/scipy-0.18.1/reference/generated/scipy.sparse.csr_matrix.tolil.html#scipy.sparse.csr_matrix.tolil" rel="nofollow">tolil</a>([copy])</td> <td>Convert this matrix to LInked List format.</td> </tr> <tr> <td><a href="https://docs.scipy.org/doc/scipy-0.18.1/reference/generated/scipy.sparse.csr_matrix.transpose.html#scipy.sparse.csr_matrix.transpose" rel="nofollow">transpose</a>([axes, copy])</td> <td>Reverses the dimensions of the sparse matrix.</td> </tr> <tr> <td><a href="https://docs.scipy.org/doc/scipy-0.18.1/reference/generated/scipy.sparse.csr_matrix.trunc.html#scipy.sparse.csr_matrix.trunc" rel="nofollow">trunc</a>()</td> <td>Element-wise trunc.</td> </tr> </tbody> </table> [https_docs.scipy.org_doc_scipy-0.18.1_reference_generated_scipy.sparse.csr_matrix.html]: https://docs.scipy.org/doc/scipy-0.18.1/reference/generated/scipy.sparse.csr_matrix.html [source]: http://github.com/scipy/scipy/blob/v0.18.1/scipy/sparse/csr.py#L23-L456
还没有评论,来说两句吧...