package scipy

  1. Overview
  2. Docs
Legend:
Library
Module
Module type
Parameter
Class
Class type
type tag = [
  1. | `Lil_matrix
]
type t = [ `ArrayLike | `IndexMixin | `Lil_matrix | `Object ] Obj.t
val of_pyobject : Py.Object.t -> t
val to_pyobject : [> tag ] Obj.t -> Py.Object.t
val as_index : t -> [ `IndexMixin ] Obj.t
val create : ?shape:Py.Object.t -> ?dtype:Py.Object.t -> ?copy:Py.Object.t -> arg1:Py.Object.t -> unit -> t

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'.

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 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``.
val __getitem__ : key:Py.Object.t -> [> tag ] Obj.t -> Py.Object.t

None

val __iter__ : [> tag ] Obj.t -> Py.Object.t

None

val __setitem__ : key:Py.Object.t -> x:Py.Object.t -> [> tag ] Obj.t -> Py.Object.t

None

val asformat : ?copy:bool -> format:[ `S of string | `None ] -> [> tag ] Obj.t -> Py.Object.t

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 : This matrix in the passed format.

val asfptype : [> tag ] Obj.t -> Py.Object.t

Upcast matrix to a floating point format (if necessary)

val astype : ?casting:[ `No | `Equiv | `Safe | `Same_kind | `Unsafe ] -> ?copy:bool -> dtype:[ `S of string | `Dtype of Np.Dtype.t ] -> [> tag ] Obj.t -> Py.Object.t

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` is `False`, the result might share some memory with this matrix. If `copy` is `True`, it is guaranteed that the result and this matrix do not share any memory.

val conj : ?copy:bool -> [> tag ] Obj.t -> Py.Object.t

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 : The element-wise complex conjugate.

val conjugate : ?copy:bool -> [> tag ] Obj.t -> Py.Object.t

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 : The element-wise complex conjugate.

val copy : [> tag ] Obj.t -> Py.Object.t

Returns a copy of this matrix.

No data/indices will be shared between the returned value and current matrix.

val count_nonzero : [> tag ] Obj.t -> Py.Object.t

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.

val diagonal : ?k:int -> [> tag ] Obj.t -> Py.Object.t

Returns the kth diagonal of the matrix.

Parameters ---------- k : int, optional Which diagonal to get, corresponding to elements ai, i+k. Default: 0 (the main diagonal).

.. versionadded:: 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)

val dot : other:Py.Object.t -> [> tag ] Obj.t -> Py.Object.t

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)

val getH : [> tag ] Obj.t -> Py.Object.t

Return the Hermitian transpose of this matrix.

See Also -------- numpy.matrix.getH : NumPy's implementation of `getH` for matrices

val get_shape : [> tag ] Obj.t -> Py.Object.t

Get shape of a matrix.

val getcol : j:Py.Object.t -> [> tag ] Obj.t -> Py.Object.t

Returns a copy of column j of the matrix, as an (m x 1) sparse matrix (column vector).

val getformat : [> tag ] Obj.t -> Py.Object.t

Format of a matrix representation as a string.

val getmaxprint : [> tag ] Obj.t -> Py.Object.t

Maximum number of elements to display when printed.

val getnnz : ?axis:[ `One | `Zero ] -> [> tag ] Obj.t -> Py.Object.t

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

val getrow : i:Py.Object.t -> [> tag ] Obj.t -> Py.Object.t

Returns a copy of the 'i'th row.

val getrowview : i:Py.Object.t -> [> tag ] Obj.t -> Py.Object.t

Returns a view of the 'i'th row (without copying).

val maximum : other:Py.Object.t -> [> tag ] Obj.t -> Py.Object.t

Element-wise maximum between this and another matrix.

val mean : ?axis:[ `One | `Zero | `PyObject of Py.Object.t ] -> ?dtype:Np.Dtype.t -> ?out:[> `Ndarray ] Np.Obj.t -> [> tag ] Obj.t -> [ `ArrayLike | `Ndarray | `Object ] Np.Obj.t

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.

.. versionadded:: 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.

.. versionadded:: 0.18.0

Returns ------- m : np.matrix

See Also -------- numpy.matrix.mean : NumPy's implementation of 'mean' for matrices

val minimum : other:Py.Object.t -> [> tag ] Obj.t -> Py.Object.t

Element-wise minimum between this and another matrix.

val multiply : other:Py.Object.t -> [> tag ] Obj.t -> Py.Object.t

Point-wise multiplication by another matrix

val nonzero : [> tag ] Obj.t -> Py.Object.t

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))

val power : ?dtype:Py.Object.t -> n:Py.Object.t -> [> tag ] Obj.t -> Py.Object.t

Element-wise power.

val reshape : ?kwargs:(string * Py.Object.t) list -> Py.Object.t list -> [> tag ] Obj.t -> [ `ArrayLike | `Object | `Spmatrix ] Np.Obj.t

reshape(self, shape, order='C', copy=False)

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 : sparse matrix A sparse matrix with the given `shape`, not necessarily of the same format as the current object.

See Also -------- numpy.matrix.reshape : NumPy's implementation of 'reshape' for matrices

val resize : Py.Object.t list -> [> tag ] Obj.t -> Py.Object.t

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.

Parameters ---------- shape : (int, int) number of rows and columns in the new matrix

Notes ----- The semantics are not identical to `numpy.ndarray.resize` or `numpy.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.

val set_shape : shape:Py.Object.t -> [> tag ] Obj.t -> Py.Object.t

See `reshape`.

val setdiag : ?k:int -> values:[> `Ndarray ] Np.Obj.t -> [> tag ] Obj.t -> Py.Object.t

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 if 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 ai,i+k. Default: 0 (the main diagonal).

val sum : ?axis:[ `One | `Zero | `PyObject of Py.Object.t ] -> ?dtype:Np.Dtype.t -> ?out:[> `Ndarray ] Np.Obj.t -> [> tag ] Obj.t -> [ `ArrayLike | `Ndarray | `Object ] Np.Obj.t

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 unless `a` has an integer dtype of less precision than the default platform integer. In that case, if `a` is signed then the platform integer is used while if `a` is unsigned then an unsigned integer of the same precision as the platform integer is used.

.. versionadded:: 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.

.. versionadded:: 0.18.0

Returns ------- sum_along_axis : np.matrix A matrix with the same shape as `self`, with the specified axis removed.

See Also -------- numpy.matrix.sum : NumPy's implementation of 'sum' for matrices

val toarray : ?order:[ `C | `F ] -> ?out:[ `Ndarray of [> `Ndarray ] Np.Obj.t | `T2_D of Py.Object.t ] -> [> tag ] Obj.t -> Py.Object.t

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', indicating the NumPy default of C-ordered. 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 : ndarray, 2-D 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.

val tobsr : ?blocksize:Py.Object.t -> ?copy:Py.Object.t -> [> tag ] Obj.t -> Py.Object.t

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.

val tocoo : ?copy:Py.Object.t -> [> tag ] Obj.t -> Py.Object.t

Convert this matrix to COOrdinate format.

With copy=False, the data/indices may be shared between this matrix and the resultant coo_matrix.

val tocsc : ?copy:Py.Object.t -> [> tag ] Obj.t -> Py.Object.t

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.

val tocsr : ?copy:Py.Object.t -> [> tag ] Obj.t -> Py.Object.t

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.

val todense : ?order:[ `C | `F ] -> ?out:[ `Ndarray of [> `Ndarray ] Np.Obj.t | `T2_D of Py.Object.t ] -> [> tag ] Obj.t -> [> `ArrayLike ] Np.Obj.t

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', indicating the NumPy default of C-ordered. 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 : numpy.matrix, 2-D 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 a `numpy.matrix`), it will be filled with the appropriate values and returned wrapped in a `numpy.matrix` object that shares the same memory.

val todia : ?copy:Py.Object.t -> [> tag ] Obj.t -> Py.Object.t

Convert this matrix to sparse DIAgonal format.

With copy=False, the data/indices may be shared between this matrix and the resultant dia_matrix.

val todok : ?copy:Py.Object.t -> [> tag ] Obj.t -> Py.Object.t

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.

val tolil : ?copy:Py.Object.t -> [> tag ] Obj.t -> Py.Object.t

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.

val transpose : ?axes:Py.Object.t -> ?copy:bool -> [> tag ] Obj.t -> Py.Object.t

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 : `self` with the dimensions reversed.

See Also -------- numpy.matrix.transpose : NumPy's implementation of 'transpose' for matrices

val dtype : t -> Np.Dtype.t

Attribute dtype: get value or raise Not_found if None.

val dtype_opt : t -> Np.Dtype.t option

Attribute dtype: get value as an option.

val shape : t -> Py.Object.t

Attribute shape: get value or raise Not_found if None.

val shape_opt : t -> Py.Object.t option

Attribute shape: get value as an option.

val ndim : t -> int

Attribute ndim: get value or raise Not_found if None.

val ndim_opt : t -> int option

Attribute ndim: get value as an option.

val nnz : t -> Py.Object.t

Attribute nnz: get value or raise Not_found if None.

val nnz_opt : t -> Py.Object.t option

Attribute nnz: get value as an option.

val data : t -> Py.Object.t

Attribute data: get value or raise Not_found if None.

val data_opt : t -> Py.Object.t option

Attribute data: get value as an option.

val rows : t -> Py.Object.t

Attribute rows: get value or raise Not_found if None.

val rows_opt : t -> Py.Object.t option

Attribute rows: get value as an option.

val to_string : t -> string

Print the object to a human-readable representation.

val show : t -> string

Print the object to a human-readable representation.

val pp : Format.formatter -> t -> unit

Pretty-print the object to a formatter.

OCaml

Innovation. Community. Security.