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

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.

NumPy是一个强大的Python库,主要用于科学计算。它提供了一个高性能的多维数组对象ndarray以及用于操作这些数组的工具。下面我们将详细介绍如何使用NumPy进行数据存取。 ### 使用`numpy.save`保存单个数组到文件 ```python import numpy as np # 创建一个示例数组 data = np.array([[1, 2], [3, 4]]) # 将数组保存为 .npy 文件 np.save('example.npy', data) ``` 通过以上代码片段可以看到我们创建了一组简单的二维数组,并将其保存成了名为 `example.npy` 的二进制文件。这种格式非常适合快速读写大型数值型数据集并且保持良好的性能特点。 ### 加载已存储的数据 当需要从 `.npy` 文件加载之前保存过的 NumPy 数组时可以这样做: ```python loaded_data = np.load('example.npy') print(loaded_data) # 输出应显示相同的矩阵 [[1 2] # [3 4]] ``` 如果想要将多个数组一起打包成一个`.npz`压缩包的形式,则可通过以下方式进行处理: #### 存储多个数组至单一归档文件(.npz) ```python a = np.arange(5) b = np.linspace(0., 2.*np.pi, 6) # 把两个数组作为关键字参数传入,键名即为它们将在档案中的名称。 np.savez("arrays.npz", a=a, b=b) ``` 此时会生成一个新的压缩文件叫做"arrays.npz". 为了恢复这个压缩文档里的所有内容你需要做的是这样的: ```python archive = np.load('arrays.npz') for key in archive.files: print(f"{key}: {archive[key]}") ``` 除了上述提到的方式外,还可以直接利用文本形式来进行简单的小规模数字列表交换如 CSV 或者 TSV 等常用表格结构化信息载体也都是可行的选择之一。 对于文本文件(例如CSV),你可以选择使用`genfromtxt()`或`loadtxt()`, 它们都能很好地应对这类任务需求。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值