Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Saving numpy array in mongodb

I have a couple of MongoDB documents wherein one my the fields is best represented as a matrix (numpy array). I would like to save this document to MongoDB, how do I do this?

{ 'name' : 'subject1', 'image_name' : 'blah/foo.png', 'feature1' : np.array(...) } 
like image 472
Dat Chu Avatar asked Jun 16 '11 05:06

Dat Chu


People also ask

Can we store array in MongoDB?

One of the benefits of MongoDB's rich schema model is the ability to store arrays as document field values. Storing arrays as field values allows you to model one-to-many or many-to-many relationships in a single document, instead of across separate collections as you might in a relational database.

Can I save a NumPy array?

You can save your NumPy arrays to CSV files using the savetxt() function. This function takes a filename and array as arguments and saves the array into CSV format.

Which matrix multiplication algorithm does NumPy use?

NumPy uses a highly-optimized, carefully-tuned BLAS method for matrix multiplication (see also: ATLAS). The specific function in this case is GEMM (for generic matrix multiplication).


1 Answers

For a 1D numpy array, you can use lists:

# serialize 1D array x record['feature1'] = x.tolist()  # deserialize 1D array x x = np.fromiter( record['feature1'] ) 

For multidimensional data, I believe you'll need to use pickle and pymongo.binary.Binary:

# serialize 2D array y record['feature2'] = pymongo.binary.Binary( pickle.dumps( y, protocol=2) ) )  # deserialize 2D array y y = pickle.loads( record['feature2'] ) 
like image 75
jeff7 Avatar answered Sep 27 '22 23:09

jeff7