Populating a NumPy array using dictionary containing indices

问题内容:

I am trying to populate a numpy array based on a dictionary of values.

My dictionary looks like this:

A = {(12, 15): 4, (532, 31): 7, (742, 1757): 1, …}

I am trying to populate the array so that (using my above example) 4 is at the index (12,15) and so on.
The keys in A are called ‘d’,’s’ and the value is referred to as ‘count’.

A = {(d,s): count}

At the moment my code to populate the array looks like this:

N = len(seen)
Am = np.zeros((N,N), 'i')
for key, count in A.items():
    Am[d,s] = count

But this just leads to multiple arrays mostly full of zeros being created.

Thank you

问题评论:

    
What is N?….
    
N is the highest value of s/d in the dictionary.
– Alcor
4 hours ago

答案:

答案1:

Here’s one approach –

def dict_to_arr(A):
    idx = np.array(list(A.keys()))
    val = np.array(list(A.values()))

    m,n = idx.max(0)+1 # max extents of indices to decide o/p array
    out = np.zeros((m,n), dtype=val.dtype)
    out[idx[:,0], idx[:,1]] = val # or out[tuple(idx.T)] = val
    return out

Possibly faster one if we avoid the array conversion of the indices and values and directly use those for assigning at the last step, like so –

out[zip(*A.keys())] = list(A.values())

Sample run –

In [3]: A = {(12, 15): 4, (532, 31): 7, (742, 1757): 1}

In [4]: arr = dict_to_arr(A)

In [5]: arr[12,15], arr[532,31], arr[742,1757]
Out[5]: (4, 7, 1)

Store into sparse matrix

To save on memory and possibly gain performance as well, we might want to store in a sparse matrix. Let’s do it with csr_matrix, like so –

from scipy.sparse import csr_matrix

def dict_to_sparsemat(A):
    idx = np.array(list(A.keys()))
    val = np.array(list(A.values()))
    m,n = idx.max(0)+1
    return csr_matrix((val, (idx[:,0], idx[:,1])), shape=(m,n))

Sample run –

In [64]: A = {(12, 15): 4, (532, 31): 7, (742, 1757): 1}

In [65]: out = dict_to_sparsemat(A)

In [66]: out[12,15], out[532,31], out[742,1757]
Out[66]: (4, 7, 1)

答案评论:

原文地址:

https://stackoverflow.com/questions/47752233/populating-a-numpy-array-using-dictionary-containing-indices

添加评论

友情链接:蝴蝶教程