python 2.7 - I need to calculate the likelihood P(D|N) and also to implement my function that it allows arrays as input arguments -


i tried gives me error 'local variable 'l' referenced before assignment'

def likelihood(n,n,k):     """     call:         l = likelihood(n,n,k)     input argument:         n: integer (array)         n: integer (array)         k: integer (array)     output argument:         l: float (array)     example:         likelihood(6,10,5)         =>         1.190748e-05     """     if isinstance(n,list): # n array         l = zeros(len(n))          i, in enumerate(n):             l[i]=exp(log_factorial(i)-log_factorial(i-k)-n*log(i))         else: # n scalar             l= exp(log_factorial(n)-log_factorial(n-k)-n*log(n))     return(l) 

where wrong? or there way solve it?

if call function n not list, not go if clause. l not defined when return reached. actual error, though, else clause indented. want:

from numpy import zeros, exp, log scipy.special import gammaln log_factorial = lambda z:gammaln(z+1) def likelihood(n,n,k):     if isinstance(n,list): # n array         l = zeros(len(n))          i, in enumerate(n):             l[i]=exp(log_factorial(i)-log_factorial(i-k)-n*log(i))     else: # n scalar         l= exp(log_factorial(n)-log_factorial(n-k)-n*log(n))     return l likelihood([6],10,5) likelihood(6,10,5) 

Comments

Popular posts from this blog

node.js - Mongoose: Cast to ObjectId failed for value on newly created object after setting the value -

[C++][SFML 2.2] Strange Performance Issues - Moving Mouse Lowers CPU Usage -

ios - Possible to get UIButton sizeThatFits to work? -