RaggedArray
A RaggedArray is a 2d array, where the lengths of the rows can differ. These arrays behaves as much as possible like numpy array, meaning that you can index it like a numpy array, call unfuncs on them and also call a limited set of array functions on them.
API documentation
- class npstructures.RaggedArray(data: _SupportsArray[dtype[Any]] | _NestedSequence[_SupportsArray[dtype[Any]]] | bool | int | float | complex | str | bytes | _NestedSequence[bool | int | float | complex | str | bytes], shape: List[int] | RaggedShape | RaggedView | Shape | None = None, dtype: dtype[Any] | None | type[Any] | _SupportsDType[dtype[Any]] | str | tuple[Any, int] | tuple[Any, SupportsIndex | Sequence[SupportsIndex]] | list[Any] | _DTypeDict | tuple[Any, Any] = None, safe_mode: bool = True)[source]
Class to represent 2d arrays with differing row lengths
Provides objects that behaves similar to numpy ndarrays, but can represent arrays with differing row lengths. Numpy-like functionality is provided in three ways.
ufuncs are supported, so addition, multiplication, sin etc. works just like numpy arrays
Indexing works similar to numpy arrays. Slice objects, index lists and boolean arrays etc.
Some numpy functions like np.concatenate, np.sum, np.nonzero are implemented.
See the examples for simple demonstrations.
- Parameters:
- datanested list or array_like
the nested list to be converted, or (if shape is provided) a continous array of values
- shapelist or Shape, optional
the shape of the ragged array, or list of row lenghts
- dtypeoptional
the data type to use for the array
Examples
>>> ra = RaggedArray([[2, 4, 8], [3, 2], [5, 7, 3, 1]]) >>> ra+1 RaggedArray([[3, 5, 9], [4, 3], [6, 8, 4, 2]]) >>> ra*2 RaggedArray([[4, 8, 16], [6, 4], [10, 14, 6, 2]]) >>> ra[1] array([3, 2]) >>> ra[0:2] RaggedArray([[2, 4, 8], [3, 2]]) >>> np.nonzero(ra>3) (array([0, 0, 2, 2]), array([1, 2, 0, 1]))
- Attributes:
- shapeRaggedShape
the shape (row-lengths) of the array
- sizeint
the total size of the array
- dtype
the data-type of the array
- astype(dtype: dtype[Any] | None | type[Any] | _SupportsDType[dtype[Any]] | str | tuple[Any, int] | tuple[Any, SupportsIndex | Sequence[SupportsIndex]] | list[Any] | _DTypeDict | tuple[Any, Any]) RaggedArray [source]
Return ragged array with underlying data changed to dtype
- Parameters:
- dtypenpt.DTypeLike
- Returns:
- ‘RaggedArray’
- cumsum(axis: int | None = None, dtype: dtype[Any] | None | type[Any] | _SupportsDType[dtype[Any]] | str | tuple[Any, int] | tuple[Any, SupportsIndex | Sequence[SupportsIndex]] | list[Any] | _DTypeDict | tuple[Any, Any] = None) RaggedArray [source]
Return an array with cumulative sums along the given axis
- Parameters:
- axisint
- dtypenpt.DTypeLike
- Returns:
- RaggedArray
- classmethod load(filename: str) RaggedArray [source]
Loads a ragged array from file
- Parameters:
- filenamestr
name of the file
- Returns:
- RaggedArray
The ragged array loaded from file
- nonzero() Tuple[_SupportsArray[dtype[Any]] | _NestedSequence[_SupportsArray[dtype[Any]]] | bool | int | float | complex | str | bytes | _NestedSequence[bool | int | float | complex | str | bytes]] [source]
Return the indices of all nonzero entries in the array
- Returns:
- Tuple[npt.ArrayLike]
- save(filename)[source]
Saves the ragged array to file using np.savez
- Parameters:
- filenamestr
name of the file
- npstructures.arrayfunctions.concatenate(ragged_arrays: List[RaggedArray], axis: int = 0) RaggedArray [source]
Concatenate a set of raggedarrays along the given axis
- Parameters:
- ragged_arraysList[RaggedArray]
- axisint
- Returns:
- RaggedArray
- npstructures.arrayfunctions.diff(ragged_array: RaggedArray, n: int = 1, axis: int = -1) RaggedArray [source]
Return diffs for each row in a raggedarray
- Parameters:
- ragged_arrayRaggedArray
- nint
- axisint
- npstructures.arrayfunctions.zeros_like(ragged_array: RaggedArray, dtype: dtype[Any] | None | type[Any] | _SupportsDType[dtype[Any]] | str | tuple[Any, int] | tuple[Any, SupportsIndex | Sequence[SupportsIndex]] | list[Any] | _DTypeDict | tuple[Any, Any] = None, shape: Tuple[int] | None = None) RaggedArray [source]
Return a raggedarray with the same shape and dtype as raggedarray filled with zeros
- Parameters:
- ragged_arrayRaggedArray
- dtypenpt.DTypeLike
- shapeTuple[int]
- Returns:
- RaggedArray
- npstructures.arrayfunctions.ones_like(ragged_array: RaggedArray, dtype: dtype[Any] | None | type[Any] | _SupportsDType[dtype[Any]] | str | tuple[Any, int] | tuple[Any, SupportsIndex | Sequence[SupportsIndex]] | list[Any] | _DTypeDict | tuple[Any, Any] = None, shape: Tuple[int] | None = None) RaggedArray [source]
Return a raggedarray with the same shape and dtype as raggedarray filled with ones
- Parameters:
- ragged_arrayRaggedArray
- dtypenpt.DTypeLike
- shapeTuple[int]
- Returns:
- RaggedArray
- npstructures.arrayfunctions.empty_like(ragged_array: RaggedArray, dtype: dtype[Any] | None | type[Any] | _SupportsDType[dtype[Any]] | str | tuple[Any, int] | tuple[Any, SupportsIndex | Sequence[SupportsIndex]] | list[Any] | _DTypeDict | tuple[Any, Any] = None, shape: Tuple[int] | None = None) RaggedArray [source]
Return an empty raggedarray with the same shape and dtype as raggedarray
- Parameters:
- ragged_arrayRaggedArray
- dtypenpt.DTypeLike
- shapeTuple[int]
- Returns:
- RaggedArray
- npstructures.arrayfunctions.where(ragged_mask: RaggedArray, x: RaggedArray | None = None, y: RaggedArray | None = None) RaggedArray [source]
Perform an ifelse (tertiary operator) on a raggedarray
Inicies where ragged_mask is True gets the corresponding value from x. Where False it gets from y
- Parameters:
- ragged_maskRaggedArray
- xRaggedArray
- yRaggedArray
- Returns:
- RaggedArray
- npstructures.arrayfunctions.unique(ragged_array: RaggedArray, axis: int | None = None, return_counts: bool = False) RaggedArray | Tuple[RaggedArray] [source]
Get the unqiue values from ragged_array. If return_counts then also return the number of elemtents with each value
- Parameters:
- ragged_arrayRaggedArray
- axisint
- return_countsbool
- Returns:
- Union[RaggedArray, Tuple[RaggedArray]]