I have a non-oriented network, where each node can be one of the types k . For each node, I need to calculate the number of neighbors that the node I have for each type.
I now represent edges using edgelist, where the columns are node indices. Nodes are represented as an nxk matrix , where each column represents a node type. If a node is of type k, then the value of the kth column is 1, 0.
Here is my current code, which is correct but too slow.
nodes = np.array([[0, 0, 1],
[0, 1, 0],
[1, 0, 0]])
edges = np.array([[0, 1],
[1, 2]])
neighbors = np.zeros_like(nodes)
for i, j in edges:
neighbors[i] += nodes[j]
neighbors[j] += nodes[i]
Is there any clever numpy that would allow me to avoid this loop? If the best way to do this is with an adjacency matrix, this is also acceptable.