Applying a Function Along a Numpy Array

Applying a function along a numpy array

Function numpy.apply_along_axis is not good for this purpose.
Try to use numpy.vectorize to vectorize your function: https://docs.scipy.org/doc/numpy/reference/generated/numpy.vectorize.html
This function defines a vectorized function which takes a nested sequence of objects or numpy arrays as inputs and returns an single or tuple of numpy array as output.

import numpy as np
import math

# custom function
def sigmoid(x):
return 1 / (1 + math.exp(-x))

# define vectorized sigmoid
sigmoid_v = np.vectorize(sigmoid)

# test
scores = np.array([ -0.54761371, 17.04850603, 4.86054302])
print sigmoid_v(scores)

Output: [ 0.36641822 0.99999996 0.99231327]

Performance test which shows that the scipy.special.expit is the best solution to calculate logistic function and vectorized variant comes to the worst:

import numpy as np
import math
import timeit

def sigmoid_(x):
return 1 / (1 + math.exp(-x))
sigmoidv = np.vectorize(sigmoid_)

def sigmoid(x):
return 1 / (1 + np.exp(x))

print timeit.timeit("sigmoidv(scores)", "from __main__ import sigmoidv, np; scores = np.random.randn(100)", number=25),\
timeit.timeit("sigmoid(scores)", "from __main__ import sigmoid, np; scores = np.random.randn(100)", number=25),\
timeit.timeit("expit(scores)", "from scipy.special import expit; import numpy as np; scores = np.random.randn(100)", number=25)

print timeit.timeit("sigmoidv(scores)", "from __main__ import sigmoidv, np; scores = np.random.randn(1000)", number=25),\
timeit.timeit("sigmoid(scores)", "from __main__ import sigmoid, np; scores = np.random.randn(1000)", number=25),\
timeit.timeit("expit(scores)", "from scipy.special import expit; import numpy as np; scores = np.random.randn(1000)", number=25)

print timeit.timeit("sigmoidv(scores)", "from __main__ import sigmoidv, np; scores = np.random.randn(10000)", number=25),\
timeit.timeit("sigmoid(scores)", "from __main__ import sigmoid, np; scores = np.random.randn(10000)", number=25),\
timeit.timeit("expit(scores)", "from scipy.special import expit; import numpy as np; scores = np.random.randn(10000)", number=25)

Results:

size        vectorized      numpy                 expit
N=100: 0.00179314613342 0.000460863113403 0.000132083892822
N=1000: 0.0122890472412 0.00084114074707 0.000464916229248
N=10000: 0.109477043152 0.00530695915222 0.00424313545227

Apply a function to numpy array in Python

Try passing the array as np.float32

import numpy as np
def wcdf(U, A, k):
return np.array([1 - np.exp(-(u/A)**(k)) if u >0 else 0 for u in U])


u=wcdf(np.array([0,2],dtype=np.float32),10,2);
print(u)

My result:

[ 0. 0.03921056]

Using your method using np.vectorize

import numpy as np
def wcdf(u, A, k):
return 1 - np.exp(-(u/A)**(k)) if u >0 else 0

f = np.vectorize(wcdf,otypes=[float])
u=f(np.array([0,2],dtype=np.float32),10,2);
print(u)

Result:

[ 0. 0.03921056]

You have add the otypes as float

Numpy apply function to every item in array

No need to change anything in your function.

Just apply the vectorized version of your function to your array
and stack the result:

np.stack(np.vectorize(filter_func)(myarray), axis=2)

The result is:

array([[[5, 1, 4],
[2, 1, 1]],

[[1, 0, 1],
[4, 4, 4]]])

Numpy apply function to array

You can use below code to achieve desirable output

import numpy as np
array = np.linspace(0, 5, 6)
f2 = lambda x: x-x
print(f2(array))

Numpy, apply a list of functions along array dimension

You can iterate over your function list using np.apply_along_axis:

import numpy as np
x = np.ranom.randn(100, 100)
for f in fun_list:
x = np.apply_along_axis(f, 0, x)

Based on OP's Update

Assuming your functions and batches are the same in size:

batch = ... # tuple of 4 images  
batch_out = tuple([np.apply_along_axis(f, 0, x) for f, x in zip(fun_list, batch)])

Applying a function along a numpy array using indexes as parameters

What you try to do can be done with meshgrid.

Return coordinate matrices from coordinate vectors.

n_rows, n_cols = arr.shape
col_matrix, row_matrix = np.meshgrid(np.arange(n_cols), np.arange(n_rows))
result = arr + col_matrix + row_matrix
print(result)

This returns

[[ 0  2  4  6  8]
[ 6 8 10 12 14]]


Related Topics



Leave a reply



Submit