简体   繁体   English

numpy reshape 会创建副本吗?

[英]Does numpy reshape create a copy?

Is there a way to do a reshape on numpy arrays but inplace.有没有办法对 numpy 数组进行重塑但就地。 My problem is that my array is very big so any unnecessary copies strain the memory.我的问题是我的数组非常大,所以任何不必要的副本都会使内存紧张。

My current approach is like this:我目前的做法是这样的:

train_x = train_x.reshape(n,32*32*3)

this doesn't exactly solve the problem since it creates a new array and then attributes the label train_x to the new array.这并不能完全解决问题,因为它创建了一个新数组,然后将标签train_x于新数组。

In a normal case this would be ok, since the garbage collector would very soon collect the original array.在正常情况下,这没问题,因为垃圾收集器很快就会收集原始数组。

The problem is that I have something like this:问题是我有这样的事情:

train_x, train_y = train_set
train_x = train_x.reshape(n,32*32*3)

So in this case even though the train_x no longer points to the original array, there is still a pointer to the original array inside of train_set .所以在这种情况下,即使train_x不再指向原始数组,仍然有一个指向train_set内部原始数组的train_set

I want a way that changes all pointers of the previous array to this new array.我想要一种将前一个数组的所有指针更改为这个新数组的方法。 Is there a way?有办法吗?

Or maybe there is some other way of dealing with this?或者也许有其他方法来处理这个问题?

For Python keep in mind that several variables or names can point to the same object, such as a numpy array.对于 Python,请记住多个变量或名称可以指向同一个对象,例如 numpy 数组。 Arrays can also have views, which are new array objects, but with shared data buffers.数组也可以有视图,它们是新的数组对象,但具有共享数据缓冲区。 A copy has its own data buffer.副本有自己的数据缓冲区。

In [438]: x = np.arange(12)
In [439]: y = x                # same object
In [440]: y.shape = (2,6)      # inplace shape change
In [441]: y
Out[441]: 
array([[ 0,  1,  2,  3,  4,  5],
       [ 6,  7,  8,  9, 10, 11]])
In [442]: x
Out[442]: 
array([[ 0,  1,  2,  3,  4,  5],
       [ 6,  7,  8,  9, 10, 11]])
In [443]: y = y.reshape(3,4)        # y is a new view
In [444]: y
Out[444]: 
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11]])
In [445]: x
Out[445]: 
array([[ 0,  1,  2,  3,  4,  5],
       [ 6,  7,  8,  9, 10, 11]])

y has a different shape, but shares the data buffer: y具有不同的形状,但共享数据缓冲区:

In [446]: y += 1
In [447]: y
Out[447]: 
array([[ 1,  2,  3,  4],
       [ 5,  6,  7,  8],
       [ 9, 10, 11, 12]])
In [448]: x
Out[448]: 
array([[ 1,  2,  3,  4,  5,  6],
       [ 7,  8,  9, 10, 11, 12]])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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