egttools.analytical.sed_analytical.lil_matrix¶
- class lil_matrix(arg1, shape=None, dtype=None, copy=False)[source]¶
Bases:
spmatrix
,IndexMixin
Row-based list of lists sparse matrix
This is a structure for constructing sparse matrices incrementally. Note that inserting a single item can take linear time in the worst case; to construct a matrix efficiently, make sure the items are pre-sorted by index, per row.
- This can be instantiated in several ways:
- lil_matrix(D)
with a dense matrix or rank-2 ndarray D
- lil_matrix(S)
with another sparse matrix S (equivalent to S.tolil())
- lil_matrix((M, N), [dtype])
to construct an empty matrix with shape (M, N) dtype is optional, defaulting to dtype=’d’.
- dtype¶
Data type of the matrix
- Type
dtype
- shape¶
Shape of the matrix
- Type
2-tuple
- nnz¶
Number of stored values, including explicit zeros
- data¶
LIL format data array of the matrix
- rows¶
LIL format row index array of the matrix
Notes
Sparse matrices can be used in arithmetic operations: they support addition, subtraction, multiplication, division, and matrix power.
- Advantages of the LIL format
supports flexible slicing
changes to the matrix sparsity structure are efficient
- Disadvantages of the LIL format
arithmetic operations LIL + LIL are slow (consider CSR or CSC)
slow column slicing (consider CSC)
slow matrix vector products (consider CSR or CSC)
- Intended Usage
LIL is a convenient format for constructing sparse matrices
once a matrix has been constructed, convert to CSR or CSC format for fast arithmetic and matrix vector operations
consider using the COO format when constructing large matrices
- Data Structure
An array (
self.rows
) of rows, each of which is a sorted list of column indices of non-zero elements.The corresponding nonzero values are stored in similar fashion in
self.data
.
Methods
Return this matrix in the passed format.
Upcast matrix to a floating point format (if necessary)
Cast the matrix elements to a specified type.
Element-wise complex conjugation.
Element-wise complex conjugation.
Returns a copy of this matrix.
Number of non-zero entries, equivalent to
Returns the kth diagonal of the matrix.
Ordinary dot product
Return the Hermitian transpose of this matrix.
Get shape of a matrix.
Returns a copy of column j of the matrix, as an (m x 1) sparse matrix (column vector).
Format of a matrix representation as a string.
Maximum number of elements to display when printed.
Number of stored values, including explicit zeros.
Returns a copy of the 'i'th row.
Returns a view of the 'i'th row (without copying).
Element-wise maximum between this and another matrix.
Compute the arithmetic mean along the specified axis.
Element-wise minimum between this and another matrix.
Point-wise multiplication by another matrix
nonzero indices
Element-wise power.
Gives a new shape to a sparse matrix without changing its data.
Resize the matrix in-place to dimensions given by
shape
See
reshape
.Set diagonal or off-diagonal elements of the array.
Sum the matrix elements over a given axis.
Return a dense ndarray representation of this matrix.
Convert this matrix to Block Sparse Row format.
Convert this matrix to COOrdinate format.
Convert this matrix to Compressed Sparse Column format.
Convert this matrix to Compressed Sparse Row format.
Return a dense matrix representation of this matrix.
Convert this matrix to sparse DIAgonal format.
Convert this matrix to Dictionary Of Keys format.
Convert this matrix to List of Lists format.
Returns the sum along diagonals of the sparse matrix.
Reverses the dimensions of the sparse matrix.
Attributes
Number of stored values, including explicit zeros.
Get shape of a matrix.
- __abs__()¶
- __add__(other)¶
- __bool__()¶
- __div__(other)¶
- __eq__(other)¶
Return self==value.
- __ge__(other)¶
Return self>=value.
- __getattr__(attr)¶
- __gt__(other)¶
Return self>value.
- __idiv__(other)¶
- __iter__()¶
- __le__(other)¶
Return self<=value.
- __len__()¶
- __lt__(other)¶
Return self<value.
- __matmul__(other)¶
- __mul__(other)¶
- __ne__(other)¶
Return self!=value.
- __neg__()¶
- __nonzero__()¶
- __pow__(other)¶
- __radd__(other)¶
- __rdiv__(other)¶
- __repr__()¶
Return repr(self).
- __rmatmul__(other)¶
- __rmul__(other)¶
- __round__(ndigits=0)¶
- __rsub__(other)¶
- __rtruediv__(other)¶
- __sub__(other)¶
- asformat(format, copy=False)¶
Return this matrix in the passed format.
- Parameters
format ({str, None}) – The desired matrix format (“csr”, “csc”, “lil”, “dok”, “array”, …) or None for no conversion.
copy (bool, optional) – If True, the result is guaranteed to not share data with self.
- Returns
A
- Return type
This matrix in the passed format.
- asfptype()¶
Upcast matrix to a floating point format (if necessary)
- astype(dtype, casting='unsafe', copy=True)¶
Cast the matrix elements to a specified type.
- Parameters
dtype (string or numpy dtype) – Typecode or data-type to which to cast the data.
casting ({'no', 'equiv', 'safe', 'same_kind', 'unsafe'}, optional) – Controls what kind of data casting may occur. Defaults to ‘unsafe’ for backwards compatibility. ‘no’ means the data types should not be cast at all. ‘equiv’ means only byte-order changes are allowed. ‘safe’ means only casts which can preserve values are allowed. ‘same_kind’ means only safe casts or casts within a kind, like float64 to float32, are allowed. ‘unsafe’ means any data conversions may be done.
copy (bool, optional) – If
copy
isFalse
, the result might share some memory with this matrix. Ifcopy
isTrue
, it is guaranteed that the result and this matrix do not share any memory.
- conj(copy=True)¶
Element-wise complex conjugation.
If the matrix is of non-complex data type and
copy
is False, this method does nothing and the data is not copied.- Parameters
copy (bool, optional) – If True, the result is guaranteed to not share data with self.
- Returns
A
- Return type
The element-wise complex conjugate.
- conjugate(copy=True)¶
Element-wise complex conjugation.
If the matrix is of non-complex data type and
copy
is False, this method does nothing and the data is not copied.- Parameters
copy (bool, optional) – If True, the result is guaranteed to not share data with self.
- Returns
A
- Return type
The element-wise complex conjugate.
- copy()[source]¶
Returns a copy of this matrix.
No data/indices will be shared between the returned value and current matrix.
- count_nonzero()[source]¶
Number of non-zero entries, equivalent to
np.count_nonzero(a.toarray())
Unlike getnnz() and the nnz property, which return the number of stored entries (the length of the data attribute), this method counts the actual number of non-zero entries in data.
- diagonal(k=0)¶
Returns the kth diagonal of the matrix.
- Parameters
k (int, optional) –
Which diagonal to get, corresponding to elements a[i, i+k]. Default: 0 (the main diagonal).
New in version 1.0.
See also
numpy.diagonal
Equivalent numpy function.
Examples
>>> from scipy.sparse import csr_matrix >>> A = csr_matrix([[1, 2, 0], [0, 0, 3], [4, 0, 5]]) >>> A.diagonal() array([1, 0, 5]) >>> A.diagonal(k=1) array([2, 3])
- dot(other)¶
Ordinary dot product
Examples
>>> import numpy as np >>> from scipy.sparse import csr_matrix >>> A = csr_matrix([[1, 2, 0], [0, 0, 3], [4, 0, 5]]) >>> v = np.array([1, 0, -1]) >>> A.dot(v) array([ 1, -3, -1], dtype=int64)
- getH()¶
Return the Hermitian transpose of this matrix.
See also
numpy.matrix.getH
NumPy’s implementation of
getH
for matrices
- get_shape()¶
Get shape of a matrix.
- getcol(j)¶
Returns a copy of column j of the matrix, as an (m x 1) sparse matrix (column vector).
- getformat()¶
Format of a matrix representation as a string.
- getmaxprint()¶
Maximum number of elements to display when printed.
- getnnz(axis=None)[source]¶
Number of stored values, including explicit zeros.
- Parameters
axis (None, 0, or 1) – Select between the number of values across the whole matrix, in each column, or in each row.
See also
count_nonzero
Number of non-zero entries
- maximum(other)¶
Element-wise maximum between this and another matrix.
- mean(axis=None, dtype=None, out=None)¶
Compute the arithmetic mean along the specified axis.
Returns the average of the matrix elements. The average is taken over all elements in the matrix by default, otherwise over the specified axis.
float64
intermediate and return values are used for integer inputs.- Parameters
axis ({-2, -1, 0, 1, None} optional) – Axis along which the mean is computed. The default is to compute the mean of all elements in the matrix (i.e.,
axis
=None
).dtype (data-type, optional) –
Type to use in computing the mean. For integer inputs, the default is
float64
; for floating point inputs, it is the same as the input dtype.New in version 0.18.0.
out (np.matrix, optional) –
Alternative output matrix in which to place the result. It must have the same shape as the expected output, but the type of the output values will be cast if necessary.
New in version 0.18.0.
- Returns
m
- Return type
np.matrix
See also
numpy.matrix.mean
NumPy’s implementation of ‘mean’ for matrices
- minimum(other)¶
Element-wise minimum between this and another matrix.
- multiply(other)¶
Point-wise multiplication by another matrix
- nonzero()¶
nonzero indices
Returns a tuple of arrays (row,col) containing the indices of the non-zero elements of the matrix.
Examples
>>> from scipy.sparse import csr_matrix >>> A = csr_matrix([[1,2,0],[0,0,3],[4,0,5]]) >>> A.nonzero() (array([0, 0, 1, 2, 2]), array([0, 1, 2, 0, 2]))
- power(n, dtype=None)¶
Element-wise power.
- reshape(self, shape, order='C', copy=False)[source]¶
Gives a new shape to a sparse matrix without changing its data.
- Parameters
shape (length-2 tuple of ints) – The new shape should be compatible with the original shape.
order ({'C', 'F'}, optional) – Read the elements using this index order. ‘C’ means to read and write the elements using C-like index order; e.g., read entire first row, then second row, etc. ‘F’ means to read and write the elements using Fortran-like index order; e.g., read entire first column, then second column, etc.
copy (bool, optional) – Indicates whether or not attributes of self should be copied whenever possible. The degree to which attributes are copied varies depending on the type of sparse matrix being used.
- Returns
reshaped_matrix – A sparse matrix with the given
shape
, not necessarily of the same format as the current object.- Return type
sparse matrix
See also
numpy.matrix.reshape
NumPy’s implementation of ‘reshape’ for matrices
- resize(*shape)[source]¶
Resize the matrix in-place to dimensions given by
shape
Any elements that lie within the new shape will remain at the same indices, while non-zero elements lying outside the new shape are removed.
Notes
The semantics are not identical to
numpy.ndarray.resize
ornumpy.resize
. Here, the same data will be maintained at each index before and after reshape, if that index is within the new bounds. In numpy, resizing maintains contiguity of the array, moving elements around in the logical matrix but not within a flattened representation.We give no guarantees about whether the underlying data attributes (arrays, etc.) will be modified in place or replaced with new objects.
- setdiag(values, k=0)¶
Set diagonal or off-diagonal elements of the array.
- Parameters
values (array_like) –
New values of the diagonal elements.
Values may have any length. If the diagonal is longer than values, then the remaining diagonal entries will not be set. If values are longer than the diagonal, then the remaining values are ignored.
If a scalar value is given, all of the diagonal is set to it.
k (int, optional) – Which off-diagonal to set, corresponding to elements a[i,i+k]. Default: 0 (the main diagonal).
- sum(axis=None, dtype=None, out=None)¶
Sum the matrix elements over a given axis.
- Parameters
axis ({-2, -1, 0, 1, None} optional) – Axis along which the sum is computed. The default is to compute the sum of all the matrix elements, returning a scalar (i.e.,
axis
=None
).dtype (dtype, optional) –
The type of the returned matrix and of the accumulator in which the elements are summed. The dtype of
a
is used by default unlessa
has an integer dtype of less precision than the default platform integer. In that case, ifa
is signed then the platform integer is used while ifa
is unsigned then an unsigned integer of the same precision as the platform integer is used.New in version 0.18.0.
out (np.matrix, optional) –
Alternative output matrix in which to place the result. It must have the same shape as the expected output, but the type of the output values will be cast if necessary.
New in version 0.18.0.
- Returns
sum_along_axis – A matrix with the same shape as
self
, with the specified axis removed.- Return type
np.matrix
See also
numpy.matrix.sum
NumPy’s implementation of ‘sum’ for matrices
- toarray(order=None, out=None)[source]¶
Return a dense ndarray representation of this matrix.
- Parameters
order ({'C', 'F'}, optional) – Whether to store multidimensional data in C (row-major) or Fortran (column-major) order in memory. The default is ‘None’, which provides no ordering guarantees. Cannot be specified in conjunction with the
out
argument.out (ndarray, 2-D, optional) – If specified, uses this array as the output buffer instead of allocating a new array to return. The provided array must have the same shape and dtype as the sparse matrix on which you are calling the method. For most sparse types,
out
is required to be memory contiguous (either C or Fortran ordered).
- Returns
arr – An array with the same shape and containing the same data represented by the sparse matrix, with the requested memory order. If
out
was passed, the same object is returned after being modified in-place to contain the appropriate values.- Return type
ndarray, 2-D
- tobsr(blocksize=None, copy=False)¶
Convert this matrix to Block Sparse Row format.
With copy=False, the data/indices may be shared between this matrix and the resultant bsr_matrix.
When blocksize=(R, C) is provided, it will be used for construction of the bsr_matrix.
- tocoo(copy=False)¶
Convert this matrix to COOrdinate format.
With copy=False, the data/indices may be shared between this matrix and the resultant coo_matrix.
- tocsc(copy=False)¶
Convert this matrix to Compressed Sparse Column format.
With copy=False, the data/indices may be shared between this matrix and the resultant csc_matrix.
- tocsr(copy=False)[source]¶
Convert this matrix to Compressed Sparse Row format.
With copy=False, the data/indices may be shared between this matrix and the resultant csr_matrix.
- todense(order=None, out=None)¶
Return a dense matrix representation of this matrix.
- Parameters
order ({'C', 'F'}, optional) – Whether to store multi-dimensional data in C (row-major) or Fortran (column-major) order in memory. The default is ‘None’, which provides no ordering guarantees. Cannot be specified in conjunction with the
out
argument.out (ndarray, 2-D, optional) – If specified, uses this array (or
numpy.matrix
) as the output buffer instead of allocating a new array to return. The provided array must have the same shape and dtype as the sparse matrix on which you are calling the method.
- Returns
arr – A NumPy matrix object with the same shape and containing the same data represented by the sparse matrix, with the requested memory order. If
out
was passed and was an array (rather than anumpy.matrix
), it will be filled with the appropriate values and returned wrapped in anumpy.matrix
object that shares the same memory.- Return type
numpy.matrix, 2-D
- todia(copy=False)¶
Convert this matrix to sparse DIAgonal format.
With copy=False, the data/indices may be shared between this matrix and the resultant dia_matrix.
- todok(copy=False)¶
Convert this matrix to Dictionary Of Keys format.
With copy=False, the data/indices may be shared between this matrix and the resultant dok_matrix.
- tolil(copy=False)[source]¶
Convert this matrix to List of Lists format.
With copy=False, the data/indices may be shared between this matrix and the resultant lil_matrix.
- trace(offset=0)¶
Returns the sum along diagonals of the sparse matrix.
- Parameters
offset (int, optional) – Which diagonal to get, corresponding to elements a[i, i+offset]. Default: 0 (the main diagonal).
- transpose(axes=None, copy=False)[source]¶
Reverses the dimensions of the sparse matrix.
- Parameters
axes (None, optional) – This argument is in the signature solely for NumPy compatibility reasons. Do not pass in anything except for the default value.
copy (bool, optional) – Indicates whether or not attributes of
self
should be copied whenever possible. The degree to which attributes are copied varies depending on the type of sparse matrix being used.
- Returns
p
- Return type
self
with the dimensions reversed.
See also
numpy.matrix.transpose
NumPy’s implementation of ‘transpose’ for matrices
- __array_priority__ = 10.1¶
- __hash__ = None¶
- format = 'lil'¶
- ndim = 2¶
- property nnz¶
Number of stored values, including explicit zeros.
See also
count_nonzero
Number of non-zero entries
- property shape¶
Get shape of a matrix.