I am running a simulation that creates various arrays and matrices every update cycle that I wish to store. The data is all numerical and ranges from scalars to 3 x 4 matrices. I would ideally save the data to a single file and wish to subsequently analyse the data in pandas. I have tried the csv.DictWriter.writerow() method but this saves the arrays as strings which I cannot do analysis on.
            Asked
            
        
        
            Active
            
        
            Viewed 107 times
        
    -1
            
            
         
    
    
        Marcin Orlowski
        
- 72,056
- 11
- 123
- 141
 
    
    
        seanysull
        
- 720
- 1
- 8
- 24
- 
                    Have you tried this? https://stackoverflow.com/questions/6081008/dump-a-numpy-array-into-a-csv-file – James Chang Jan 28 '19 at 11:00
3 Answers
1
            You can use numpy.savez to save multiple numpy arrays into one file.
For analysing you can just load it with numpy.load
 
    
    
        markuscosinus
        
- 2,248
- 1
- 8
- 19
- 
                    
- 
                    As far as I know no, not directly. However you could `numpy.load` the file, then add the new entries to the list and then `numpy.savez` it again. – markuscosinus Jan 28 '19 at 11:06
0
            
            
        Probably the fastest and simplest way of storing and loading your matrix data would be to store them in numpy binary format with np.save (https://docs.scipy.org/doc/numpy-1.15.0/reference/generated/numpy.save.html)
and then loading it back with numpy load before importing into pandas dataframe. 
Binary format will give your speed advantage over any text based formats.
 
    
    
        Artem Trunov
        
- 1,340
- 9
- 16
0
            
            
        Maybe you can consider the yaml library.
import yaml
import numpy as np
ary = np.zeros((2, 2, 2))
Save to file:
with open('numpy_to.yml', 'w') as outfile:
    yaml.dump(ary, outfile, default_flow_style=False)
Reload data:
with open("numpy_to.yml", 'r') as inputfile:
    ary_back = yaml.load(inputfile)
print(ary_back)
# [[[0. 0.]
#   [0. 0.]]
#  [[0. 0.]
#   [0. 0.]]]
If you want to store to a single file, you could
dump a dienter code herect:
zeros = np.zeros((2, 2, 2))
ones = np.ones((2, 2, 2))
my_numpies = { 'zeros': zeros, 'ones': ones}
 
    
    
        iGian
        
- 11,023
- 3
- 21
- 36