numpy.savez_compressed

numpy.savez_compressed(file, *args, **kwds)[source]
Save several arrays into a single file in compressed .npz format.

Provide arrays as keyword arguments to store them under the corresponding name in the output file: savez(fn, x=x, y=y).

If arrays are specified as positional arguments, i.e., savez(fn, x, y), their names will be arr_0, arr_1, etc.

Parameters
filestr or file
Either the filename (string) or an open file (file-like object) where the data will be saved. If file is a string or a Path, the .npz extension will be appended to the filename if it is not already there.

argsArguments, optional
Arrays to save to the file. Please use keyword arguments (see kwds below) to assign names to arrays. Arrays specified as args will be named “arr_0”, “arr_1”, and so on.

kwdsKeyword arguments, optional
Arrays to save to the file. Each array will be saved to the output file with its corresponding keyword name.

Returns
None

例子:
test_array = np.random.rand(3, 2)
test_vector = np.random.rand(4)
np.savez_compressed(‘/tmp/123’, a=test_array, b=test_vector)
loaded = np.load(‘/tmp/123.npz’)
print(np.array_equal(test_array, loaded[‘a’]))
True
print(np.array_equal(test_vector, loaded[‘b’]))
True

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: `np.savez_compressed` 是 NumPy 库中的一个函数,用于将一个或多个 NumPy 数组保存到一个压缩的 `.npz` 文件中。 其语法如下: ``` np.savez_compressed(file, *args, **kwds) ``` 其中,`file` 是要保存数据的文件名,`*args` 是要保存的数组列表,`**kwds` 是一些可选的关键字参数。 与 `np.savez` 不同的是,`np.savez_compressed` 会使用压缩算法将数据存储到磁盘上,以减小文件大小。这对于存储大量数据时非常有用,可以节省磁盘空间和网络传输带宽。 例如,下面的代码将两个数组 `a` 和 `b` 保存到名为 `data.npz` 的压缩文件中: ``` python import numpy as np a = np.array([1, 2, 3]) b = np.array([4, 5, 6]) np.savez_compressed('data.npz', a=a, b=b) ``` 在这个例子中,`a` 和 `b` 是要保存的数组,`a=a` 和 `b=b` 是指定数组名称的关键字参数。文件保存在当前工作目录下,名为 `data.npz`。 ### 回答2: np.savez_compressed是一个NumPy函数,用于将多个NumPy数组保存到一个压缩文件中。与np.savez不同的是,该函数会自动对保存的数据进行压缩,从而减少文件大小。该函数的语法格式如下: np.savez_compressed(file, *args, **kwds) 其中,file表示保存的文件名,*args表示要保存的NumPy数组(可以是多个),**kwds表示可选的关键字参数。 np.savez_compressed函数通过使用zlib压缩算法对数据进行压缩,可以显著减少保存文件的大小,从而节省存储空间和数据传输时间。另外,对于存储大量数据的科学计算应用程序,使用np.savez_compressed函数可以更高效地管理和组织数据。 当需要保存多个NumPy数组时,可以使用np.savez_compressed函数,避免使用多个独立的np.save函数。这样可以简化代码、减少磁盘空间占用,并且方便读取保存的数据。 使用np.savez_compressed函数保存数据非常简单,只需要指定保存的文件名和要保存的NumPy数组即可。例如: import numpy as np a = np.array([1, 2, 3]) b = np.array([4, 5, 6]) c = np.array([7, 8, 9]) np.savez_compressed('data.npz', a, b, c) 这个例子将三个NumPy数组保存到名为data.npz的压缩文件中。在读取数据时,可以使用np.load函数来加载数据: data = np.load('data.npz') print(data['arr_0']) # 输出数组a的值 更多关于np.savez_compressed的使用方法和详细说明,请参考NumPy官方文档。 ### 回答3: np.savez_compressed是一个函数,主要用于保存多个numpy数组。与np.savez函数不同的是,它会使用压缩算法来减小保存文件的大小。 函数的使用方法为: ``` numpy.savez_compressed(file, *args, **kwds) ``` 其中,file是一个保存.npy文件的路径,*args代表要保存的numpy数组,可以有多个,在函数调用时用逗号隔开。**kwds则是可选参数,可以指定数组的名称,例如: ``` np.savez_compressed('my_arrays.npz', a=my_array1, b=my_array2) ``` 这样就会将my_array1和my_array2保存在文件my_arrays.npz中,并分别用a和b来标识。 当我们需要读取保存的.npy文件时,使用以下代码: ``` npzfile = np.load('my_arrays.npz') ``` 这将返回一个类似于字典的对象,其中包含被保存的numpy数组。我们可以通过数组名称来访问它们: ``` my_array1 = npzfile['a'] my_array2 = npzfile['b'] ``` np.savez_compressed函数的优点是它可以更好地利用存储空间,因为它使用的是压缩算法。但是,由于压缩需要一些时间,因此在保存大文件或大量数据时可能会花费更长的时间。因此,在需要快速保存和加载数据时,最好使用np.savez函数。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值