Skip to content

Tag: vectorization

Python dataframe vectorizing for loop

I would like to vectorize this piece of python code with for loop conditioned on current state for speed and efficiency. values for df_B are computed based on current-state (state) AND corresponding df_A value. Any ideas would be appreciated. Answer This seems overkill. Your state variable basically is the pr…

bootstrap numpy 2D array

I am trying to sample with replacement a base 2D numpy array with shape of (4,2) by rows, say 10 times. The final output should be a 3D numpy array. Have tried the code below, it works. But is there a way to do it without the for loop? Answer Here’s one vectorized approach – Basic idea is that we

Vectorize over the rows of an array

I have an array X and I want to apply a function f to all the rows of X: Now, y should be array([15,30], ‘i’). Which method or slicing magic will implement rows in the most efficient way? Answer NumPy implements the concept of “action over a particular axis”. The general function is nu…