.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/vietoris_rips/plot_matrices.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_vietoris_rips_plot_matrices.py: .. _vietoris_rips_matrices: Sparse Matrices ======================================== Sparse matrices in applied topology present several challenges. - They are often large, consuming huge amounts of memory. - They are indexed by simplices, cubes, of other objects, rather than integers - They have a variety of different coefficient rings This tutorial introduces some highly effective OAT tools to meet these challenges. We will focus on the boundary matrix of a Vietoris-Rips complex for illustration, but many of the methods carry over to different types of sparse matrix offered by OAT. .. GENERATED FROM PYTHON SOURCE LINES 17-25 .. code-block:: Python import oat_python as oat from fractions import Fraction import numpy as np import pandas as pd import plotly.graph_objects as go .. GENERATED FROM PYTHON SOURCE LINES 26-28 Vietoris Rips Complex -------------------------- .. GENERATED FROM PYTHON SOURCE LINES 30-31 Create a Vietoris-Rips complex from a points of points .. GENERATED FROM PYTHON SOURCE LINES 31-35 .. code-block:: Python points = np.random.rand(5, 3) dissimilarity_matrix = oat.dissimilarity.sparse_matrix_for_points(points, max_dissimilarity=np.inf) vietoris_rips = oat.core.vietoris_rips.VietorisRipsComplex(dissimilarity_matrix) .. GENERATED FROM PYTHON SOURCE LINES 36-38 Simplices ---------------- .. GENERATED FROM PYTHON SOURCE LINES 40-41 List all simplices in a given dimension(s) .. GENERATED FROM PYTHON SOURCE LINES 41-43 .. code-block:: Python vietoris_rips.simplices_for_dimensions(dimensions=[0,1]) .. raw:: html
simplex filtration
0 (0,) 0.000000
1 (1,) 0.000000
2 (2,) 0.000000
3 (3,) 0.000000
4 (4,) 0.000000
5 (0, 2) 0.370513
6 (2, 4) 0.619095
7 (1, 4) 0.632122
8 (1, 3) 0.660166
9 (3, 4) 0.682666
10 (1, 2) 0.726618
11 (2, 3) 0.830129
12 (0, 1) 0.877768
13 (0, 3) 0.901882
14 (0, 4) 0.940755


.. GENERATED FROM PYTHON SOURCE LINES 44-45 Look up a simplex's filtration value .. GENERATED FROM PYTHON SOURCE LINES 45-47 .. code-block:: Python vietoris_rips.filtration_value(simplex=[0, 1, 2]) .. rst-class:: sphx-glr-script-out .. code-block:: none 0.8777684218544266 .. GENERATED FROM PYTHON SOURCE LINES 48-49 An error is returned if the simplex is not in the complex, or if it is not formatted as a strictly ascending sequence of nonnegative integers .. GENERATED FROM PYTHON SOURCE LINES 49-70 .. code-block:: Python try: vietoris_rips.filtration_value(simplex=(0, 0,)) except Exception as e: print(f"Error: {e}") try: vietoris_rips.filtration_value(simplex=(1, 0)) except Exception as e: print(f"Error: {e}") try: vietoris_rips.filtration_value(simplex=(0,100)) except Exception as e: print(f"Error: {e}") try: vietoris_rips.filtration_value(simplex=(-1,)) except Exception as e: print(f"Error: {e}") .. rst-class:: sphx-glr-script-out .. code-block:: none Error: Simplex [0, 0] is not strictly sorted. Error: Simplex [1, 0] is not strictly sorted. Error: The input [0, 100] is not a valid simplex in this Vietoris-Rips complex. Error: out of range integral type conversion attempted .. GENERATED FROM PYTHON SOURCE LINES 71-73 Boundary matrix oracle (memory light) --------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 75-79 We begin by creating a *boundary matrix oracle*. To conserve memory, this object stores only a copy of the dissimilarity matrix in memory, and computes computes rows, columns, and entries of the boundary matrix on demand. .. GENERATED FROM PYTHON SOURCE LINES 81-83 .. code-block:: Python boundary_matrix_oracle = vietoris_rips.boundary_matrix_oracle() .. GENERATED FROM PYTHON SOURCE LINES 84-86 Rows and columns ----------------------- .. GENERATED FROM PYTHON SOURCE LINES 88-90 .. code-block:: Python boundary_matrix_oracle.row_for_simplex([0,1]) .. raw:: html
simplex filtration coefficient
0 (0, 1, 2) 0.877768 1
1 (0, 1, 3) 0.901882 1
2 (0, 1, 4) 0.940755 1


.. GENERATED FROM PYTHON SOURCE LINES 91-93 .. code-block:: Python boundary_matrix_oracle.column_for_simplex([0,1]) .. raw:: html
simplex filtration coefficient
0 (0,) 0.0 -1
1 (1,) 0.0 1


.. GENERATED FROM PYTHON SOURCE LINES 94-96 Entries ------------------ .. GENERATED FROM PYTHON SOURCE LINES 98-99 Look up an entry .. GENERATED FROM PYTHON SOURCE LINES 99-104 .. code-block:: Python boundary_matrix_oracle.entry_for_row_and_column( row_simplex=(0,1), column_simplex=(0,1,2) ) .. rst-class:: sphx-glr-script-out .. code-block:: none Fraction(1, 1) .. GENERATED FROM PYTHON SOURCE LINES 105-106 Keywords don't have to be explicit .. GENERATED FROM PYTHON SOURCE LINES 106-111 .. code-block:: Python boundary_matrix_oracle.entry_for_row_and_column( [0,1], [0,1,2] ) .. rst-class:: sphx-glr-script-out .. code-block:: none Fraction(1, 1) .. GENERATED FROM PYTHON SOURCE LINES 112-114 Boundaries and coboundaries ----------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 116-117 Define a vector as a linear combination of simplices .. GENERATED FROM PYTHON SOURCE LINES 117-125 .. code-block:: Python vector = { "simplex": [(0,), (1,), (0, 1), (1, 2)], "coefficient": [Fraction(1, 2), Fraction(-3, 4), Fraction(2, 3), Fraction(5, 6)] } vector = pd.DataFrame(vector) vector .. raw:: html
simplex coefficient
0 (0,) 1/2
1 (1,) -3/4
2 (0, 1) 2/3
3 (1, 2) 5/6


.. GENERATED FROM PYTHON SOURCE LINES 126-127 Multiply the vector with the boundary matrix as a column vector (i.e. compute the boundary) .. GENERATED FROM PYTHON SOURCE LINES 127-129 .. code-block:: Python boundary_matrix_oracle.boundary_for_chain(vector) .. raw:: html
simplex filtration coefficient
0 (0,) 0.0 -2/3
1 (1,) 0.0 -1/6
2 (2,) 0.0 5/6


.. GENERATED FROM PYTHON SOURCE LINES 130-131 Multiply the vector with the boundary matrix as a row vector (i.e. compute the coboundary) .. GENERATED FROM PYTHON SOURCE LINES 131-133 .. code-block:: Python boundary_matrix_oracle.coboundary_for_cochain(vector) .. raw:: html
simplex filtration coefficient
0 (0, 2) 0.370513 -1/2
1 (1, 4) 0.632122 3/4
2 (1, 3) 0.660166 3/4
3 (1, 2) 0.726618 3/4
4 (1, 2, 4) 0.726618 5/6
5 (1, 2, 3) 0.830129 5/6
6 (0, 1) 0.877768 -5/4
7 (0, 1, 2) 0.877768 3/2
8 (0, 1, 3) 0.901882 2/3
9 (0, 3) 0.901882 -1/2
10 (0, 1, 4) 0.940755 2/3
11 (0, 4) 0.940755 -1/2


.. GENERATED FROM PYTHON SOURCE LINES 134-138 Row and column numbers ------------------------------- Boundary matrices are naturally indexed by simplices, not but row/column numbers. However, it's common to represent submatrices of the boundary matrix with numbered rows and columns. OAT offers a tool to help translate between simplices and numbers. .. GENERATED FROM PYTHON SOURCE LINES 140-142 Assign submatrix row and column numbers ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ .. GENERATED FROM PYTHON SOURCE LINES 144-145 Initialize the index tool with no row or column indices .. GENERATED FROM PYTHON SOURCE LINES 145-154 .. code-block:: Python submatrix_index_tool = vietoris_rips.submatrix_index_tool() oat.plot.display_dataframes_side_by_side( submatrix_index_tool.row_indices(), submatrix_index_tool.column_indices(), titles=["Row indices", "Column indices"] ) .. raw:: html

Row indices

simplex filtration

Column indices

simplex filtration


.. GENERATED FROM PYTHON SOURCE LINES 155-156 Provide custom lists of row and column indices .. GENERATED FROM PYTHON SOURCE LINES 156-165 .. code-block:: Python submatrix_index_tool.set_row_indices([(0,1), (1,2),]) submatrix_index_tool.set_column_indices([(0,1,2), (0,1,3)]) oat.plot.display_dataframes_side_by_side( submatrix_index_tool.row_indices(), submatrix_index_tool.column_indices(), titles=["Row indices", "Column indices"] ) .. raw:: html

Row indices

simplex filtration
0 (0, 1) 0.877768
1 (1, 2) 0.726618

Column indices

simplex filtration
0 (0, 1, 2) 0.877768
1 (0, 1, 3) 0.901882


.. GENERATED FROM PYTHON SOURCE LINES 166-167 An error will be returned if the list of row (respectively) column indices contains duplicate entries. .. GENERATED FROM PYTHON SOURCE LINES 167-172 .. code-block:: Python try: submatrix_index_tool.set_row_indices([(0,1), (0,1)]) except Exception as e: print(f"Error: {e}") .. rst-class:: sphx-glr-script-out .. code-block:: none Error: User-provided indices include one or more duplicates, including WeightedSimplex { weight: OrderedFloat(0.8777684218544266), vertices: [0, 1] }. .. GENERATED FROM PYTHON SOURCE LINES 173-175 Select indices by simplex dimension ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ .. GENERATED FROM PYTHON SOURCE LINES 177-178 Indexing is managed by this `submatrix_index_tool` .. GENERATED FROM PYTHON SOURCE LINES 178-186 .. code-block:: Python submatrix_index_tool = vietoris_rips.submatrix_index_tool( row_dimensions=[0, 1], column_dimensions=[1,2], ) print("\nThe first few row indices:") submatrix_index_tool.row_indices().head() .. rst-class:: sphx-glr-script-out .. code-block:: none The first few row indices: .. raw:: html
simplex filtration
0 (0,) 0.0
1 (1,) 0.0
2 (2,) 0.0
3 (3,) 0.0
4 (4,) 0.0


.. GENERATED FROM PYTHON SOURCE LINES 187-189 Translate simplices and row/column numbers ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ .. GENERATED FROM PYTHON SOURCE LINES 191-192 Look up the column number for a simplex .. GENERATED FROM PYTHON SOURCE LINES 192-195 .. code-block:: Python column_number = submatrix_index_tool.submatrix_column_number_for_simplex([1,2,3]) column_number .. rst-class:: sphx-glr-script-out .. code-block:: none 12 .. GENERATED FROM PYTHON SOURCE LINES 196-197 Look up the simplex for a column number .. GENERATED FROM PYTHON SOURCE LINES 197-200 .. code-block:: Python weighted_simplex = submatrix_index_tool.weighted_simplex_for_submatrix_column_number( column_number ) weighted_simplex .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 201-202 The WeightedSimplex object is just a wrapper around the simplex and the weight .. GENERATED FROM PYTHON SOURCE LINES 202-206 .. code-block:: Python print(weighted_simplex) print(weighted_simplex.simplex()) print(weighted_simplex.weight()) .. rst-class:: sphx-glr-script-out .. code-block:: none Simplex: (1, 2, 3) Weight: 0.830129 (1, 2, 3) 0.830129490771365 .. GENERATED FROM PYTHON SOURCE LINES 207-209 Convert column vector types ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ .. GENERATED FROM PYTHON SOURCE LINES 211-214 .. code-block:: Python column_vector = boundary_matrix_oracle.column_for_simplex([0,1,2]) dense_array = submatrix_index_tool.dense_array_for_column_vector_dataframe(column_vector) .. GENERATED FROM PYTHON SOURCE LINES 215-217 .. code-block:: Python column_vector .. raw:: html
simplex filtration coefficient
0 (0, 2) 0.370513 -1
1 (1, 2) 0.726618 1
2 (0, 1) 0.877768 1


.. GENERATED FROM PYTHON SOURCE LINES 218-220 .. code-block:: Python dense_array .. rst-class:: sphx-glr-script-out .. code-block:: none array([Fraction(0, 1), Fraction(0, 1), Fraction(0, 1), Fraction(0, 1), Fraction(0, 1), Fraction(-1, 1), Fraction(0, 1), Fraction(0, 1), Fraction(0, 1), Fraction(0, 1), Fraction(1, 1), Fraction(0, 1), Fraction(1, 1), Fraction(0, 1), Fraction(0, 1)], dtype=object) .. GENERATED FROM PYTHON SOURCE LINES 221-222 Reconstruct the original column vector .. GENERATED FROM PYTHON SOURCE LINES 222-225 .. code-block:: Python reconstructed_column_vector = submatrix_index_tool.column_vector_dataframe_for_dense_array(dense_array) column_vector.equals(reconstructed_column_vector) .. rst-class:: sphx-glr-script-out .. code-block:: none True .. GENERATED FROM PYTHON SOURCE LINES 226-228 Convert row vector types ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ .. GENERATED FROM PYTHON SOURCE LINES 230-232 .. code-block:: Python row_vector = boundary_matrix_oracle.row_for_simplex([0,1]) dense_array = submatrix_index_tool.dense_array_for_row_vector_dataframe(row_vector) .. GENERATED FROM PYTHON SOURCE LINES 233-235 .. code-block:: Python row_vector .. raw:: html
simplex filtration coefficient
0 (0, 1, 2) 0.877768 1
1 (0, 1, 3) 0.901882 1
2 (0, 1, 4) 0.940755 1


.. GENERATED FROM PYTHON SOURCE LINES 236-238 .. code-block:: Python dense_array .. rst-class:: sphx-glr-script-out .. code-block:: none array([Fraction(0, 1), Fraction(0, 1), Fraction(0, 1), Fraction(0, 1), Fraction(0, 1), Fraction(0, 1), Fraction(0, 1), Fraction(0, 1), Fraction(0, 1), Fraction(0, 1), Fraction(0, 1), Fraction(0, 1), Fraction(0, 1), Fraction(0, 1), Fraction(1, 1), Fraction(1, 1), Fraction(0, 1), Fraction(1, 1), Fraction(0, 1), Fraction(0, 1)], dtype=object) .. GENERATED FROM PYTHON SOURCE LINES 239-240 Reconstruct the original column vector .. GENERATED FROM PYTHON SOURCE LINES 240-243 .. code-block:: Python reconstructed_row_vector = submatrix_index_tool.row_vector_dataframe_for_dense_array(dense_array) row_vector.equals(reconstructed_row_vector) .. rst-class:: sphx-glr-script-out .. code-block:: none True .. GENERATED FROM PYTHON SOURCE LINES 244-248 Common Python (sparse) matrix formats ------------------------------------------- OAT offers convenient tools to export submatrices to common formats. The first step is specifying which rows and columns you want to export to this format. We do this with a `SubmatrixIndexTool`, which also provides a number of convenient methods for mapping between simplices and row/column numbers. See the tutorial on Vietoris Rips boundary matrices for more details. .. GENERATED FROM PYTHON SOURCE LINES 250-252 SciPy sparse matrix: CSR format ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ .. GENERATED FROM PYTHON SOURCE LINES 254-256 .. code-block:: Python submatrix = boundary_matrix_oracle.write_submatrix_to_csr( submatrix_index_tool ) .. GENERATED FROM PYTHON SOURCE LINES 257-259 .. code-block:: Python submatrix .. rst-class:: sphx-glr-script-out .. code-block:: none <15x20 sparse matrix of type '' with 50 stored elements in Compressed Sparse Row format> .. GENERATED FROM PYTHON SOURCE LINES 260-262 SciPy sparse matrix: (row,column,coefficient) triplet format ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ .. GENERATED FROM PYTHON SOURCE LINES 264-265 get a list of the nonzero triplets .. GENERATED FROM PYTHON SOURCE LINES 265-268 .. code-block:: Python sparse_coo = submatrix.tocoo() # convert to COOrdinate format list(zip(sparse_coo.row, sparse_coo.col, sparse_coo.data)) # zip the row, column, and data arrays together to get a list of (row, column, value) triplets .. rst-class:: sphx-glr-script-out .. code-block:: none [(0, 0, -1.0), (0, 7, -1.0), (0, 8, -1.0), (0, 9, -1.0), (1, 2, -1.0), (1, 3, -1.0), (1, 5, -1.0), (1, 7, 1.0), (2, 0, 1.0), (2, 1, -1.0), (2, 5, 1.0), (2, 6, -1.0), (3, 3, 1.0), (3, 4, -1.0), (3, 6, 1.0), (3, 8, 1.0), (4, 1, 1.0), (4, 2, 1.0), (4, 4, 1.0), (4, 9, 1.0), (5, 14, -1.0), (5, 16, 1.0), (5, 18, 1.0), (6, 11, 1.0), (6, 13, -1.0), (6, 18, 1.0), (7, 10, -1.0), (7, 11, -1.0), (7, 17, 1.0), (8, 10, 1.0), (8, 12, -1.0), (8, 15, 1.0), (9, 10, 1.0), (9, 13, 1.0), (9, 19, 1.0), (10, 11, 1.0), (10, 12, 1.0), (10, 14, 1.0), (11, 12, 1.0), (11, 13, 1.0), (11, 16, 1.0), (12, 14, 1.0), (12, 15, 1.0), (12, 17, 1.0), (13, 15, -1.0), (13, 16, -1.0), (13, 19, 1.0), (14, 17, -1.0), (14, 18, -1.0), (14, 19, -1.0)] .. GENERATED FROM PYTHON SOURCE LINES 269-271 Dense numpy array ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ .. GENERATED FROM PYTHON SOURCE LINES 273-274 print a dense version of the matrix .. GENERATED FROM PYTHON SOURCE LINES 274-276 .. code-block:: Python submatrix.todense() .. rst-class:: sphx-glr-script-out .. code-block:: none matrix([[-1., 0., 0., 0., 0., 0., 0., -1., -1., -1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.], [ 0., 0., -1., -1., 0., -1., 0., 1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.], [ 1., -1., 0., 0., 0., 1., -1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.], [ 0., 0., 0., 1., -1., 0., 1., 0., 1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.], [ 0., 1., 1., 0., 1., 0., 0., 0., 0., 1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.], [ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., -1., 0., 1., 0., 1., 0.], [ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1., 0., -1., 0., 0., 0., 0., 1., 0.], [ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., -1., -1., 0., 0., 0., 0., 0., 1., 0., 0.], [ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1., 0., -1., 0., 0., 1., 0., 0., 0., 0.], [ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1., 0., 0., 1., 0., 0., 0., 0., 0., 1.], [ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1., 1., 0., 1., 0., 0., 0., 0., 0.], [ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1., 1., 0., 0., 1., 0., 0., 0.], [ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1., 1., 0., 1., 0., 0.], [ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., -1., -1., 0., 0., 1.], [ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., -1., -1., -1.]]) .. GENERATED FROM PYTHON SOURCE LINES 277-279 Sparse dataframe ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ .. GENERATED FROM PYTHON SOURCE LINES 281-282 you can use the the submatrix_index_tool to export a CSR matrix to a sparse pandas DataFrame with labeled rows and column .. GENERATED FROM PYTHON SOURCE LINES 282-284 .. code-block:: Python submatrix_index_tool.sparse_dataframe_for_csr_matrix(submatrix) .. raw:: html
simplex (0, 2) (2, 4) (1, 4) (1, 3) (3, 4) (1, 2) (2, 3) (0, 1) (0, 3) (0, 4) (1, 3, 4) (1, 2, 4) (1, 2, 3) (2, 3, 4) (0, 1, 2) (0, 1, 3) (0, 2, 3) (0, 1, 4) (0, 2, 4) (0, 3, 4)
(0,) -1.0 0 0 0 0 0 0 -1.0 -1.0 -1.0 0 0 0 0 0 0 0 0 0 0
(1,) 0 0 -1.0 -1.0 0 -1.0 0 1.0 0 0 0 0 0 0 0 0 0 0 0 0
(2,) 1.0 -1.0 0 0 0 1.0 -1.0 0 0 0 0 0 0 0 0 0 0 0 0 0
(3,) 0 0 0 1.0 -1.0 0 1.0 0 1.0 0 0 0 0 0 0 0 0 0 0 0
(4,) 0 1.0 1.0 0 1.0 0 0 0 0 1.0 0 0 0 0 0 0 0 0 0 0
(0, 2) 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -1.0 0 1.0 0 1.0 0
(2, 4) 0 0 0 0 0 0 0 0 0 0 0 1.0 0 -1.0 0 0 0 0 1.0 0
(1, 4) 0 0 0 0 0 0 0 0 0 0 -1.0 -1.0 0 0 0 0 0 1.0 0 0
(1, 3) 0 0 0 0 0 0 0 0 0 0 1.0 0 -1.0 0 0 1.0 0 0 0 0
(3, 4) 0 0 0 0 0 0 0 0 0 0 1.0 0 0 1.0 0 0 0 0 0 1.0
(1, 2) 0 0 0 0 0 0 0 0 0 0 0 1.0 1.0 0 1.0 0 0 0 0 0
(2, 3) 0 0 0 0 0 0 0 0 0 0 0 0 1.0 1.0 0 0 1.0 0 0 0
(0, 1) 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1.0 1.0 0 1.0 0 0
(0, 3) 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -1.0 -1.0 0 0 1.0
(0, 4) 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -1.0 -1.0 -1.0


.. GENERATED FROM PYTHON SOURCE LINES 285-286 this method is identical to calling the following: .. GENERATED FROM PYTHON SOURCE LINES 286-293 .. code-block:: Python df = pd.DataFrame.sparse.from_spmatrix( submatrix, index=submatrix_index_tool.row_indices().simplex.tolist(), columns=submatrix_index_tool.column_indices().simplex, ) df .. raw:: html
simplex (0, 2) (2, 4) (1, 4) (1, 3) (3, 4) (1, 2) (2, 3) (0, 1) (0, 3) (0, 4) (1, 3, 4) (1, 2, 4) (1, 2, 3) (2, 3, 4) (0, 1, 2) (0, 1, 3) (0, 2, 3) (0, 1, 4) (0, 2, 4) (0, 3, 4)
(0,) -1.0 0 0 0 0 0 0 -1.0 -1.0 -1.0 0 0 0 0 0 0 0 0 0 0
(1,) 0 0 -1.0 -1.0 0 -1.0 0 1.0 0 0 0 0 0 0 0 0 0 0 0 0
(2,) 1.0 -1.0 0 0 0 1.0 -1.0 0 0 0 0 0 0 0 0 0 0 0 0 0
(3,) 0 0 0 1.0 -1.0 0 1.0 0 1.0 0 0 0 0 0 0 0 0 0 0 0
(4,) 0 1.0 1.0 0 1.0 0 0 0 0 1.0 0 0 0 0 0 0 0 0 0 0
(0, 2) 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -1.0 0 1.0 0 1.0 0
(2, 4) 0 0 0 0 0 0 0 0 0 0 0 1.0 0 -1.0 0 0 0 0 1.0 0
(1, 4) 0 0 0 0 0 0 0 0 0 0 -1.0 -1.0 0 0 0 0 0 1.0 0 0
(1, 3) 0 0 0 0 0 0 0 0 0 0 1.0 0 -1.0 0 0 1.0 0 0 0 0
(3, 4) 0 0 0 0 0 0 0 0 0 0 1.0 0 0 1.0 0 0 0 0 0 1.0
(1, 2) 0 0 0 0 0 0 0 0 0 0 0 1.0 1.0 0 1.0 0 0 0 0 0
(2, 3) 0 0 0 0 0 0 0 0 0 0 0 0 1.0 1.0 0 0 1.0 0 0 0
(0, 1) 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1.0 1.0 0 1.0 0 0
(0, 3) 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -1.0 -1.0 0 0 1.0
(0, 4) 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -1.0 -1.0 -1.0


.. GENERATED FROM PYTHON SOURCE LINES 294-296 Matplotlib scatter ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ .. GENERATED FROM PYTHON SOURCE LINES 298-308 .. code-block:: Python import matplotlib.pyplot as plt # Suppose `csr` is your scipy.sparse.csr_matrix plt.figure(figsize=(6, 6)) plt.spy(submatrix, markersize=5) plt.title("Sparsity Pattern (matplotlib)") plt.xlabel("Columns") plt.ylabel("Rows") plt.show() .. image-sg:: /auto_examples/vietoris_rips/images/sphx_glr_plot_matrices_001.png :alt: Sparsity Pattern (matplotlib) :srcset: /auto_examples/vietoris_rips/images/sphx_glr_plot_matrices_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 309-311 Plotly ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ .. GENERATED FROM PYTHON SOURCE LINES 313-359 .. code-block:: Python # points: your scipy.sparse.coo_matrix # row_labels: list of row labels (length = points.shape[0]) # col_labels: list of column labels (length = points.shape[1]) points = submatrix.tocoo() row_labels = submatrix_index_tool.row_indices().simplex.tolist() col_labels = submatrix_index_tool.column_indices().simplex.tolist() hover_text = [ f"row: {row_labels[r]}
column: {col_labels[c]}
coefficient: {v}" for r, c, v in zip(points.row, points.col, points.data) ] fig = go.Figure(go.Scattergl( x=points.col, y=points.row, mode='markers', marker=dict(size=4), text=hover_text, hoverinfo="text" )) fig.update_layout( title="Hover over points to see row/column/coefficient!", xaxis_title="Columns", yaxis_title="Rows", xaxis=dict( tickmode='array', tickvals=list(range(len(col_labels))), ticktext=[str(label) for label in col_labels] ), yaxis=dict( tickmode='array', tickvals=list(range(len(row_labels))), ticktext=[str(label) for label in row_labels], autorange='reversed' ), width=1200, height=800 ) fig .. raw:: html


.. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.108 seconds) .. _sphx_glr_download_auto_examples_vietoris_rips_plot_matrices.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_matrices.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_matrices.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_matrices.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_