sigmoid in python that can take scalar, vector or matrix
The numpy module, included in many Python distributions and easy to add to others, has array capabilities. Here is how to do what you want in Python with numpy. Note that defining an array in numpy is a bit different than in Octave, but the sigmoid expression is almost exactly the same.
from numpy import array, exp
z = array([ 0.2, 0.4, 0.1])
print('z = \n', z)
g = 1 / (1 + exp(-z))
print('g =\n', g)
print()
z = array([[0.2, 0.4], [0.5, 0.7], [0.9, .004]])
print('z = \n', z)
g = 1 / (1 + exp(-z))
print('g =\n', g)
The results of that code (running in IPython) are:
z =
[ 0.2 0.4 0.1]
g =
[ 0.549834 0.59868766 0.52497919]
z =
[[ 0.2 0.4 ]
[ 0.5 0.7 ]
[ 0.9 0.004]]
g =
[[ 0.549834 0.59868766]
[ 0.62245933 0.66818777]
[ 0.7109495 0.501 ]]
Alternatively, you can use the vectorized Sigmoid function expit
that is available in scipy:
from scipy.special import expit
from numpy import array
z = array([ 0.2, 0.4, 0.1])
g = expit(z)