Render sparse and dense matrices to HTML and Latex, with a Jupyter extension.
import scipy.sparse
import numpy as np
np.random.seed(123)
# so matrepr can be imported from the source tree.
import sys
sys.path.insert(0, '..')
from matrepr import mdisplay, mprint
%load_ext matrepr
scipy.sparse.random(6, 6, density=0.5)
0 | 1 | 2 | 3 | 4 | 5 | |
---|---|---|---|---|---|---|
0 | 0.611 | 0.4337 | 0.4309 | 0.4264 | ||
1 | 0.3123 | 0.323 | 0.0921 | |||
2 | 0.7224 | 0.5018 | 0.2937 | |||
3 | 0.4258 | |||||
4 | 0.624 | 0.9442 | ||||
5 | 0.2283 | 0.3618 | 0.631 | 0.8934 | 0.4937 |
mat = np.random.random((10, 10))
mdisplay(mat, floatfmt=".2f")
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | |
---|---|---|---|---|---|---|---|---|---|---|
0 | 0.12 | 0.32 | 0.41 | 0.87 | 0.25 | 0.48 | 0.99 | 0.52 | 0.61 | 0.12 |
1 | 0.83 | 0.60 | 0.55 | 0.34 | 0.30 | 0.42 | 0.68 | 0.88 | 0.51 | 0.67 |
2 | 0.59 | 0.62 | 0.67 | 0.84 | 0.08 | 0.76 | 0.24 | 0.19 | 0.57 | 0.10 |
3 | 0.89 | 0.63 | 0.72 | 0.02 | 0.59 | 0.56 | 0.16 | 0.15 | 0.70 | 0.32 |
4 | 0.69 | 0.55 | 0.39 | 0.93 | 0.84 | 0.36 | 0.04 | 0.30 | 0.40 | 0.70 |
5 | 1.00 | 0.36 | 0.76 | 0.59 | 0.69 | 0.15 | 0.40 | 0.24 | 0.34 | 0.51 |
6 | 0.67 | 0.11 | 0.13 | 0.32 | 0.66 | 0.85 | 0.55 | 0.85 | 0.38 | 0.32 |
7 | 0.35 | 0.17 | 0.83 | 0.34 | 0.55 | 0.58 | 0.52 | 0.00 | 0.99 | 0.91 |
8 | 0.21 | 0.29 | 0.52 | 0.90 | 0.98 | 0.26 | 0.56 | 0.81 | 0.39 | 0.73 |
9 | 0.16 | 0.60 | 0.87 | 0.98 | 0.08 | 0.43 | 0.20 | 0.45 | 0.55 | 0.09 |
mat = np.random.random((500, 25))
mdisplay(mat, floatfmt=".2f", max_rows=15, max_cols=25)
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | 0.30 | 0.93 | 0.57 | 0.46 | 0.75 | 0.74 | 0.05 | 0.71 | 0.84 | 0.17 | 0.78 | 0.29 | 0.31 | 0.67 | 0.11 | 0.66 | 0.89 | 0.70 | 0.44 | 0.44 | 0.77 | 0.57 | 0.08 | 0.58 | 0.81 |
1 | 0.34 | 0.93 | 0.75 | 0.57 | 0.75 | 0.08 | 0.86 | 0.82 | 0.91 | 0.13 | 0.08 | 0.14 | 0.40 | 0.42 | 0.56 | 0.12 | 0.20 | 0.81 | 0.47 | 0.81 | 0.01 | 0.55 | 0.93 | 0.58 | 0.21 |
2 | 0.72 | 0.38 | 0.67 | 0.03 | 0.64 | 0.03 | 0.74 | 0.47 | 0.12 | 0.54 | 0.07 | 0.65 | 1.00 | 0.77 | 0.57 | 0.10 | 0.70 | 0.66 | 0.05 | 0.79 | 0.52 | 0.43 | 0.79 | 0.41 | 0.48 |
3 | 0.18 | 0.32 | 0.85 | 0.19 | 0.42 | 0.99 | 0.24 | 0.92 | 0.92 | 0.09 | 0.46 | 0.50 | 0.31 | 0.05 | 0.24 | 0.10 | 0.24 | 0.81 | 0.89 | 0.04 | 0.30 | 0.98 | 0.54 | 0.63 | 0.01 |
4 | 0.48 | 0.99 | 0.38 | 0.10 | 0.46 | 0.96 | 0.34 | 0.80 | 0.80 | 0.21 | 0.44 | 0.72 | 0.41 | 0.19 | 0.97 | 0.65 | 0.87 | 0.03 | 0.27 | 0.50 | 0.07 | 0.99 | 0.24 | 0.37 | 0.21 |
5 | 0.11 | 0.23 | 0.30 | 0.63 | 0.28 | 0.36 | 0.01 | 0.37 | 0.53 | 0.16 | 0.60 | 0.29 | 0.63 | 0.03 | 0.89 | 0.02 | 0.13 | 0.78 | 0.05 | 0.71 | 0.97 | 0.87 | 0.71 | 0.96 | 0.43 |
6 | 0.87 | 0.36 | 0.93 | 0.15 | 0.94 | 0.83 | 0.85 | 0.12 | 0.60 | 0.02 | 0.72 | 0.01 | 0.08 | 0.23 | 0.88 | 0.36 | 0.54 | 0.57 | 0.23 | 0.57 | 0.66 | 0.30 | 0.42 | 0.45 | 0.93 |
⋮ | ⋮ | ⋮ | ⋮ | ⋮ | ⋮ | ⋮ | ⋮ | ⋮ | ⋮ | ⋮ | ⋮ | ⋮ | ⋮ | ⋮ | ⋮ | ⋮ | ⋮ | ⋮ | ⋮ | ⋮ | ⋮ | ⋮ | ⋮ | ⋮ | |
493 | 0.21 | 0.03 | 0.82 | 0.02 | 0.41 | 0.69 | 0.87 | 0.63 | 0.34 | 0.32 | 0.19 | 0.33 | 0.09 | 0.99 | 0.75 | 0.72 | 0.19 | 0.15 | 0.66 | 0.59 | 0.77 | 0.93 | 0.56 | 0.68 | 0.63 |
494 | 0.71 | 0.82 | 0.71 | 0.60 | 0.61 | 0.62 | 0.70 | 0.82 | 0.05 | 0.46 | 0.31 | 0.28 | 0.36 | 0.01 | 0.52 | 0.69 | 0.26 | 0.57 | 0.05 | 0.46 | 0.66 | 0.62 | 0.37 | 0.61 | 0.14 |
495 | 0.62 | 0.21 | 0.61 | 0.30 | 0.66 | 0.29 | 0.67 | 0.24 | 0.09 | 0.41 | 0.96 | 0.80 | 0.21 | 0.79 | 0.10 | 0.18 | 0.16 | 0.99 | 0.96 | 0.57 | 0.56 | 0.43 | 0.91 | 0.09 | 0.36 |
496 | 0.61 | 0.91 | 0.76 | 0.41 | 0.81 | 0.58 | 0.54 | 0.15 | 0.15 | 0.08 | 0.43 | 0.51 | 0.25 | 0.92 | 0.10 | 0.66 | 0.61 | 0.46 | 0.87 | 0.74 | 0.34 | 0.26 | 0.80 | 0.13 | 0.75 |
497 | 0.21 | 0.26 | 0.87 | 0.69 | 0.47 | 0.02 | 0.15 | 0.31 | 0.37 | 0.46 | 0.07 | 0.10 | 0.07 | 0.15 | 0.95 | 0.12 | 0.14 | 0.15 | 0.73 | 0.33 | 0.72 | 0.44 | 0.04 | 0.67 | 0.11 |
498 | 0.47 | 0.39 | 0.52 | 0.49 | 0.71 | 0.58 | 0.14 | 0.98 | 0.35 | 0.83 | 0.18 | 0.03 | 0.93 | 0.87 | 0.98 | 0.08 | 0.74 | 0.36 | 0.70 | 0.06 | 0.89 | 0.52 | 0.50 | 0.15 | 0.95 |
499 | 0.03 | 0.32 | 0.40 | 0.76 | 0.43 | 0.12 | 0.92 | 0.49 | 0.23 | 0.23 | 0.29 | 0.33 | 0.23 | 0.57 | 0.23 | 0.48 | 0.41 | 0.54 | 0.05 | 0.34 | 0.92 | 0.85 | 0.70 | 0.36 | 0.52 |
cities = ["Boston", "Buffalo", "Chicago", "Cleveland", "Dallas", "Denver"]
distances = np.array([
[None, 457, 983, 639, 1815, 1991],
[457, None, 536, 192, 1387, 1561],
[983, 536, None, 344, 931, 1050],
[639, 192, 344, None, 1205, 1369],
[1815, 1387, 931, 1205, None, 801],
[1991, 1561, 1050, 1369, 801, None],
])
mdisplay(distances, title=None, row_labels=cities, col_labels=cities, fill_value="--")
Boston | Buffalo | Chicago | Cleveland | Dallas | Denver | |
---|---|---|---|---|---|---|
Boston | -- | 457 | 983 | 639 | 1815 | 1991 |
Buffalo | 457 | -- | 536 | 192 | 1387 | 1561 |
Chicago | 983 | 536 | -- | 344 | 931 | 1050 |
Cleveland | 639 | 192 | 344 | -- | 1205 | 1369 |
Dallas | 1815 | 1387 | 931 | 1205 | -- | 801 |
Denver | 1991 | 1561 | 1050 | 1369 | 801 | -- |
vec = np.random.random((1000,))
mdisplay(vec)
0 | 1 | 2 | 3 | 4 | 5 | 6 | 993 | 994 | 995 | 996 | 997 | 998 | 999 | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0.1708 | 0.7365 | 0.4166 | 0.8764 | 0.964 | 0.7809 | 0.1369 | ⋯ | 0.7704 | 0.1608 | 0.816 | 0.008367 | 0.2858 | 0.6423 | 0.8979 |
mprint(vec, indices=False, title=False)
[0.1708, 0.7365, 0.4166, 0.8764, 0.651, ..., 0.008367, 0.2858, 0.6423, 0.8979]
Sparse multidimensional tensors are presented as a list of tuples.
import sparse
tensor_4d = sparse.random((1000, 10, 10, 10, 10), density=0.1234)
mdisplay(tensor_4d)
0 | 1 | 2 | 3 | 4 | val | |
---|---|---|---|---|---|---|
0 | 0 | 0 | 0 | 0 | 1 | 0.8415 |
1 | 0 | 0 | 0 | 1 | 6 | 0.6691 |
2 | 0 | 0 | 0 | 2 | 1 | 0.6162 |
3 | 0 | 0 | 0 | 2 | 2 | 0.09681 |
4 | 0 | 0 | 0 | 4 | 7 | 0.4684 |
⋮ | ⋮ | ⋮ | ⋮ | ⋮ | ⋮ | |
1233995 | 999 | 9 | 9 | 6 | 7 | 0.9675 |
1233996 | 999 | 9 | 9 | 7 | 0 | 0.2194 |
1233997 | 999 | 9 | 9 | 7 | 7 | 0.188 |
1233998 | 999 | 9 | 9 | 8 | 5 | 0.03111 |
1233999 | 999 | 9 | 9 | 9 | 9 | 0.1041 |
mprint(tensor_4d)
<shape=(1000,10,10,10,10), 1234000 'float64' elements, coo, fill_value=0.0, read-only, 56.5MiB> 0 1 2 3 4 val 0 ( 0, 0, 0, 0, 1, 0.8415 ) 1 ( 0, 0, 0, 1, 6, 0.6691 ) 2 ( 0, 0, 0, 2, 1, 0.6162 ) 3 ( 0, 0, 0, 2, 2, 0.09681) 4 ( 0, 0, 0, 4, 7, 0.4684 ) ( :, :, :, :, :, : ) 1233995 (999, 9, 9, 6, 7, 0.9675 ) 1233996 (999, 9, 9, 7, 0, 0.2194 ) 1233997 (999, 9, 9, 7, 7, 0.188 ) 1233998 (999, 9, 9, 8, 5, 0.03111) 1233999 (999, 9, 9, 9, 9, 0.1041 )
dense_4d = np.random.random((3, 3, 3, 3))
mdisplay(dense_4d, floatfmt=".2f")
0 | 1 | 2 | ||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 |
|
|
|
|||||||||||||||||||||||||||
1 |
|
|
|
|||||||||||||||||||||||||||
2 |
|
|
|
import graphblas as gb
edges = gb.Matrix.from_coo(
[3, 0, 3, 5, 6, 0, 6, 1, 6, 2, 4, 1],
[0, 1, 2, 2, 2, 3, 3, 4, 4, 5, 5, 6],
[3, 2, 3, 1, 5, 3, 7, 8, 3, 1, 7, 4],
nrows=7, ncols=7
)
vertices = gb.Vector.from_coo([range(7)], [f"V{i+1}" for i in range(7)], size=7)
mdisplay(edges, row_labels=vertices, col_labels=vertices, title=False)
V1 | V2 | V3 | V4 | V5 | V6 | V7 | |
---|---|---|---|---|---|---|---|
V1 | 2 | 3 | |||||
V2 | 8 | 4 | |||||
V3 | 1 | ||||||
V4 | 3 | 3 | |||||
V5 | 7 | ||||||
V6 | 1 | ||||||
V7 | 5 | 7 | 3 |
# You may mix types if the datastructure allows, as a Python list does
mat = [
[scipy.sparse.random(2, 2, density=0.8), [[1, 2], [3, 4]]],
[np.array([[1, 2], [3, 4]]), scipy.sparse.random(2, 2, density=0.6)]
]
mdisplay(mat, floatfmt=".2f")
0 | 1 | |||||||||
---|---|---|---|---|---|---|---|---|---|---|
0 |
|
|
||||||||
1 |
|
|
scipy.sparse.eye(100_000_000, format="csr")
0 | 1 | 2 | 3 | 4 | 5 | 6 | 99999993 | 99999994 | 99999995 | 99999996 | 99999997 | 99999998 | 99999999 | ||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | 1 | ⋯ | |||||||||||||
1 | 1 | ⋯ | |||||||||||||
2 | 1 | ⋯ | |||||||||||||
3 | 1 | ⋯ | |||||||||||||
4 | 1 | ⋯ | |||||||||||||
⋮ | ⋮ | ⋮ | ⋮ | ⋮ | ⋮ | ⋮ | ⋱ | ⋮ | ⋮ | ⋮ | ⋮ | ⋮ | ⋮ | ⋮ | |
99999995 | ⋯ | 1 | |||||||||||||
99999996 | ⋯ | 1 | |||||||||||||
99999997 | ⋯ | 1 | |||||||||||||
99999998 | ⋯ | 1 | |||||||||||||
99999999 | ⋯ | 1 |
r = scipy.sparse.random(10000, 10000, density=0.23456789, format="csr")
r
0 | 1 | 2 | 3 | 4 | 5 | 6 | 9993 | 9994 | 9995 | 9996 | 9997 | 9998 | 9999 | ||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | 0.5248 | 0.2646 | 0.03848 | 0.591 | ⋯ | 0.8166 | 0.8947 | 0.3634 | |||||||
1 | 0.6212 | 0.166 | 0.7899 | ⋯ | 0.6492 | ||||||||||
2 | 0.6416 | ⋯ | |||||||||||||
3 | ⋯ | 0.7153 | 0.9322 | ||||||||||||
4 | ⋯ | 0.1142 | |||||||||||||
⋮ | ⋮ | ⋮ | ⋮ | ⋮ | ⋮ | ⋮ | ⋱ | ⋮ | ⋮ | ⋮ | ⋮ | ⋮ | ⋮ | ⋮ | |
9995 | 0.9442 | ⋯ | 0.2925 | 0.4961 | |||||||||||
9996 | 0.463 | 0.3782 | 0.07874 | ⋯ | 0.6619 | ||||||||||
9997 | 0.2867 | 0.2823 | ⋯ | 0.31 | |||||||||||
9998 | 0.2122 | 0.4611 | ⋯ | 0.6378 | 0.2867 | ||||||||||
9999 | 0.2333 | 0.4093 | ⋯ |
Some sparse formats allow multiple values with the same indices.
row = [0, 1, 1, 2, 2, 2]
col = [0, 1, 1, 2, 2, 2]
val = [1, 2.1, 2.2, 3.1, 3.2, 3.3]
dupes = scipy.sparse.coo_array((val, (row, col)), shape=(3, 3))
mdisplay(dupes)
0 | 1 | 2 | |
---|---|---|---|
0 | 1 | ||
1 | 2.1 2.2 |
||
2 | 3.1 3.2 3.3 |