Saving and loading a Numpy Matrix in python

19,025

Solution 1

>>> import numpy
>>> mat = numpy.matrix("1 2 3; 4 5 6; 7 8 9")
>>> mat.dump("my_matrix.dat")
>>> mat2 = numpy.load("my_matrix.dat")

Solution 2

you can pickle your matrix:

 >> import numpy
 >> import pickle
 >> b=numpy.matrix('1 2; 3 4')
 >> f=open('test','w')
 >> pickle.dump(b, f)
 >> f.close()

 >> f2 = open('test', 'r')
 >> s = pickle.load(f2)
 >> f2.close()
 >> s

    matrix([[1, 2],
            [3, 4]])

Tamas answer is much better than this: numpy matrixes objects have a direct method to pickle them.

In any case take into account that the pickle library is a general tool for saving python objects including classes.

Solution 3

You can obviously try numpy.save() and numpy.load() being quite efficient and fast as follows:

import numpy as np
def save_matrices(A,B,C, file_name):
    with open(file_name, 'wb') as f:
        np.save(f, A)
        np.save(f, B)
        np.save(f, C)

def load_matrices(file_name):
    with open(file_name, 'rb') as f:
        A = np.load(f)
        B = np.load(f)
        C = np.load(f)
    return (A,B,C)

if __name__ == "__main__":
    # generate random matrices in [0,1):       
    a, b = 0, 1
    A = (b - a) * np.random.random_sample((3, 3)) + a
    B = (b - a) * np.random.random_sample((3, 3)) + a
    C = (b - a) * np.random.random_sample((3, 3)) + a
    my_file = 'test.npy'
    save_matrices(A,B,C, my_file)
    loaded_A, loaded_B, loaded_C = load_matrices(my_file)
Share:
19,025
Hossein
Author by

Hossein

Updated on June 20, 2022

Comments

  • Hossein
    Hossein almost 2 years

    Can someone gives me an example of how to save a 2-d matrix in a file and reloading it for further use?

  • Justin Peel
    Justin Peel almost 14 years
    Also, the numpy built-in methods are much, much faster than using pickle (or even cPickle). I found this out personally some time ago. The speed difference was very substantial for what I was doing.
  • joaquin
    joaquin almost 14 years
    @Justin: Interesting. Are they compatible library pickle and numpy pickle?
  • Justin Peel
    Justin Peel almost 14 years
    I don't know for sure. I think that the reason is that the numpy methods are made to more efficiently store the arrays which is where most of the speed comes from. I say this because in my experience the sizes of the respective files for pickle and numpy's methods are much smaller for the latter. There might be other reasons, but that is enough for me.
  • Tamás
    Tamás almost 14 years
    according to the NumPy help on ndarray.dump, they should be compatible.
  • joaquin
    joaquin almost 14 years
    Thanks I will experiment with it