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(...) }
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.
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.
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).
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'] )
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With