new for NumPy and may not be looking correctly, so I will take pieces if this is a general question ...
I am working on a problem when I need to compute log (n!) For relatively large numbers - i.e. to big, to calculate the factorial first, so I wrote the following function:
def log_fact(n):
x = 0
for i in range(1,n+1):
x += log(i)
return x
Now the problem is that I want to use this as part of the function passed to curve_fit:
def logfactfunc(x, a, b, c):
return a*log_fact(x) + b*x + c
from scipy.optimize import curve_fit
curve_fit(logfactfunc, x, y)
However, this results in the following error:
File "./fit2.py", line 16, in log_fact
for i in range(1,n+1):
TypeError: only length-1 arrays can be converted to Python scalars
A small search suggested numpy.frompyfunc () to convert this to ufunc
curve_fit(np.frompyfunc(logfactfunc, 1, 1), data[k].step, data[k].sieve)
TypeError: <ufunc 'logfactfunc (vectorized)'> is not a Python function
Tried this too:
def logfactfunc(x, a, b, c):
return a*np.frompyfunc(log_fact, 1, 1)(x) + b*x + c
File "./fit2.py", line 30, in logfactfunc
return a*np.frompyfunc(log_fact, 1, 1)(x) + b*x + c
TypeError: unsupported operand type(s) for +: 'numpy.ndarray' and 'numpy.float64
Any ideas on how I can get my log_fact () function to be used in curve_fit () function
Thanks!