I have a function which takes a two element array as input. Now I have large data (shape = (360000,2)) and want to evaluate the function at each point by using numpy.apply_along_axis . One of the answers given in the this thread(numpy np.apply_along_axis function speed up?) says that numpy.apply_along_axis is not for speed. My function is vectorised. How can I improve the evolution time for all my data without using jit/cython.
I will include a sample code of what I'm trying to do exaclty
import numpy as np
import random
def sample(x):
return np.sin(x[0])*np.cos(x[1])
data = np.random.normal(size=600*600*2)
data = data.reshape(600*600,2)
%timeit np.sum(np.apply_along_axis(sample, 1,data)) #using the apply_along_axis
def loop_way(): # using loop
result = []
for i in data:
result += [sample(i)]
return np.sum(result)
%timeit loop_way()
output when using np. apply_along_axis: 1 loop, best of 3: 4.06 s per loop
output for loop_way function: 1 loop, best of 3: 2.41 s per loop