Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom data types in numpy arrays

Tags:

I'm creating a numpy array which is to be filled with objects of a particular class I've made. I'd like to initialize the array such that it will only ever contain objects of that class. For example, here's what I'd like to do, and what happens if I do it.

class Kernel:     pass  >>> L = np.empty(4,dtype=Kernel)  TypeError: data type not understood 

I can do this:

>>> L = np.empty(4,dtype=object) 

and then assign each element of L as a Kernel object (or any other type of object). It would be so neat were I able to have an array of Kernels, though, from both a programming point of view (type checking) and a mathematical one (operations on sets of functions).

Is there any way for me to specify the data type of a numpy array using an arbitrary class?

like image 900
Mike Dewar Avatar asked Feb 28 '10 04:02

Mike Dewar


People also ask

Can NumPy array have different data types?

Can an array store different data types? Yes, a numpy array can store different data String, Integer, Complex, Float, Boolean.

How do I change the datatype of a NumPy array?

We have a method called astype(data_type) to change the data type of a numpy array. If we have a numpy array of type float64, then we can change it to int32 by giving the data type to the astype() method of numpy array. We can check the type of numpy array using the dtype class.

Do NumPy arrays have to have the same data type?

The elements in a NumPy array are all required to be of the same data type, and thus will be the same size in memory. The exception: one can have arrays of (Python, including NumPy) objects, thereby allowing for arrays of different sized elements.


1 Answers

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.

like image 54
dwf Avatar answered Sep 28 '22 09:09

dwf