numpy数据合并和数据的存取np.save, np.load,np.savetxt, np.loadtxt,np.savez, np.load

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/weixin_38208741/article/details/84463359

=====以下是关于numpy数组的合并操作

import numpy as np

a=np.arrange(9).reshape(3,-1)

>>> a
array([[0, 1, 2],
       [3, 4, 5],
       [6, 7, 8]])

b=np.arange(10,19).reshape(3,-1)

>>> b
array([[10, 11, 12],
       [13, 14, 15],
       [16, 17, 18]])

np.row_stack((a, b))  #按行的形式进行保存

>>> np.row_stack((a, b))
array([[ 0,  1,  2],
       [ 3,  4,  5],
       [ 6,  7,  8],
       [10, 11, 12],
       [13, 14, 15],
       [16, 17, 18]])

np.row_stack((a, b))  #按列的形式进行保存

>>> np.column_stack((a, b))
array([[ 0,  1,  2, 10, 11, 12],
       [ 3,  4,  5, 13, 14, 15],
       [ 6,  7,  8, 16, 17, 18]])

  

=====以下是关于numpy数据的存储和读取,来源于:https://blog.csdn.net/zz2230633069/article/details/81363630

import numpy as np
np.load(vgg16_npy_path, encoding='latin1').item()

np.load(vgg16_npy_path, encoding='latin1')#是一个ndrarry的类型(该类型是numpy的,类似于tensor类型是tensorflow的)

np.load(vgg16_npy_path, encoding='latin1').item()#是一个dict类型,进行了转换

NumPy 是一个 Python 包。 它代表 “Numeric Python”。 它是一个由多维数组对象和用于处理数组的例程集合组成的库。

使用NumPy,开发人员可以执行以下操作:

        数组的算数和逻辑运算等操作。

        傅立叶变换和图形操作。

        与线性代数有关的操作。 NumPy 拥有线性代数和随机数生成的内置函数。

        数据的存取

数据的存取:

numpy提供了便捷的内部文件存取,将数据存为np(numpy)专用的npy(二进制格式)或npz(压缩打包格式)格式

    npy格式以二进制存储数据的,在二进制文件第一行以文本形式保存了数据的元信息(维度,数据类型),可以用二进制工具查看查看内容

    npz文件以压缩打包文件存储,可以用压缩软件解压

对各个格式文件的存取如下

    np格式:np.save,  np.load

    znp格式:np.savez,  np.load

    csv文件:np.savetxt,  np.loadtxt

    详细参数的设定参考保存和加载数据

对数据的存取可以参考numpy文件存取
格式:numpy.load(file, mmap_mode=None, allow_pickle=True, fix_imports=True, encoding='ASCII')

    参数:

    file : file-like object, string, or pathlib.Path. The file to read. File-like objects must support the seek() and read() methods. Pickled files require that the file-like object support the readline() method as well.

    mmap_mode : {None, ‘r+’, ‘r’, ‘w+’, ‘c’}, optional.  If not None, then memory-map the file, using the given mode (
    ‘r’     Open existing file for reading only.
    ‘r+’     Open existing file for reading and writing.
    ‘w+’     Create or overwrite existing file for reading and writing.
    ‘c’     Copy-on-write: assignments affect data in memory, but changes are not saved to disk. The file on disk is read-only.

    ). A memory-mapped array is kept on disk. However, it can be accessed and sliced like any ndarray. Memory mapping is especially useful for accessing small fragments of large files without reading the entire file into memory.

    allow_pickle : bool, optional. Allow loading pickled object arrays stored in npy files. Reasons for disallowing pickles include security, as loading pickled data can execute arbitrary code. If pickles are disallowed, loading object arrays will fail. Default: True

    fix_imports : bool, optional.  Only useful when loading Python 2 generated pickled files on Python 3, which includes npy/npz files containing object arrays. If fix_imports is True, pickle will try to map the old Python 2 names to the new names used in Python 3.

    encoding : str, optional.  What encoding to use when reading Python 2 strings. Only useful when loading Python 2 generated pickled files on Python 3, which includes npy/npz files containing object arrays. Values other than(除了) ‘latin1’, ‘ASCII’, and ‘bytes’ are not allowed, as they can corrupt numerical data. Default: ‘ASCII’

 返回值和引起的错误

    Returns:   result: array, tuple, dict, etc.   Data stored in the file. For .npz files, the returned instance of NpzFile class must be closed to avoid leaking file descriptors.

    Raises:     IOError: If the input file does not exist or cannot be read.

                       ValueError: The file contains an object array, but allow_pickle=False given.
--------------------- 
 

猜你喜欢

转载自blog.csdn.net/weixin_38208741/article/details/84463359
np