Gray Fullbuster
Gray Fullbuster

Reputation: 1

How do i make adjacency Matrix with the given data

I have a gene interaction data like down below. Any idea how to make an adjacency matrix from this data?

this data have 3 columns and 66k rows, and have about 5k unique values

Upvotes: 0

Views: 361

Answers (1)

Riley
Riley

Reputation: 2261

Example data:

cat_type = pd.CategoricalDtype(list("abcdef"))
df = pd.DataFrame(
    {
        "node1":["a", "b", "a", "c", "b", "f"],
        "node2":["b", "d", "c", "e", "f", "e"],
    }
).astype(cat_type)

df looks like this:

  node1 node2
0     a     b
1     b     d
2     a     c
3     c     e
4     b     f
5     f     e

Solution

adj_mat = pd.crosstab(df["node1"], df["node2"], dropna=False)

results in a dataframe:

   a  b  c  d  e  f
a  0  1  1  0  0  0
b  0  0  0  1  0  1
c  0  0  0  0  1  0
d  0  0  0  0  0  0
e  0  0  0  0  0  0
f  0  0  0  0  1  0

If you need it symmetrical around the diagonal then the following will give you a boolean result

adj_mat.transpose() + adj_mat > 0

which you can then convert to integer with .astype(int) if required

Upvotes: 1

Related Questions