Python numpy数据的保存和读取

在科学计算的过程中,往往需要保存一些数据,也经常需要把保存的这些数据加载到程序中,在 Matlab 中我们可以用 save 和 lood 函数很方便的实现。类似的在 Python 中,我们可以用 numpy.save() 和 numpy.load() 函数达到类似的效果,并且还可以用 scipy.io.savemat() 将数据保存为 .mat 格式,用scipy.io.loadmat() 读取 .mat 格式的数据,达到可以和 Matlab 或者Octave 进行数据互动的效果.

下面分别介绍之:
numpy.save()

    Save an array to a binary file in NumPy ``.npy`` format.

    Parameters
    ----------
    file : file, str, or pathlib.Path
        File or filename to which the data is saved.  If file is a file-object,
        then the filename is unchanged.  If file is a string or Path, a ``.npy``
        extension will be appended to the file name if it does not already
        have one.
    arr : array_like
        Array data to be saved.
    allow_pickle : bool, optional
        Allow saving object arrays using Python pickles. Reasons for disallowing
        pickles include security (loading pickled data can execute arbitrary
        code) and portability (pickled objects may not be loadable on different
        Python installations, for example if the stored objects require libraries
        that are not available, and not all pickled data is compatible between
        Python 2 and Python 3).
        Default: True
    fix_imports : bool, optional
        Only useful in forcing objects in object arrays on Python 3 to be
        pickled in a Python 2 compatible way. If `fix_imports` is True, pickle
        will try to map the new Python 3 names to the old module names used in
        Python 2, so that the pickle data stream is readable with Python 2.

    See Also
    --------
    savez : Save several arrays into a ``.npz`` archive
    savetxt, load

    Notes
    -----
    For a description of the ``.npy`` format, see :py:mod:`numpy.lib.format`.

    Examples
    --------
    >>> from tempfile import TemporaryFile
    >>> outfile = TemporaryFile()

    >>> x = np.arange(10)
    >>> np.save(outfile, x)

    >>> outfile.seek(0) # Only needed here to simulate closing & reopening file
    >>> np.load(outfile)
    array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

示例:

import numpy as np

a=np.mat('1,2,3;4,5,6')
b=np.array([[1,2,3],[4,5,6]])

np.save('a.npy',a)
np.save('b.npy',b)

numpy.load()

    Wrapper around cPickle.load which accepts either a file-like object or
    a filename.

    Note that the NumPy binary format is not based on pickle/cPickle anymore.
    For details on the preferred way of loading and saving files, see `load`
    and `save`.

    See Also
    --------
    load, save

示例:

data_a=np.load('a.npy')
data_b=np.load('b.npy')

print ('data_a 
',data_a,'
 the type is',type(data_a))
print ('data_b 
',data_a,'
 the type is',type(data_b))

data_a

[[1 2 3]

[4 5 6]]

the type is <class ‘numpy.ndarray’>

data_b

[[1 2 3]

[4 5 6]]

the type is <class ‘numpy.ndarray’>

我们可以看到这一过程把原本为矩阵的 a 变为数组型了
如果想同时保存 a b 到同一个文件,我们可以用 np.savez() 函数,具体用法如下:

np.savez('ab.npz',k_a=a,k_b=b)
c=np.load('ab.npz')

print (c['k_a'])
print (c['k_b'])

[[1 2 3]
[4 5 6]]
[[1 2 3]
[4 5 6]]

这时的 c 是一个字典,需要通过关键字取出我们需要的数据

下面我们来认识下 scipy.io.savemat() 和 scipy.io.loadmat()
首先我们用 scipy.io.savemat() 创建 .mat 文件,该函数有两个参数,一个文件名和一个包含变量名和取值的字典.

import numpy as np
from scipy import io

a=np.mat('1,2,3;4,5,6')
b=np.array([[1,1,1],[2,2,2]])

io.savemat('a.mat', {'matrix': a})
io.savemat('b.mat', {'array': b})

至此 Python 的当前工作路径下就多了 a.mat 和 b.mat 这两个文件.
下面我们用 Matlab 读取这两个文件
可以看到 Matlab 已成功读取 Python 生成的 .mat 文件.
在这里插入图片描述
我们在来看看 Python 是怎么读取 .mat 文件的。首先来读取刚才生成的 a.mat

c=io.loadmat('a.mat')

print (type(c))
print (c)

dict {‘version’: ‘1.0’, ‘globals’: [], ‘header’: b’MATLAB
5.0 MAT-file Platform: nt, Created on: Tue Aug 4 16:49:28 2015’, ‘a_matrix’: array([[1, 2, 3],[4, 5, 6]])}

所以 Python 读取.mat 文件后返回的是个字典,如果要访问里面的值,就要用到关键字,如:

print(c['a_matrix'])

[[1 2 3] [4 5 6]]

当然了,Python 也可以读取 Matlab 创建的 .mat 文件,从而可以把他们设置在同一工作路径下,在必要的时候进行数据的共享.

原文地址:https://www.cnblogs.com/gmhappy/p/11863972.html