2

I have created a adjacency matrix using networkx as below:

from networkx.algorithms.bipartite.matrix import biadjacency_matrix as adj
user_node_list = data['user_id'].unique()
item_node_list = data['item_id'].unique()
adj_matrix = adj(B, user_node_list, column_order=item_node_list, dtype=None, weight='rating', format='csr')

I want to visualize this adj_matrix. How can I do this?

Lalit Jain
  • 363
  • 2
  • 14
  • I think you want to visualize the graph and not its adjacency matrix. – Mykola Zotko Feb 10 '20 at 09:39
  • @MykolaZotko I already have created graph using that graph only i have created matrix. But i want to check whether it is correct So for checking it i need to see the matrix – Lalit Jain Feb 10 '20 at 09:41
  • Are you after `print(adj_matrix)`? – Joel Feb 10 '20 at 10:48
  • @Joel I tried that but this doesn't give me clear picture what exactly matrix look like – Lalit Jain Feb 10 '20 at 10:52
  • You'll need to explain in more detail what you mean by visualize. – Joel Feb 10 '20 at 14:01
  • @Joel data is about a user, an item and rating given by the user to that particular item. There total 943 unique user and 1662 unique items. I need to create a matrix such that size of matrix is (943x1662). So if for each user matrix will have 1662 columns. Only those column will be filled for a user to which he/she has given rating. I want to visualize whether correct matrix has been created as per my requirement – Lalit Jain Feb 10 '20 at 14:56
  • It's still unclear how you want to visualize it. The easiest way I can see to do this is to print the matrix, or print specific entries. Otherwise, maybe use some for loops to check that the entries are what you expect. If those aren't good enough, then we still need to know how you want to visualize it. What do you want it to look like? – Joel Feb 10 '20 at 23:03
  • @Joel I think what means is they would like to inspect the elements of the matrix visually. Networkx only returns compressed sparse row matrices. A print call of course would not display the matrix elements as Lalit expects. See my answer for more detail – Hugo Jan 29 '21 at 04:18

2 Answers2

0

You can use Pandas to visualize your adj_matrix as following:

import pandas as pd
A = pd.DataFrame(adj_matrix)
Sahil Kamboj
  • 390
  • 2
  • 5
  • 16
0

Much of the time we're working with graphs with sparse adjacency matrices, so networkx returns a SciPy Compressed Sparse Row matrix rather than a numpy.ndarray or numpy.matrix. The former representation uses more efficient data structures and algorithms for representing and processing sparse matrices. In particular the __repr__ representation of the matrix differs from that of a vanilla (dense) NumPy matrix. It will look something like

<11x11 sparse matrix of type '<class 'numpy.int64'>'
    with 28 stored elements in Compressed Sparse Row format>

This makes sense because if the representation of a CSR matrix were the same as what we see with a dense matrix, a simple print statement or logging message could have serious performance impacts if the matrix were very large.

Compare the above output with the __repr__ output of a vanilla (dense) NumPy matrix:

matrix([[0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0],
        [1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0],
        [1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1],
        [0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0],
        [0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0],
        [0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0],
        [0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0],
        [0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0],
        [0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1],
        [0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0]])

which allows us to inspect the matrix elements visually (I am guessing that this is what was meant with "visualize the adj_matrix").

To convert a sparse CSR matrix to a dense NumPy matrix, simply do sparse_matrix.todense(). Note that this representation of a sparse matrix will require substantially more memory, so be mindful of that when working with larger graphs.

Hugo
  • 546
  • 5
  • 12