Accumulate values of "neigborhood" from edgelist with numpy
<p>I have a undirected network where each node can be one of <strong>k</strong> types. For each node <em>i</em>, I need to calculate the number of neighbors that node <em>i</em> has of each type.</p>

<p>Right now I am representing the edges with an edgelist where the columns are indexes of the nodes. The nodes are represented as a <strong>n x k</strong> matrix, where each column represents a node type. If a node is of type <em>k</em> then the <em>k</em>th column's value is 1, 0 otherwise.</p>

<p>Here's my current code, which is correct, but too slow.</p>

<pre><code># example nodes and edges, both typically much longer
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]
</code></pre>

<p>Is there some clever numpy that would allow me to avoid this for loop? If the best way to do this is with an adjacency matrix, that is also acceptable.</p>

<p>If I understand your question correctly, the <a href="https://pypi.python.org/pypi/numpy-indexed" rel="nofollow">numpy_indexed</a> package (disclaimer: I am its author) has a fast and elegant solution to this:</p>

<pre><code># generate a random example graph
n_edges = 50
n_nodes = 10
n_types = 3
edges = np.random.randint(0, n_nodes, size=(n_edges, 2))
node_types = np.random.randint(0, 2, size=(n_nodes, n_types)).astype(np.bool)

# Note; this is for a directed graph
s, e = edges.T
# for undirected, add reversed edges
s, e = np.concatenate([edges, edges[:,::-1]], axis=0).T
import numpy_indexed as npi
node_idx, neighbor_type_count = npi.group_by(s).sum(node_types[e])
</code></pre>

<p>In general, operations on graphs, or algorithms involving jagged-arrays, can often be efficiently and elegantly expressed using grouping-operations.</p>

python numpy vectorization graph-algorithm numpy-broadcasting