Brad Spendlove Brad Spendlove - 1 year ago 138
Python Question

Numpy: how to apply vectorized functions to array with dtype

I'm following this tutorial on how to use numpy to manipulate images. When I load the sample image using scipy, I get a 2D array of RGB tuples, with a dtype value appended on the end.

array([[7, 8, 5],
[3, 5, 7]], dtype=uint8)

I wrote a function and vectorized it

def myfunc(a, b):
return a + 2

vfunc = np.vectorize(myfunc)

but when I apply it to my array, the result doesn't have the dtype

array([[9, 10, 7],
[5, 7, 9]])

My guess is that because "dtype + 2" isn't defined, it's just losing that element of the array.

How can I write a function that will not strip the dtype when I vectorize it and apply it to a numpy array?

Answer Source

dtype=uint8 is not an element of the array. It is just a thing that gets printed to let you know that the array is of type np.uint8.

The default types np.float_ and np.int_ do not get a printout like that, which is what you are seeing in the second case. The way you can tell float and int arrays apart is that float arrays will always have decimal points in the numbers.

The reason that this is happening is that you are adding 2 to each element of your array. Since 2 is an integer, the output array gets promoted to np.int_ type and you do not get an explicit dtype printout.

You can try the following experiment: redefine myfunc to add a np.uint8 instead of an integer to the array elements and try to print the result:

def myfunc(a, b):
    return a + np.uint8(2)

Finally, keep in mind that vectorizing Python code is usually not the best way to get things done. The function itself will be a Python function, and therefore slow. It is generally better to find a way of performing whatever operations you want with numpy functions.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download