Sparse Arrays

class parallel_statistics.SparseArray(size=None, dtype=<class 'numpy.float64'>)[source]

A sparse 1D array class.

This not complete, and is mainly designed to support the use case in this package. The scipy sparse classes are all focused on matrix applications and did not quite fit

These operations are defined:
  • setting and getting indices

  • Adding another by another SparseArray

  • Subtracting to another by another SparseArray

  • Multiplying by another SparseArray with the same indices

  • Dividing by another SparseArray with the same indices

  • Raising the array to a scalar power

  • Comparing to another SparseArray with the same indices

Examples

>>> s = SparseArray()
>>> s[1000] = 1.0
>>> s[2000] = 2.0
>>> t = s + s
Attributes
ddict

The dictionary of set indices (keys) and values

Methods

count_nonzero()

The number of non-zero array elements

from_dense(dense)

Convert a standard (dense) 1D array into a sparse array, elements with value zero will not be set in the new array.

to_arrays()

Return the indices (keys) and values of elements that have been set.

to_dense()

Make a dense version of the array, just as a plain numpy array.

count_nonzero()[source]

The number of non-zero array elements

Returns
int
classmethod from_dense(dense)[source]

Convert a standard (dense) 1D array into a sparse array, elements with value zero will not be set in the new array.

Parameters
dense: array

1D numpy array to convert to sparse form

Returns
sparse: SparseArray
to_arrays()[source]

Return the indices (keys) and values of elements that have been set.

Returns
indices: array

indices of elements that have been set.

values: array

values of elements that have been set.

to_dense()[source]

Make a dense version of the array, just as a plain numpy array. Un-set values will be zero.

Returns
dense: array

Dense version of array