Search code examples
pythonarraysnumpynumpy-memmap

cast numpy array into memmap


I generate some data in my memory and I want to cast it into numpy.memmap to save up RAM. What should I do? my data is in:

    X_list_total_standardized=np.array(X_list_total_standardized)

I know that I could initialize an empty numpy.memmap:

X_list_total_standardized_memmap=np.memmap(self._prepared_data_location_npmemmap_X,dtype='float32',mode='w+')

What is the most convenient way to store X_list_total_standardized into the memmap? Thank you

PS: would the following command be ok?

    X_list_total_standardized_memmap[:]=X_list_total_standardized[:]

Solution

  • I found next example in numpy documentation :

    data = np.arange(12, dtype='float32')
    data.resize((3,4))
    fp = np.memmap(filename, dtype='float32', mode='w+', shape=(3,4))
    fp[:] = data[:]
    

    So your last command is ok.