2017-09-30 22 views
1

配列内の負のインデックス付けを防ぐことを目的としました。TypeError:必須の引数が見つかりません__getitem__ numpy

import numpy as np 

class Myarray (np.ndarray): 
    def __getitem__(self,n): 
     if n<0: 
      raise IndexError("...") 
     return np.ndarray.__getitem__(self,n) 

class Items(Myarray): 
    def __init__(self): 
     self.load_tab() 

class Item_I(Items): 
    def load_tab(self): 
     self.tab=np.load("file.txt") 

a=Item_I() 

私はエラーましインスタンスを作成する場合:あなたはそれも__init__を呼び出そうとする前に、新しいインスタンスとnumpy.ndarray requires several arguments in __new__を作成するために__new__を使用するクラスからサブクラスためだ

in <module> 
    a=Item_I() 

TypeError: Required argument 'shape' (pos 1) not found 

答えて

1

Parameters for the __new__ method

shape : tuple of ints

Shape of created array. 

dtype : data-type, optional

Any object that can be interpreted as a numpy data type. 

buffer : object exposing buffer interface, optional

Used to fill the array with data. 

offset : int, optional

Offset of array data in buffer. 

strides : tuple of ints, optional

Strides of data in memory. 

order : {‘C’, ‘F’}, optional

Row-major (C-style) or column-major (Fortran-style) order. 

ただし、NumPyのドキュメントには、Subclassing ndarrayのページ全体が含まれています。

あなたはおそらくちょうどMyarrayからviewMyarrayの代わりに、サブクラス化を使用する必要があります。

tab=np.load("file.txt") 
tab.view(Myarray) 
関連する問題