numpy 文件读取
tofile() fromfile()
>>a = np.arange(0,12) >>a.shape = 3,4
>>a array([[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11]]) >>a.tofile("a.bin") >>b.fromfile("a.bin",dtype=np.int32) >>b array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]) >>b.reshape = 3,4
>>b
array([[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11]])
这种方法读入数据时,必须明确dtype,然后再reshape,过于麻烦
load() save()
>>np.save("a.npy",a)
>>c = np.load("a.npy")
>>c
array([[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11]])
savetxt() loadtxt()
>>a = np.arange(0,12,0.5).reshape(4,-1)
>>np.savetxt("a.txt",a,fmt="%d",delimiter=",")
>>np.loadtxt("a.txt",delimiter=",")
array([[ 0., 0., 1., 1., 2., 2.],
[ 3., 3., 4., 4., 5., 5.],
[ 6., 6., 7., 7., 8., 8.],
[ 9., 9., 10., 10., 11., 11.]])