import numpy as np a = np.array([1,2,3,4,5]) # 保存到 outfile.npy 文件上np.save('outfile.npy',a)
# 我们可以查看文件内容:$ cat outfile.npy ?NUMPYv{'descr': '<i8', 'fortran_order': False, 'shape': (5,), }
# 可以看出文件是乱码的,因为它们是 Numpy 专用的二进制格式后的数据。NDArray a = manager.create(new int[]{1,2,3,4,5});NDList encoded = new NDList(a);encoded.encode();OutputStream os = Files.newOutputStream(Paths.get("src/test/resources/outfile.npy"));encoded.encode(os, true);
# 输出结果如下:src/test/resources/outfile.npyimport numpy as np b = np.load('outfile.npy') print (b)
# 输出结果如下:[1 2 3 4 5]byte[] data = readFile("arr.npy");NDList decoded = NDList.decode(manager, data);NDArray array = decoded.get(0);System.out.println(array.toDebugString(100, 10, 100, 100));
# 输出结果如下:ND: (5) cpu() int32[ 1, 2, 3, 4, 5]import numpy as np a = np.array([[1,2,3],[4,5,6]])b = np.arange(0, 1.0, 0.1)
np.savez("runoob.npz", a, b)
# 输出结果如下:runoob.npza = manager.create(new int[][]{{1, 2, 3}, {4, 5, 6}});NDArray b = manager.arange(0f, 1f, 0.1f);encoded = new NDList(a, b);encoded.encode();os = Files.newOutputStream(Paths.get("src/test/resources/runoob.npz"));encoded.encode(os, true); # 输出结果如下:src/test/resources/runoob.npzimport numpy as np
r = np.load("runoob.npz") print(r.files) # 查看各个数组名称print(r["arr_0"]) # 数组 aprint(r["arr_1"]) # 数组 b
# 输出结果如下:['sin_array', 'arr_0', 'arr_1'][[1 2 3] [4 5 6]][0. 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9]data = readFile("runoob.npz");decoded = NDList.decode(manager, data);a = decoded.get(0);b = decoded.get(1);System.out.println(a.toDebugString(100, 10, 100, 100));System.out.println(b.toDebugString(100, 10, 100, 100)); # 输出结果如下:ND: (2, 3) cpu() int32[[ 1, 2, 3], [ 4, 5, 6],]
ND: (10) cpu() float32[0. , 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9]