Python For Data Analysis-四章第四节
《Python For Data Analysis》的第四章第四节比较简单,讲述的是array数组数据存储到文件和读回到数组里的基本操作。array数组直接写入文件可以用save和savez函数,文件的后缀分别是.npy和.npz,而从这两类文件里读回数据到数组都用load函数即可。
import numpy as np
from numpy.random import shuffle
xxx = np.arange(-5, 5)
yyy = np.arange(-3, 15)
zzz = np.arange(24).reshape((4, 6))
print zzz, "# zzz"
print "-" * 50
np.save("out.npy", zzz)
shuffle(zzz)
print zzz, "# zzz shuffle"
print "-" * 50
np.savez("out.npz", a = xxx, b = yyy, c = zzz)
b = np.load("out.npy")
print b, "# b"
print "-" * 50
c = np.load("out.npz")
print c, "# c"
print "-" * 50
#print dir(c)
print type(c.items()), "# type(c.items())"
print "-" * 50
print c.files, "# c.files"
print "-" * 50
print c.values(),"# c.values()"
print "-" * 50
print c['a'], "# c['a']"
print c['b'], "# c['b']"
print c['c'], "# c['c']"
print "-" * 50
print c.items(), "# c.items()"
print "-" * 50
print c.items()[0][1], "# c.items()[0][1]"
执行结果:
[[ 0 1 2 3 4 5]
[ 6 7 8 9 10 11]
[12 13 14 15 16 17]
[18 19 20 21 22 23]] # zzz
--------------------------------------------------
[[ 6 7 8 9 10 11]
[12 13 14 15 16 17]
[18 19 20 21 22 23]
[ 0 1 2 3 4 5]] # zzz shuffle
--------------------------------------------------
[[ 0 1 2 3 4 5]
[ 6 7 8 9 10 11]
[12 13 14 15 16 17]
[18 19 20 21 22 23]] # b
--------------------------------------------------
<numpy.lib.npyio.NpzFile object at 0x7f909d2f7890> # c
--------------------------------------------------
<type 'list'> # type(c.items())
--------------------------------------------------
['a', 'c', 'b'] # c.files
--------------------------------------------------
[array([-5, -4, -3, -2, -1, 0, 1, 2, 3, 4]), array([[ 6, 7, 8, 9, 10, 11],
[12, 13, 14, 15, 16, 17],
[18, 19, 20, 21, 22, 23],
[ 0, 1, 2, 3, 4, 5]]), array([-3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,
14])] # c.values()
--------------------------------------------------
[-5 -4 -3 -2 -1 0 1 2 3 4] # c['a']
[-3 -2 -1 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14] # c['b']
[[ 6 7 8 9 10 11]
[12 13 14 15 16 17]
[18 19 20 21 22 23]
[ 0 1 2 3 4 5]] # c['c']
--------------------------------------------------
[('a', array([-5, -4, -3, -2, -1, 0, 1, 2, 3, 4])), ('c', array([[ 6, 7, 8, 9, 10, 11],
[12, 13, 14, 15, 16, 17],
[18, 19, 20, 21, 22, 23],
[ 0, 1, 2, 3, 4, 5]])), ('b', array([-3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,
14]))] # c.items()
--------------------------------------------------
[-5 -4 -3 -2 -1 0 1 2 3 4] # c.items()[0][1]
- save函数只能存储一个数组
np.save("out.npy", zzz)
,而savez可以存储多个数组np.savez("out.npz", a = xxx, b = yyy, c = zzz)
且压缩,等号前的是数据的索引、类似与字典files记录这等号前的索引(键),values函数可以获得各个值,而items函数获得键和值数据,load以后可以通过$a$找到数据$xxx$数组的值c['a']
。 - loadtxt等函数可以参考本站NumPy部分。