Skip to content

Commit b8f2717

Browse files
authored
DOC: Add array representations to sparse array construction examples (#606)
1 parent 790f6d5 commit b8f2717

File tree

2 files changed

+43
-14
lines changed

2 files changed

+43
-14
lines changed

docs/construct.rst

Lines changed: 42 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,16 @@ matrix:
2020
... [0, 1, 2, 3, 4]]
2121
>>> data = [10, 20, 30, 40, 50]
2222
>>> s = sparse.COO(coords, data, shape=(5, 5))
23-
24-
>>> s.todense()
25-
array([[10, 0, 0, 0, 0],
26-
[ 0, 20, 0, 0, 0],
27-
[ 0, 0, 30, 0, 0],
28-
[ 0, 0, 0, 40, 0],
29-
[ 0, 0, 0, 0, 50]])
23+
>>> s
24+
<COO: shape=(5, 5), dtype=int64, nnz=5, fill_value=0>
25+
0 1 2 3 4
26+
┌ ┐
27+
0 │ 10 │
28+
1 │ 20 │
29+
2 │ 30 │
30+
3 │ 40 │
31+
4 │ 50 │
32+
└ ┘
3033

3134
In general :code:`coords` should be a :code:`(ndim, nnz)` shaped
3235
array. Each row of :code:`coords` contains one dimension of the
@@ -47,6 +50,15 @@ identity matrix:
4750
... [0, 1, 2, 3]]
4851
>>> data = 1
4952
>>> s = sparse.COO(coords, data, shape=(4, 4))
53+
>>> s
54+
<COO: shape=(4, 4), dtype=int64, nnz=4, fill_value=0>
55+
0 1 2 3
56+
┌ ┐
57+
0 │ 1 │
58+
1 │ 1 │
59+
2 │ 1 │
60+
3 │ 1 │
61+
└ ┘
5062

5163
You can, and should, pass in :obj:`numpy.ndarray` objects for
5264
:code:`coords` and :code:`data`.
@@ -61,9 +73,19 @@ explicitly. For example, if we did the following without the
6173

6274
.. code-block:: python
6375

64-
coords = [[0, 3, 2, 1], [4, 1, 2, 0]]
65-
data = [1, 4, 2, 1]
66-
s = COO(coords, data, shape=(5, 5))
76+
>>> coords = [[0, 3, 2, 1], [4, 1, 2, 0]]
77+
>>> data = [1, 4, 2, 1]
78+
>>> s = COO(coords, data, shape=(5, 5))
79+
>>> s
80+
<COO: shape=(5, 5), dtype=int64, nnz=4, fill_value=0>
81+
0 1 2 3 4
82+
┌ ┐
83+
0 │ 1 │
84+
1 │ 1 │
85+
2 │ 2 │
86+
3 │ 4 │
87+
4 │ │
88+
└ ┘
6789

6890
:obj:`COO` arrays support arbitrary fill values. Fill values are the "default"
6991
value, or value to not store. This can be given a value other than zero. For
@@ -73,9 +95,16 @@ with nonzero fill values.
7395

7496
.. code-block:: python
7597

76-
coords = [[0, 1], [1, 0]]
77-
data = [0, 0]
78-
s = COO(coords, data, fill_value=1)
98+
>>> coords = [[0, 1], [1, 0]]
99+
>>> data = [0, 0]
100+
>>> s = COO(coords, data, fill_value=1)
101+
>>> s
102+
<COO: shape=(2, 2), dtype=int64, nnz=2, fill_value=1>
103+
0 1
104+
┌ ┐
105+
0 │ 0 │
106+
1 │ 0 │
107+
└ ┘
79108

80109
From :std:doc:`Scipy sparse matrices <scipy:reference/generated/scipy.sparse.spmatrix>`
81110
---------------------------------------------------------------------------------------

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ tests = [
3535
"pre-commit",
3636
]
3737
tox = ["sparse[tests]", "tox"]
38-
all = ["sparse[docs,tox]"]
38+
all = ["sparse[docs,tox]", "matrepr"]
3939

4040
[project.urls]
4141
Documentation = "https://sparse.pydata.org/"

0 commit comments

Comments
 (0)