Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert dict to array in NumPy

Tags:

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.

like image 762
wheaties Avatar asked Feb 08 '10 20:02

wheaties


1 Answers

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 
like image 187
John La Rooy Avatar answered Nov 15 '22 04:11

John La Rooy