Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to calculate a logistic sigmoid function in Python?

Tags:

python

sigmoid

This is a logistic sigmoid function:

enter image description here

I know x. How can I calculate F(x) in Python now?

Let's say x = 0.458.

F(x) = ?

like image 341
Richard Knop Avatar asked Oct 01 '22 19:10

Richard Knop


1 Answers

This should do it:

import math

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

And now you can test it by calling:

>>> sigmoid(0.458)
0.61253961344091512

Update: Note that the above was mainly intended as a straight one-to-one translation of the given expression into Python code. It is not tested or known to be a numerically sound implementation. If you know you need a very robust implementation, I'm sure there are others where people have actually given this problem some thought.

like image 292
unwind Avatar answered Oct 10 '22 10:10

unwind