A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://docs.scipy.org/doc/scipy/reference/sparse.migration_to_sparray.html below:

Migration from spmatrix to sparray — SciPy v1.16.1 Manual

Migration from spmatrix to sparray#

This document provides guidance for converting code from sparse matrices to sparse arrays in scipy.sparse.

The change from sparse matrices to sparse arrays mirrors conversion from np.matrix to np.ndarray. Essentially we must move from an all-2D matrix-multiplication-centric matrix object to a 1D or 2D “array” object that supports the matrix multiplication operator and elementwise computation.

Notation: For this guide we denote the sparse array classes generally as sparray and the sparse matrix classes spmatrix. Dense numpy arrays are denoted np.ndarray and dense matrix classes are np.matrix. Supported sparse formats are denoted BSR, COO, CSC, CSR, DIA, DOK, LIL and all formats are supported by both sparray and spmatrix. The term sparse refers to either sparray or spmatrix, while dense refers to either np.ndarray or np.matrix.

Overview and big picture# Recommended steps for migration# Details: construction functions#

These four functions are new and only handle sparrays: block_array, diags_array, eye_array, and random_array. Their signatures are:

def block_array(blocks, format=None, dtype=None):
def diags_array(diagonals, /, *, offsets=0, shape=None, format=None, dtype=None):
def eye_array(m, n=None, *, k=0, dtype=float, format=None):
def random_array(shape, density=0.01, format='coo', dtype=None, rng=None, data_sampler=None):

The random_array function has a shape (2-tuple) arg rather than two integers. And the rng arg defaults to NumPy’s new default_rng(). This differs from the spmatrix rand and random which default to the global RandomState instance. If you don’t care much about these things, leaving it as the default should work fine. If you care about seeding your random numbers, you should probably add a rng=... keyword argument to this call when you switch functions. In summary, to migrate to random_array change the function name, switch the shape argument to a single tuple argument, leave any other parameters as before, and think about what sort of rng= argument should be used, if any.

The diags_array function uses keyword-only rules for arguments. So you have to type the offsets= in front of the offsets arguments. That seems like a pain during migration from using diags, but it helps avoid confusion and eases reading. A single shape parameter replaces two integers for this migration as well.

Existing functions that need careful migration#

These functions return sparray or spmatrix, depending on the input types they receive: kron, kronsum, hstack, vstack, block_diag, tril, and triu. Their signatures are:

def kron(A, B, format=None):
def kronsum(A, B, format=None):
def hstack(blocks, format=None, dtype=None):
def vstack(blocks, format=None, dtype=None):
def block_diag(mats, format=None, dtype=None):
def tril(A, k=0, format=None):
def triu(A, k=0, format=None):

Use of these functions should be examined and inputs adjusted to ensure return values are sparrays. And in turn the outputs should be treated as sparrays. To return sparrays, at least one input must be an sparray. If you use list-of-lists or numpy arrays as input you should convert one of them to a sparse array to get sparse arrays out.

Functions that changed names for the migration#
Details: shape changes and reductions# Removed methods and attributes# Use tests to find * and ** spots# Index Array DTypes#

If you provide compressed indices to a constructor, e.g. csr_array((data, indices, indptr)) sparse arrays set the index dtype by only checking the index arrays dtype, while sparse matrices check the index values too and may downcast to int32 (see gh-18509 for more details). This means you may get int64 indexing when you used to get int32. You can control this by setting the dtype before instantiating, or by recasting after construction.

Two sparse utility functions can help with handling the index dtype. Use get_index_dtype(arrays, maxval, check_contents) while creating indices to find an appropriate dtype (int32 or int64) to use for your compressed indices.

Use safely_cast_index_arrays(A, idx_dtype) for recasting after construction, while making sure you con’t create overflows during downcasting. This function doesn’t actually change the input array. The cast arrays are returned. And copies are only made when needed. So you can check if casting was done using if indices is not A.indices:

The function signatures are:

def get_index_dtype(arrays=(), maxval=None, check_contents=False):
def safely_cast_index_arrays(A, idx_dtype=np.int32, msg=""):

Example idioms include the following for get_index_dtype:

.. code-block:: python

    # select index dtype before construction based on shape
    shape = (3, 3)
    idx_dtype = scipy.sparse.get_index_dtype(maxval=max(shape))
    indices = np.array([0, 1, 0], dtype=idx_dtype)
    indptr = np.arange(3, dtype=idx_dtype)
    A = csr_array((data, indices, indptr), shape=shape)

and for safely_cast_index_arrays:

.. code-block:: python

    # rescast after construction, raising exception if shape too big
    indices, indptr = scipy.sparse.safely_cast_index_arrays(B, np.int32)
    B.indices, B.indptr = indices, indptr
Other#

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4