简体   繁体   中英

Interpreting a Mathematical Function in Python

I have a problem I'm working on where I have to produce a function which mirrors a mathematical one given: Probability = e^beta / 1 + e^beta . So far I produced code that works when I feed it an integer, but I need to use the function to calculate the probabilities of an array.

My code so far:

import math
e = math.e


def likelihood(beta):
  for i in range(beta): 
    return (e**(beta)/(1+ e**(beta)))

beta_candidate = np.random.uniform(-5, 5, 50)

likelihood_candidate = likelihood(beta_candidate)

Whenever I run the code I'm met with an error stating: only integer scalar arrays can be converted to a scalar index.

In [3]: import math

In [4]: e = math.e

In [5]: def likelihood(beta):
   ...:     return [e**i/(1+e**i) for i in beta]
   ...:

In [7]: likelihood_candidate = likelihood(beta_candidate)

Since you have your beta_candidate as numpy array, you can just do vectorized numpy operations:

l = np.exp(beta_candidate)/(1+np.exp(beta_candidate))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM