Custom data types in numpy arrays

If your Kernel class has a predictable amount of member data, then you could define a dtype for it instead of a class. e.g. if it’s parameterized by 9 floats and an int, you could do

kerneldt = np.dtype([('myintname', np.int32), ('myfloats', np.float64, 9)])
arr = np.empty(dims, dtype=kerneldt)

You’ll have to do some coercion to turn them into objects of class Kernel every time you want to manipulate methods of a single kernel but that’s one way to store the actual data in a NumPy array. If you want to only store a reference, then the object dtype is the best you can do without subclassing ndarray.

Leave a Comment