Participer au site avec un Tip
Rechercher
 

Améliorations / Corrections

Vous avez des améliorations (ou des corrections) à proposer pour ce document : je vous remerçie par avance de m'en faire part, cela m'aide à améliorer le site.

Emplacement :

Description des améliorations :

Vous êtes un professionnel et vous avez besoin d'une formation ? Programmation Python
Les fondamentaux
Voir le programme détaillé
Module « scipy.sparse »

Fonction safely_cast_index_arrays - module scipy.sparse

Signature de la fonction safely_cast_index_arrays

def safely_cast_index_arrays(A, idx_dtype=<class 'numpy.int32'>, msg='') 

Description

help(scipy.sparse.safely_cast_index_arrays)

Safely cast sparse array indices to `idx_dtype`.

Check the shape of `A` to determine if it is safe to cast its index
arrays to dtype `idx_dtype`. If any dimension in shape is larger than
fits in the dtype, casting is unsafe so raise ``ValueError``.
If safe, cast the index arrays to `idx_dtype` and return the result
without changing the input `A`. The caller can assign results to `A`
attributes if desired or use the recast index arrays directly.

Unless downcasting is needed, the original index arrays are returned.
You can test e.g. ``A.indptr is new_indptr`` to see if downcasting occurred.

.. versionadded:: 1.15.0

Parameters
----------
A : sparse array or matrix
    The array for which index arrays should be downcast.
idx_dtype : dtype
    Desired dtype. Should be an integer dtype (default: ``np.int32``).
    Most of scipy.sparse uses either int64 or int32.
msg : string, optional
    A string to be added to the end of the ValueError message
    if the array shape is too big to fit in `idx_dtype`.
    The error message is ``f"<index> values too large for {msg}"``
    It should indicate why the downcasting is needed, e.g. "SuperLU",
    and defaults to f"dtype {idx_dtype}".

Returns
-------
idx_arrays : ndarray or tuple of ndarrays
    Based on ``A.format``, index arrays are returned after casting to `idx_dtype`.
    For CSC/CSR, returns ``(indices, indptr)``.
    For COO, returns ``coords``.
    For DIA, returns ``offsets``.
    For BSR, returns ``(indices, indptr)``.

Raises
------
ValueError
    If the array has shape that would not fit in the new dtype, or if
    the sparse format does not use index arrays.

Examples
--------
>>> import numpy as np
>>> from scipy import sparse
>>> data = [3]
>>> coords = (np.array([3]), np.array([1]))  # Note: int64 arrays
>>> A = sparse.coo_array((data, coords))
>>> A.coords[0].dtype
dtype('int64')

>>> # rescast after construction, raising exception if shape too big
>>> coords = sparse.safely_cast_index_arrays(A, np.int32)
>>> A.coords[0] is coords[0]  # False if casting is needed
False
>>> A.coords = coords  # set the index dtype of A
>>> A.coords[0].dtype
dtype('int32')


Vous êtes un professionnel et vous avez besoin d'une formation ? Coder avec une
Intelligence Artificielle
Voir le programme détaillé