[英]Is it possible to create a numpy array from data pointing to same object in memory
suppose we have two scalars with type of numpy.float32:假设我们有两个类型为 numpy.float32 的标量:
a
>>>1.0
type(a)
>>><class 'numpy.float32'>
b
>>>2.0
type(b)
>>><class 'numpy.float32'>
I am trying to create a numpy array from list([a,b,a,a,b,b,b,a,b,b,a,a,a,b,b,a,a]).我正在尝试从列表([a,b,a,a,b,b,b,a,b,b,a,a,a,b,b,a,a])创建一个 numpy 数组。
My question is, can we make this array point its scalars to same object in memory?我的问题是,我们可以让这个数组将其标量指向 memory 中的相同 object 吗? instead of copying them per stride of its shape?
而不是按其形状的步幅复制它们?
I have a very long array eg shape(1,30000), which consist of a few scalars (around 30) sequenced in different orders eg 1,2,3,2,1,3,2,1,2,1,3,3,2,2,3,1,2,1,1,3...我有一个很长的数组,例如 shape(1,30000),它由一些按不同顺序排列的标量(大约 30 个)组成,例如 1,2,3,2,1,3,2,1,2,1,3 ,3,2,2,3,1,2,1,1,3...
Creating this array take a huge amount of memory, but since they are actualy same scalars repeated, I thought there might be a way to load them once in memory and point array members to them.创建这个数组需要大量的 memory,但由于它们实际上是重复的相同标量,我认为可能有一种方法可以在 memory 中加载它们一次并将数组成员指向它们。
One way that might help you reduce half memory if your array size is smaller than 32767 (which is maximum int16 in numpy, try: np.iinfo(np.int16).max
) is to store array of indices of your values as int16 instead of array of values itself, with a cost of calling the value from another list.如果您的数组大小小于 32767(这是 numpy 中的最大 int16,请尝试:
np.iinfo(np.int16).max
)将您的值的索引数组存储为 int16值数组本身,需要从另一个列表中调用值。 Although this will be almost useless in a sense that you cannot leverage array calculations without making the array of values:尽管从某种意义上说这几乎是无用的,因为如果不创建值数组就无法利用数组计算:
values = np.array([1.0, 2.0, 3.0, 4.0], dtype=np.float32)
indices = np.array([1,2,3,2,1,3,2,1,2,1,3,3,2,2,3,1,2,1,1,3], dtype=np.int16)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.