-1

How would I implement this in python?

1 / (1 + e^-(-6.78+(0.04*age)))

I'm not sure about the e part of the formula. Here's image:

img

Eran Moshe
  • 3,062
  • 2
  • 22
  • 41
Daniel
  • 45
  • 1
  • 1
  • 2
  • 2
    One cannot understand what you want to implement bro... Do you want to implement the number e ? as e=2.71... ? or do you want to use e like, just calculate what ever is up there – Eran Moshe Aug 15 '18 at 05:45
  • This is a screenshot of the bit of the formula I was given. I think its the math.e function which has been said. [link](http://i68.tinypic.com/w7h0k5.png) – Daniel Aug 15 '18 at 06:12
  • And do you want to develop a method that will calculate e ? Or you just want to calculate this expression ? and its e to the power of `-(-6.78+(0.04*age))` not e minus something. use ^ for expression – Eran Moshe Aug 15 '18 at 06:53

2 Answers2

7

The function you're using is known as the sigmoid function. You can build a function that will calculate every sigmoid of x.

In order to use e^(x) you can use the numpy function exp as shown in the example.

import numpy as np

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

if __name__ == '__main__':
    age = 15
    result = sigmoid(-6.78+(0.04*age))
    print(result)
Eran Moshe
  • 3,062
  • 2
  • 22
  • 41
  • Note that the sigmoid function is already implemented in `scipy:`: [`scipy.stats.logistic.cdf`](https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.logistic.html) and [`scipy.special.expit`](https://docs.scipy.org/doc/scipy/reference/generated/scipy.special.expit.html). – Stef Sep 08 '21 at 11:58
5

Use math.e:

import math
1 / (1+ math.e-(-6.78+(0.04*age)))
jh314
  • 27,144
  • 16
  • 62
  • 82
  • 2
    I think he means e to the power of -(...). but the question is unclear – Eran Moshe Aug 15 '18 at 05:49
  • 1
    This answer is wrong for two reasons. (1) You forgot to take the exponent, so you're actually returning a subtraction instead of an exponential. (2) There is a `math.exp` function; no need to re-implement it manually using a power of `math.e`. – Stef Sep 08 '21 at 11:51