开发者

Convert dict to array in NumPy

I'd like to take a dictionary of a dictionary containing floats, indexed by ints and convert it into a numpy.array for use with the numpy library. Currently I'm manually converting the values into two arrays, one for the original indexes and the other for the values. While I've looked at numpy.asarray my conclusion has been that I must be doing something wrong with it. Could anyone show an example of how to properly convert such a creation? Don't have to use numpy.asarray, anything will do.

from collections import defaultdict
foo = defaultdict( lambda: defaultdict(float) )
#Then "foo" is populated by several
#routines reading results from a DB
#
#As an example
foo[ 7104 ][ 3 ] = 4.5
foo[ 203 ][ 1 ] = 3.2
foo[ 2 ][ 1 ] = 2.7

I'd like to have just a multi-dimensional array of floats, not an array of dicts.

Edit:

Sorry for the delay. Here is the code that I was using to create the first array object containing just the values:

storedArray = numpy.asarray( reduce( lambda x,y: x + y, (item.values()开发者_开发问答 for item in storedMapping.values() ) ) )

I was hoping someone might know a magic bullet that could convert a dict of dict into an array.


You can calculate N and M like this

N=max(foo)+1
M=max(max(x) for x in foo.values())+1
fooarray = numpy.zeros((N, M))
for key1, row in foo.iteritems():
   for key2, value in row.iteritems():
       fooarray[key1, key2] = value 

There are various options for sparse arrays. Eg,

import scipy.sparse
foosparse = scipy.sparse.lil_matrix((N, M))
for key1, row in foo.iteritems():
   for key2, value in row.iteritems():
       foosparse[(key1, key2)] = value 


Say you have a NxM array, then I'd do the following:

myarray = numpy.zeros((N, M))
for key1, row in mydict.iteritems():
   for key2, value in row.iteritems():
       myarray[key1, key2] = value 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜