简体   繁体   中英

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:

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

My question is, can we make this array point its scalars to same object in memory? 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...

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM