np.delete之后的深度复制。错误?

时间:2017-10-06 18:06:15

标签: python arrays numpy deep-copy

我有一个属性为2D列表的对象。 有时我会删除数组中的列。 有时我想在对象上使用深度复制。

然而,这给了我一个错误,如下面的例子所示。

import numpy as np
from copy import deepcopy 

创建列表数组

A = np.empty((2,3),list)
for i,j in np.ndindex(A.shape):
    A[i,j] = [i,j]

删除,复制。两个失败的版本

B = np.delete(A,[0],1)
C = deepcopy(B)
print("These two should be the same")
print(B[0,1])
print(C[0,1])

B = np.array(np.delete(A,[0],1))
C = deepcopy(B)
print("These two should be the same")
print(B[0,1])
print(C[0,1])

输出

These two should be the same
[0, 2]
[1, 1]
These two should be the same
[0, 2]
[1, 1]

以下版本按预期工作

B = np.delete(A,[0],1)
C = np.empty_like(B); C[:] = B
print("These two should be the same")
print(B[0,1])
print(C[0,1])

B = np.delete(A,0,1)
C = deepcopy(B)
print("These two should be the same")
print(B[0,1])
print(C[0,1])

B = np.delete(A,0,1)
C = B.copy()
print("These two should be the same")
print(B[0,1])
print(C[0,1])

B = np.ascontiguousarray(np.delete(A,[0],1))
C = deepcopy(B)
print("These two should be the same")
print(B[0,1])
print(C[0,1])

输出

These two should be the same
[0, 2]
[0, 2]
... etc 

通过查看数组的标志可以获得一些见解。但无论如何,这种行为是出乎意料的。另请注意,某些工作版本的版本失败非常相似。这是一个numpy bug吗?

0 个答案:

没有答案