1import numpy as np
2
3arr = np.array([1,2,3,4])
4print(np.apply_along_axis(lambda x : x ** 2, 0, arr))
5
6#Output: array([ 1, 4, 9, 16])
1In [131]: A = np.random.randint(0,255,(512,512,3)) # 512x512 colored image
2
3In [132]: def org_app(A):
4 ...: out = np.zeros(A.shape)
5 ...: for i in range(A.shape[0]):
6 ...: for j in range(A.shape[1]):
7 ...: out[i,j] = chromaticity(A[i,j])
8 ...: return out
9 ...:
10
11In [133]: %timeit org_app(A)
121 loop, best of 3: 5.99 s per loop
13
14In [134]: %timeit np.apply_along_axis(chromaticity, 2, A) #@hpaulj's soln
151 loop, best of 3: 9.68 s per loop
16
17In [135]: %timeit np.log(A/np.power(np.sum(A,2,keepdims=True),1/3))
1810 loops, best of 3: 90.8 ms per loop