You can refer to https://numpy.org/doc/stable/user/basics.subclassing.html#module-numpy.doc.subclassing (official).
Below is a simple implementation example.
class Element:
def __init__(self, r, c):
self.r = r
self.c = c
def __repr__(self):
return f'Element({self.r},{self.c})'
Suppose you want to use Element (r, c) in the r row and c column of the above self-made class as an element of a 2D ndarray.
Create the MyNdarray class by inheriting np.ndarray as follows.
import numpy as np
class MyNdarray(np.ndarray):
def __new__(cls, dimension):
shape = (dimension, dimension)
return super().__new__(cls, shape, dtype=Element)
def __init__(self, dimension):
for r in range(dimension):
for c in range(dimension):
self[r, c] = Element(r, c)
Create a numpy ndarray instance with shape (dimension * dimension) with __new__
, and perform the initial operation (constructor) with __init__
.
a = MyNdarray(3)
>> MyNdarray([[Element(0,0), Element(0,1), Element(0,2)],
[Element(1,0), Element(1,1), Element(1,2)],
[Element(2,0), Element(2,1), Element(2,2)]], dtype=object)
a[0, 0]
>> Element(0,0)
It would be nice to be able to use the slicing operation and transpose function of ndarray as it is.
a[:, 0:2]
>> MyNdarray([[Element(0,0), Element(0,1)],
[Element(1,0), Element(1,1)],
[Element(2,0), Element(2,1)]], dtype=object)
a.T
>> MyNdarray([[Element(0,0), Element(1,0), Element(2,0)],
[Element(0,1), Element(1,1), Element(2,1)],
[Element(0,2), Element(1,2), Element(2,2)]], dtype=object)
Also, if you want to add an attribute to the MyNdarray
class, use the __array_finalize__
function.
import numpy as np
class MyNdarray(np.ndarray):
def __new__(cls, dimension):
shape = (dimension, dimension)
obj = super().__new__(cls, shape, dtype=Element)
obj.dimension = dimension
return obj
def __init__(self, dimension):
for r in range(dimension):
for c in range(dimension):
self[r, c] = Element(r, c)
def __array_finalize__(self, obj):
if obj is None:
return
self.dimension = getattr(obj, 'dimension', None)
a = MyNdarray(3)
a.dimension
>>> 3
Recommended Posts