以下のコードを検討してください。
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 のサブクラスであり、これは常に機能するはずです)。
どうすればこれを行うことができますか?