I've started using NumPy lately (and it is really useful so far -- why didn't I start using it sooner?), but there are still a few things which I'm sure I'm doing wrong:
How do you apply a function to a vector (ndarray)? I'd like it to be vectorized, and its a more complex function than just multiplication. I've tried using
np.where, but that just seems to be an ugly solution (and I'm not sure if its vectorized either). Is there a function similar to map such that my function would be vectorized?Suppose I have a two-dimensional array -- just an array of 2d position vectors, and suppose I want to find the norm of each vector, and if it is over a certain value, do whatever.
np.whereseems to be a great solution for this but it won't work unless I transpose the array:np.where(sum(a.T**2) > 10, a * 2, 0)(Just an arbitrary example). This really seems verbose and the transpose doesn't make much sense.Finally, if I have a three-dimensional array - a two dimensional array of 2d position vectors (such that at position [1, 2] would be the vector (1, 2)). How can I iterate through all of the vectors (with a vectorized function such as np.where)? I've been using
np.reshape, but I'd like to keep the array the same shape if possible.
I grouped these questions together because they seem similar, but if I need to split them into separate threads, please let me know and I'll do so.
pandaswhich is set up to handle many of these operations. - Jeff Tratner