Given the following numpy matrix:
import numpy as np
mymatrix = np.matrix('-1 0 1; -2 0 2; -4 0 4')
matrix([[-1, 0, 1],
[-2, 0, 2],
[-4, 0, 4]])
and the following function (sigmoid/logistic):
import math
def myfunc(z):
return 1/(1+math.exp(-z))
I want to get a new NumPy array/matrix where each element is the result of applying the myfunc
function to the corresponding element in the original matrix.
the map(myfunc, mymatrix)
fails because it tries to apply myfunc to the rows not to each element. I tried to use numpy.apply_along_axis
and numpy.apply_over_axis
but they are meant also to apply the function to rows or columns and not on an element by element basis.
So how can apply myfunc(z)
to each element of myarray
to get:
matrix([[ 0.26894142, 0.5 , 0.73105858],
[ 0.11920292, 0.5 , 0.88079708],
[ 0.01798621, 0.5 , 0.98201379]])