【问题标题】:Subtypingclassing ndarray - how to construct from a normal ndarraySubtypingclassing ndarray - 如何从普通的ndarray构造
【发布时间】:2015-06-03 15:52:15
【问题描述】:

考虑下面的代码:

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 的子类,无论如何它应该始终有效)。

如何做到这一点?

【问题讨论】:

    标签: python numpy subtyping


    【解决方案1】:

    试试:

    obj = shape.view(imarray)
    

    【讨论】:

      猜你喜欢
      • 2014-02-01
      • 1970-01-01
      • 2016-03-23
      • 1970-01-01
      • 1970-01-01
      • 2018-06-05
      • 2018-04-12
      • 2018-10-10
      • 1970-01-01
      相关资源
      最近更新 更多