Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You can load your data with something like this:</p> <pre><code>import numpy as np import scipy.sparse as sps data = np.genfromtxt('data.txt', dtype=[('from', np.intp), ('to', np.intp), ('weight', np.float)]) </code></pre> <p>If you want to store your weights in a sparse matrix <code>graph</code>, where <code>graph[i, j]</code> is the weight from node <code>i</code> to node <code>j</code>, you could do:</p> <pre><code>graph = sps.csr_matrix((data['weight'], (data['from'], data['to']))) </code></pre> <p>To have a sorted list of outgoing nodes, I would use a dictionary, where <code>sorted_to</code> is an array of outgoing nodes sorted by weight. It's a little hacky, and dependent on the CSR sparse matrix format, but you could do:</p> <pre><code>graph = sps.rand(10, 10, density=0.1, format='csr') data, indptr, indices = graph.data, graph.indptr, graph.indices non_empty_rows, = np.nonzero(np.diff(graph.indptr)) sorted_out = {} for j in non_empty_rows: weight_slice = data[indptr[j]:indptr[j+1]] out_slice = indices[indptr[j]:indptr[j+1]] sorted_out[j] = out_slice[np.argsort(weight_slice)] </code></pre> <p>With a simple made up example:</p> <pre><code>&gt;&gt;&gt; graph = sps.rand(5, 5, density=0.2, format='csr') &gt;&gt;&gt; graph.toarray() array([[ 0.88968871, 0. , 0. , 0.80773932, 0. ], [ 0. , 0. , 0.8921645 , 0. , 0. ], [ 0. , 0. , 0. , 0. , 0. ], [ 0.18552664, 0. , 0. , 0. , 0. ], [ 0. , 0. , 0. , 0. , 0.22945956]]) &gt;&gt;&gt; non_empty_rows array([0, 1, 3, 4], dtype=int64) &gt;&gt;&gt; sorted_out {0: array([3, 0]), 1: array([2]), 3: array([0]), 4: array([4])} </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload