Trying to get the integral of a complex function
Question:
I have a function in which I am trying to compute the definite integral. However, part of this function uses a map function within it and I am getting a TypeError: only size-1 arrays can be converted to Python scalars
Here is my function:
from scipy import integrate
import numpy as np
def func(a, b, c, d): #a is an array of 4000 elements, b is an array of ten elements, c&d are integers
n = len(a)
aver = a.mean()
stdevn = a.std()
final = []
def fn(a=a, b=b, c=c, d=d):
return ((1/n)*sum(map(lambda y: ((1/c) * np.exp(-0.5*((x - y - 0.2*((b-aver)/stdevn)*y)/bandwidth)**2)), a)))
for i in b:
total = integrate.quad(fn, a=0, b=100)
final.append(total)
return final
The result should be an array of length b (10). I am not sure where in the code I have the error. x
is in the function as it is part of the integration
Traceback:
---> 10 total = integrate.quad(fn, a=0, b=100)
11
12 final.append(total)
/opt/conda/lib/python3.7/site-packages/scipy/integrate/quadpack.py in quad(func, a, b, args, full_output, epsabs, epsrel, limit, points, weight, wvar, wopts, maxp1, limlst)
350 if weight is None:
351 retval = _quad(func, a, b, args, full_output, epsabs, epsrel, limit,
--> 352 points)
353 else:
354 if points is not None:
/opt/conda/lib/python3.7/site-packages/scipy/integrate/quadpack.py in _quad(func, a, b, args, full_output, epsabs, epsrel, limit, points)
461 if points is None:
462 if infbounds == 0:
--> 463 return _quadpack._qagse(func,a,b,args,full_output,epsabs,epsrel,limit)
464 else:
465 return _quadpack._qagie(func,bound,infbounds,args,full_output,epsabs,epsrel,limit)
TypeError: only size-1 arrays can be converted to Python scalars
Answers:
I suppose you are using scipy.integrate.quad
incorrectly. From the manual page it can be seen that the quad
function needs the start and end of the interval as arguments. You try to pass two variables (I guess as arguments for your fn
function?).
Try it similar to total = integrate.quad(fn, min_interval, max_interval, args=(a, b, c, d))
.
Next time please add a minimal reproducible example. Like this it is more a guessing game.
This is a working solution:
from scipy import integrate
import numpy as np
def func(a, b, c, d): #a is an array of 4000 elements, b is an array of ten elements, c&d are integers
n = len(a)
aver = a.mean()
stdevn = a.std()
final = []
for i in b:
total = integrate.quad(lambda x: x*(inv*sum(map(lambda y: ((1/b) * np.exp(-0.5*((x-y-0.2*((b-aver)/stdevn*x/bandwidth))), clm))), a, b, epsabs = np.inf
final.append(total)
return final
I have a function in which I am trying to compute the definite integral. However, part of this function uses a map function within it and I am getting a TypeError: only size-1 arrays can be converted to Python scalars
Here is my function:
from scipy import integrate
import numpy as np
def func(a, b, c, d): #a is an array of 4000 elements, b is an array of ten elements, c&d are integers
n = len(a)
aver = a.mean()
stdevn = a.std()
final = []
def fn(a=a, b=b, c=c, d=d):
return ((1/n)*sum(map(lambda y: ((1/c) * np.exp(-0.5*((x - y - 0.2*((b-aver)/stdevn)*y)/bandwidth)**2)), a)))
for i in b:
total = integrate.quad(fn, a=0, b=100)
final.append(total)
return final
The result should be an array of length b (10). I am not sure where in the code I have the error. x
is in the function as it is part of the integration
Traceback:
---> 10 total = integrate.quad(fn, a=0, b=100)
11
12 final.append(total)
/opt/conda/lib/python3.7/site-packages/scipy/integrate/quadpack.py in quad(func, a, b, args, full_output, epsabs, epsrel, limit, points, weight, wvar, wopts, maxp1, limlst)
350 if weight is None:
351 retval = _quad(func, a, b, args, full_output, epsabs, epsrel, limit,
--> 352 points)
353 else:
354 if points is not None:
/opt/conda/lib/python3.7/site-packages/scipy/integrate/quadpack.py in _quad(func, a, b, args, full_output, epsabs, epsrel, limit, points)
461 if points is None:
462 if infbounds == 0:
--> 463 return _quadpack._qagse(func,a,b,args,full_output,epsabs,epsrel,limit)
464 else:
465 return _quadpack._qagie(func,bound,infbounds,args,full_output,epsabs,epsrel,limit)
TypeError: only size-1 arrays can be converted to Python scalars
I suppose you are using scipy.integrate.quad
incorrectly. From the manual page it can be seen that the quad
function needs the start and end of the interval as arguments. You try to pass two variables (I guess as arguments for your fn
function?).
Try it similar to total = integrate.quad(fn, min_interval, max_interval, args=(a, b, c, d))
.
Next time please add a minimal reproducible example. Like this it is more a guessing game.
This is a working solution:
from scipy import integrate
import numpy as np
def func(a, b, c, d): #a is an array of 4000 elements, b is an array of ten elements, c&d are integers
n = len(a)
aver = a.mean()
stdevn = a.std()
final = []
for i in b:
total = integrate.quad(lambda x: x*(inv*sum(map(lambda y: ((1/b) * np.exp(-0.5*((x-y-0.2*((b-aver)/stdevn*x/bandwidth))), clm))), a, b, epsabs = np.inf
final.append(total)
return final