-
Notifications
You must be signed in to change notification settings - Fork 82
Expand file tree
/
Copy pathnumpy_save_savez.py
More file actions
102 lines (70 loc) · 1.59 KB
/
numpy_save_savez.py
File metadata and controls
102 lines (70 loc) · 1.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import numpy as np
print(np.__version__)
# 1.26.1
a = np.arange(6, dtype=np.int8).reshape(1, 2, 3)
print(a)
# [[[0 1 2]
# [3 4 5]]]
print(a.shape)
# (1, 2, 3)
print(a.dtype)
# int8
np.save('data/temp/np_save', a)
a_load = np.load('data/temp/np_save.npy')
print(a_load)
# [[[0 1 2]
# [3 4 5]]]
print(a_load.shape)
# (1, 2, 3)
print(a_load.dtype)
# int8
a1 = np.arange(5)
print(a1)
# [0 1 2 3 4]
a2 = np.arange(5, 10)
print(a2)
# [5 6 7 8 9]
np.savez('data/temp/np_savez', a1, a2)
npz = np.load('data/temp/np_savez.npz')
print(type(npz))
# <class 'numpy.lib.npyio.NpzFile'>
print(npz.files)
# ['arr_0', 'arr_1']
print(npz['arr_0'])
# [0 1 2 3 4]
print(npz['arr_1'])
# [5 6 7 8 9]
np.savez('data/temp/np_savez_kw', x=a1, y=a2)
npz_kw = np.load('data/temp/np_savez_kw.npz')
print(npz_kw.files)
# ['x', 'y']
print(npz_kw['x'])
# [0 1 2 3 4]
print(npz_kw['y'])
# [5 6 7 8 9]
np.savez('data/temp/np_savez_kw2', a1, y=a2)
npz_kw2 = np.load('data/temp/np_savez_kw2.npz')
print(npz_kw2.files)
# ['y', 'arr_0']
print(npz_kw2['arr_0'])
# [0 1 2 3 4]
print(npz_kw2['y'])
# [5 6 7 8 9]
np.savez_compressed('data/temp/np_savez_comp', a1, a2)
npz_comp = np.load('data/temp/np_savez_comp.npz')
print(type(npz_comp))
# <class 'numpy.lib.npyio.NpzFile'>
print(npz_comp.files)
# ['arr_0', 'arr_1']
print(npz_comp['arr_0'])
# [0 1 2 3 4]
print(npz_comp['arr_1'])
# [5 6 7 8 9]
np.savez_compressed('data/temp/np_savez_comp_kw', x=a1, y=a2)
npz_comp_kw = np.load('data/temp/np_savez_comp_kw.npz')
print(npz_comp_kw.files)
# ['x', 'y']
print(npz_comp_kw['x'])
# [0 1 2 3 4]
print(npz_comp_kw['y'])
# [5 6 7 8 9]