本文介绍了如何在 Numpy 中就地扩展数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,我有一些这样的代码

Currently, I have some code like this

import numpy as np
ret = np.array([])
for i in range(100000):
  tmp =  get_input(i)
  ret = np.append(ret, np.zeros(len(tmp)))
  ret = np.append(ret, np.ones(fixed_length))

我认为这段代码效率不高,因为np.append需要返回数组的副本而不是就地修改ret

I think this code is not efficient as np.append needs to return a copy of the array instead of modify the ret in-place

我想知道是否可以将 extend 用于这样的 numpy 数组:

I was wondering whether I can use the extend for a numpy array like this:

import numpy as np
from somewhere import np_extend
ret = np.array([])
for i in range(100000):
  tmp =  get_input(i)
  np_extend(ret, np.zeros(len(tmp)))
  np_extend(ret, np.ones(fixed_length))

这样 extend 会更有效率.有没有人对此有想法?谢谢!

So that the extend would be much more efficient.Does anyone have ideas about this?Thanks!

推荐答案

想象一个 numpy 数组占据一个连续的内存块.现在想象一下其他对象,比如其他 numpy 数组,它们占用我们 numpy 数组左右两侧的内存.将没有空间附加或扩展我们的 numpy 数组.numpy 数组中的底层数据始终占用连续块内存.

Imagine a numpy array as occupying one contiguous block of memory. Now imagine other objects, say other numpy arrays, which are occupying the memory just to the left and right of our numpy array. There would be no room to append to or extend our numpy array. The underlying data in a numpy array always occupies a contiguous block of memory.

因此,任何追加或扩展我们的 numpy 数组的请求只能通过分配一个全新的更大的内存块,将旧数据复制到新块中,然后追加或扩展来满足.

So any request to append to or extend our numpy array can only be satisfied by allocating a whole new larger block of memory, copying the old data into the new block and then appending or extending.

所以:

  1. 它不会就地发生.
  2. 效率不高.

这篇关于如何在 Numpy 中就地扩展数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 00:47