[英]Numpy appending arrays
I am trying to to grow an array/matrix with each iteration within a for loop. 我正在尝试在for循环中的每次迭代中增加数组/矩阵。 Following is my code
以下是我的代码
import numpy as np
sino = [];
for n in range(0, 4):
fileName = 'D:\zDeflectionBP\data\headData_zdef\COSWeighted_trunk_' + str(n) + '.bin'
f = open(fileName, "rb")
data = np.fromfile(f, np.float32)
sino = np.append(sino, data)
f.close()
fileName = 'D:\zDeflectionBP\data\headData_zdef\Head_FFS_COSWeighted.bin'
f = open(fileName, "wb")
f.write(bytes(sino))
f.close()
Each iteration the data
is loaded witThere four 每次迭代
data
加载有四个
However, in the end, I found the size (in terms of number of bytes) of sino is twice as it should be. 但是,最后,我发现sino的大小(以字节数计)是应该的两倍。
For example: Each size of data
: 3MB then, since I have four data
, the size of the sino should be: 3MB X 4 = 12MB. 例如:每个
data
大小:3MB,那么,由于我有四个data
,因此,sino的大小应为:3MB X 4 = 12MB。 But I found the size of the size is 24MB. 但是我发现该大小的大小是24MB。
What is happening here? 这是怎么回事 I'd like for
sino
to be only 12MB, which only contains data from the four data
variable. 我希望
sino
只有12MB,仅包含来自四个data
变量的data
。 How should I do it? 我该怎么办? Thanks.
谢谢。
Your sino
isn't a numpy array initially but a Python list. 你的
sino
是不是numpy的阵列最初但Python列表。
Numpy converts it to a 64 bit array the first time by default on a 64 bit installation, after that it stays that way, twice as large as you expected. 默认情况下,在64位安装中,Numpy会在默认情况下首次将其转换为64位数组,之后它将保持这种状态,是您预期的两倍。
All the times you append data it's converted to 64 bit, since that's the format of the target. 附加数据的所有时间都会被转换为64位,因为这是目标的格式。
Make sino
a np.float32
array right from the start to solve the problem. 从一开始
np.float32
sino
设为np.float32
数组即可解决该问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.