0

考虑下面的代码:

class imarray(np.ndarray):
    def __new__(subtype, shape, dtype=float, buffer=None, offset=0,
          strides=None, order=None):
        if isinstance(shape, np.ndarray):
            obj = shape #doesn't convert subtype.......
        else:
            obj = np.ndarray.__new__(subtype, shape, dtype, buffer, offset, strides,
                             order)
        return obj

    def __getitem__(self, key):
        return np.ndarray.__getitem__(self, key)





z = np.zeros([2,3])
x = imarray((2,3))
y = imarray(z)

print(y, type(y))
print(x, type(x))

该行y = imarray(z)应该只创建一个副本并更改数组的类型。(但 imarray 是 ndarray 的子类,无论如何它应该始终有效)。

如何做到这一点?

4

1 回答 1

0

尝试 :

obj = shape.view(imarray)
于 2015-03-30T22:01:24.307 回答