码迷,mamicode.com
首页 > 其他好文 > 详细

numpy 文件读取

时间:2015-02-07 21:30:34      阅读:404      评论:0      收藏:0      [点我收藏+]

标签:

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.]])


 

numpy 文件读取

标签:

原文地址:http://www.cnblogs.com/cmhco/p/4279296.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!