In [1]:
Copied!
import os
os.environ["SPARSE_BACKEND"] = "Finch"
import sparse
import numpy as np
import os
os.environ["SPARSE_BACKEND"] = "Finch"
import sparse
import numpy as np
/home/docs/checkouts/readthedocs.org/user_builds/sparse-nd/checkouts/latest/sparse/__init__.py:24: SparseFutureWarning: Changing back-ends is a development feature, please do not rely on it in production. warnings.warn(
--------------------------------------------------------------------------- AttributeError Traceback (most recent call last) Cell In[1], line 5 1 import os 2 3 os.environ["SPARSE_BACKEND"] = "Finch" 4 ----> 5 import sparse 6 7 import numpy as np File ~/checkouts/readthedocs.org/user_builds/sparse-nd/checkouts/latest/sparse/__init__.py:42 39 del _backend_name 41 if _BackendType.Finch == _BACKEND: ---> 42 from sparse.finch_backend import * # noqa: F403 43 from sparse.finch_backend import __all__ 44 elif _BackendType.MLIR == _BACKEND: File ~/checkouts/readthedocs.org/user_builds/sparse-nd/checkouts/latest/sparse/finch_backend/__init__.py:6 3 except ModuleNotFoundError as e: 4 raise ImportError("Finch not installed. Run `pip install sparse[finch]` to enable Finch backend") from e ----> 6 from finch import * # noqa: F403 7 from finch import __all__ as __all__ AttributeError: module 'finch' has no attribute 'Reflector'
Perform Operations¶
Let's create two arrays.
In [2]:
Copied!
rng = np.random.default_rng(42) # Seed for reproducibility
a = sparse.random((3, 3), density=1 / 6, random_state=rng)
b = sparse.random((3, 3), density=1 / 6, random_state=rng)
rng = np.random.default_rng(42) # Seed for reproducibility
a = sparse.random((3, 3), density=1 / 6, random_state=rng)
b = sparse.random((3, 3), density=1 / 6, random_state=rng)
--------------------------------------------------------------------------- NameError Traceback (most recent call last) Cell In[2], line 1 ----> 1 rng = np.random.default_rng(42) # Seed for reproducibility 2 a = sparse.random((3, 3), density=1 / 6, random_state=rng) 3 b = sparse.random((3, 3), density=1 / 6, random_state=rng) NameError: name 'np' is not defined
Now let's matrix multiply them.
In [3]:
Copied!
c = a @ b
c = a @ b
--------------------------------------------------------------------------- NameError Traceback (most recent call last) Cell In[3], line 1 ----> 1 c = a @ b NameError: name 'a' is not defined
And view the result as a (dense) NumPy array.
In [4]:
Copied!
c_dense = c.todense()
c_dense = c.todense()
--------------------------------------------------------------------------- NameError Traceback (most recent call last) Cell In[4], line 1 ----> 1 c_dense = c.todense() NameError: name 'c' is not defined
Now let's do the same for other formats, and compare the results.
In [5]:
Copied!
for format in ["coo", "csr", "csc", "dense"]:
af = sparse.asarray(a, format=format)
bf = sparse.asarray(b, format=format)
cf = af @ bf
np.testing.assert_array_equal(c_dense, cf.todense())
for format in ["coo", "csr", "csc", "dense"]:
af = sparse.asarray(a, format=format)
bf = sparse.asarray(b, format=format)
cf = af @ bf
np.testing.assert_array_equal(c_dense, cf.todense())
--------------------------------------------------------------------------- NameError Traceback (most recent call last) Cell In[5], line 2 1 for format in ["coo", "csr", "csc", "dense"]: ----> 2 af = sparse.asarray(a, format=format) 3 bf = sparse.asarray(b, format=format) 4 cf = af @ bf 5 np.testing.assert_array_equal(c_dense, cf.todense()) NameError: name 'sparse' is not defined