views:

318

answers:

2

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.

+1  A: 

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 
gnibbler
I'm looking at this and it basically looks like manually unwrapping a sparse array representation to the full representation with zeroes. If M,N = 10^6 this would cause problems fast. I should place the code I'm currently using.
wheaties
+1  A: 

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 
bayer
This could also be written as a list comprehension, though it might have too much in it to be easy to follow.
pwdyson
In the case of the list comprehension you first create a big list of lists. In this case, you bypass this, which results in less memory usage. Depends on you prefer.
bayer