numpy.savez() 函数将多个数组保存到以 npz 为扩展名的文件中

numpy.savez() 函数将多个数组保存到以 npz 为扩展名的文件中。

numpy.savez(file, *args, **kwds)

参数说明:

  • file:要保存的文件,扩展名为 .npz,如果文件路径末尾没有扩展名 .npz,该扩展名会被自动加上。
  • args: 要保存的数组,可以使用关键字参数为数组起一个名字,非关键字参数传递的数组必须放在关键字数组的前面,非关键字数组会自动起名为 arr_0arr_1, … 。
  • kwds: 要保存的数组使用关键字名称。
 1 import numpy as np 
 2  
 3 a = np.array([[1,2,3],[4,5,6]])
 4 b = np.arange(0, 1.0, 0.1)
 5 c = np.sin(b)
 6 # c 使用了关键字参数 sin_array
 7 np.savez("runoob.npz",  b, go =a, sinabc = c)
 8 r = np.load("runoob.npz")  
 9 print(r.files) # 查看各个数组名称
10 print(r["arr_0"]) # 数组 a
11 print(r["go"]) # 数组 b
12 print(r["sinabc"]) # 数组 c
1 ['go', 'sinabc', 'arr_0']  此处会按关键字数组在前,非关键数组在后排列
2 [0.  0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9]
3 [[1 2 3]
4  [4 5 6]]
5 [0.         0.09983342 0.19866933 0.29552021 0.38941834 0.47942554
6  0.56464247 0.64421769 0.71735609 0.78332691]

 

posted @ 2021-02-22 12:46  霜火  阅读(1859)  评论(0)    收藏  举报