简体   繁体   English

是否可以从指向 memory 中相同 object 的数据创建 numpy 数组

[英]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.

相关问题 是否可以同时让 numpy 数组值和对象属性指向内存中的相同位置? - Is it possible to have both numpy array values and object atribute pointing to the same position in memory? 使用稀疏数据的列表消耗的内存少于与numpy数组相同的数据 - List with sparse data consumes less memory then the same data as numpy array 如何在内存中创建一个新的数据对象,而不是指向一个? (在Python 3中) - How to create a new data object in memory, rather than pointing to one? (in Python 3) 根据一行数据创建一个numpy数组 - create a numpy array from data on a row 尽管数据具有相同的维度,但无法将 numpy 数组转换为类型:浮点数:Object - Cannot convert numpy array to type: float from type : Object despite the data being of same dimension 从可能的 numpy 数组形成 numpy 数组 - form numpy array from possible numpy array 如何在Python中将数据从内存复制到numpy数组 - How to copy data from memory to numpy array in Python 指向相同阵列的实例 - Instances pointing to the same array 如何创建其他相同长度和不同长度的numpy数组的numpy _object_数组? - How to create a numpy _object_ array of other numpy arrays of same and different length? 是否可以将数据从Numpy数组直接复制到MonetDB? - Is it possible to directly copy data from Numpy array to MonetDB?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM