I'm trying to use scipy minimize and running into a ValueError error that just I can't seem to figure out.
I have an objective function f as defined below, which takes two inputs. If I define the two inputs with random numbers, and call the function, it seems to evaluate just fine.
import numpy as np
import pandas as pd
from scipy.optimize import minimize
sigma = pd.DataFrame(np.random.randn(7, 7), columns=list('ABCDEFG'))
x0 = np.random.randn(7)
def f(x, sigma):
result = 0.0
sigmaX = sigma.dot(x)
for i in range(len(x)):
for j in range(len(x)):
result = result + (x[i] * sigmaX[i] - x[j]*sigmaX[j])**2
return result
In [387]: f(x0, sigma)
Out[387]: 371.67951578983951
But when I try to use minimize, I get a ValueError:
In [389]: minimize(f, x0, args=(sigma))
ValueError: Wrong number of items passed 1, placement implies 7
I can't figure out whether minimize is upset about the shape/size of x0 or sigma. Any help would be very much appreciated!
Eric
Copied from my comment, so it can be marked as closed
Try args=(sigma,). That is make sure you give args a tuple. Without the , the () does nothing.
Related
I would like to solve this kind of equations:
a*85**b+c=100
a*90**b+c=66
a*92**b+c=33
I tried this
import scipy.optimize
def fun(variables) :
(a,b,c)= variables
eq0=a*85**b+c-100
eq1=a*90**b+c-66
eq2=a*92**b+c-33
return [eq0,eq1,eq2]
result = scipy.optimize.fsolve(fun, (1, -1, 0))
print(result)
But I get ValueError: Integers to negative integer powers are not allowed.
Then I tried the equivalent
def fun(variables) :
(a,b,c)= variables
eq0=log(a)+b*log(85)-log(100-c)
eq1=log(a)+b*log(90)-log(66-c)
eq2=log(a)+b*log(92)-log(33-c)
return [eq0,eq1,eq2]
result = scipy.optimize.fsolve(fun, (1, -1, 0))
print(result)
I get a solution but that is equal to the initial values (1, -1, 0)
Thus when I test fun(result), I get values different from zero.
I have noticed that for this example the same problem is observed
import scipy.optimize
def fun(variables) :
(x,y)= variables
eqn_1 = x**2+y-4
eqn_2 = x+y**2+3
return [eqn_1,eqn_2]
result = scipy.optimize.fsolve(fun, (0.1, 1))
print(result)
fun(result)
Does anyone would know how I could do ? Thank you
PS I have posted here about sympy last week
Resolution of multiple equations (with exponential)
When the initial condition is not well known, sometimes its best to try other methods first . For small problems, simplex minimization is useful:
import numpy as np
def func(x):
a,b,c= x
eq0=np.log(a)+b*np.log(85)-np.log(100-c)
eq1=np.log(a)+b*np.log(90)-np.log(66-c)
eq2=np.log(a)+b*np.log(92)-np.log(33-c)
return eq0**2+ eq1**2 + eq2**2
def func_vec(x):
a,b,c= x
eq0=np.log(a)+b*np.log(85)-np.log(100-c)
eq1=np.log(a)+b*np.log(90)-np.log(66-c)
eq2=np.log(a)+b*np.log(92)-np.log(33-c)
return eq0, eq1, eq2
from scipy.optimize import fsolve, minimize
out = minimize(func, [1,1,0], method="Nelder-Mead", options={"maxfev":100000})
print("roots:", out.x)
print("value at roots:", func_vec(out.x))
# roots: [ 7.87002460e+11 -1.07401055e-09 -7.87002456e+11]
# value at roots: (6.0964566728216596e-12, -1.2086331935279304e-11, 6.235012506294879e-12)
Note, I also tried [1,1,1] as an initial condition and found it converged to the wrong solution. Further increasing maxfev from 1e5 to 1e7 allowed [1,1,1] to converged to the proper solution, but then perhaps there are better methods to solve this.
Defining a integral of multi variable function as a second function python
I am using python to integrate a multivariable function over only one of the variables (it is a function of x and theta and I am integrating over theta from 0 to 2*pi, hence the result is a function of x). I have attempted the following:
import numpy as np
import scipy.integrate as inte
d=10.0
xvals=np.linspace(-d,d,1000)
def aIntegrand(theta,x):
return 1/(2*np.pi)*np.sin(2*np.pi*x*np.sin(theta)/d)**2
def A(x):
return (inte.quad(aIntegrand,0,2*np.pi,args=(x,))[0])**(1/2)
plt.plot(xvals,A(xvals))
plt.xlabel("x")
plt.ylabel("A(x)")
plt.show()
and I get the following error:
TypeError: only size-1 arrays can be converted to Python scalars
I assume this is because the result of the quad integrator is an array with two elements and python doesn't like defining the function based on an indexed array? That is a complete guess of the problem though. If anyone knows how I can fix this and could let me know, that'd be great :)
A second attempt
I have managed to get the plot of the integral successfully using the following code:
import numpy as np
import scipy.integrate as inte
import matplotlib.pyplot as plt
d=10.0
xvals=np.linspace(-d,d,1000)
thetavals=np.linspace(0.0,2*np.pi,1000)
def aIntegrand(theta,x):
return 1/(2*np.pi)*np.sin(2*np.pi*x*np.sin(theta)/d)**2
def A(x):
result=np.zeros(len(x))
for i in range(len(x)):
result[i]=(inte.quad(aIntegrand,0,2*np.pi,args=(x[i],))[0])**(1/2)
return result
def f(x,theta):
return x**2* np.sin(theta)
plt.plot(xvals,A(xvals))
plt.xlabel("x")
plt.ylabel("A(x)")
plt.show()
But this does not give A(x) as a function, due to the way I have defined it, it requires an array form input. I need the function to be of the same form as aIntegrand, where when given parameters returns a single value & so the function can be integrated repeatedly.
I do not think that what you seek exists within Scipy. However, you have at least two alternatives.
First, you can create an interpolator using interp1d. This means that the range of x values you initially give determines the range for which you will be able to call the interpolator. This interpolator can then be called for any value of x in this interval.
You can perform the second integral even though you do not have a callable. The function simps only requires values and their location to provide an estimate of the integration process. Otherwise, you can perform the double integration in one call with dblquad.
First note that your integral can be calculated analytically. It is
0.5 * (1 - J0(4 * pi * x / d))
where J0 is the Bessel function of the first kind.
Second, you could use quadpy (one of my projects); it has fully vectorized computation.
import numpy as np
import quadpy
import matplotlib.pyplot as plt
import scipy.special
d = 10.0
x = np.linspace(-d, d, 1000)
def aIntegrand(theta):
return (
1
/ (2 * np.pi)
* np.sin(2 * np.pi * np.multiply.outer(x, np.sin(theta)) / d) ** 2
)
Ax2, err = quadpy.quad(aIntegrand, 0, 2 * np.pi)
Ax = np.sqrt(Ax2)
plt.plot(x, Ax, label="quadpy")
plt.xlabel("x")
plt.ylabel("A(x)")
plt.plot(x, np.sqrt(0.5 * (1 - scipy.special.jv(0, 4*np.pi*x/d))), label="bessel")
# plt.show()
plt.savefig("out.png", transparent=True, bbox_inches="tight")
So I have the function
f(x) = I_0(exp(Q*x/nKT)
Where Q, K and T are constants, for the sake of clarity I'll add the values
Q = 1.6x10^(-19)
K = 1.38x10^(-23)
T = 77.6
and n and I_0 are the two constraints that I'm trying to minimize.
my xdata is a list of 50 datapoints and as is my ydata. So as of yet this is my code:
from __future__ import division
import scipy.optimize as optimize
import numpy
xdata = numpy.array([1.07,1.07994,1.08752,1.09355,
1.09929,1.10536,1.10819,1.11321,
1.11692,1.12099,1.12435,1.12814,
1.13181,1.13594,1.1382,1.14147,
1.14443,1.14752,1.15023,1.15231,
1.15514,1.15763,1.15985,1.16291,1.16482])
ydata = [0.00205,
0.004136,0.006252,0.008252,0.010401,
0.012907,0.014162,0.016498,0.018328,
0.020426,0.022234,0.024363,0.026509,
0.029024,0.030457,0.032593,0.034576,
0.036725,0.038703,0.040223,0.042352,
0.044289,0.046043,0.048549,0.050146]
#data and ydata is experimental data, xdata is voltage and ydata is current
def f(x,I0,N):
# I0 = 7.85E-07
# N = 3.185413895
Q = 1.66E-19
K = 1.38065E-23
T = 77.3692
return I0*(numpy.e**((Q*x)/(N*K*T))-1)
result = optimize.curve_fit(f, xdata,ydata) #trying to minize I0 and N
But the answer doesn't give suitably optimized constraints
Any help would be hugely appreciated I realize there may be something obvious I am missing, I just can't see what it is!
I have tried this, but for some reason if you throw out those constants so function becomes
def f(x,I0,N):
return I0*(numpy.exp(x/N)-1)
you get something reasonable.
1.86901114e-13, 4.41838309e-02
Its true, that when we get rid off constants its better. Define function as:
def f(x,A,B):
return A*(np.e**(B*x)-1)
and fit it by curve_fit, you'll be able to get A that is explicitly I0 (A=I0) and B (you can obtain N simply by N=Q/(BKT) ). I managed to get pretty good fit.
I think if there is too much constants, algorithm gets confused some way.
I have seen this question or a variant asked elsewhere e.g.
Scipy error using optimization module. Failure converting array to fortran
http://numpy-discussion.10968.n7.nabble.com/minimize-Algorithmen-Problem-with-boundarys-td37709.html
But they are not really put with a simple hackable example code. Nor are there any real answers (probably because of the lack of a simple demo of the problem).
The problem is, when trying to fit a function using scipy.optimise fmin_slsqp method you get this pretty opaque error
"failed in converting 8th argument `g' of _slsqp.slsqp to C/Fortran array"
In the code below I fit a linear function to random correlated data using the leastsq method. From the .docs I can see no reason that the same syntax shouldn't do the same thing using fmin_slsqp, but it doesn't.
Does anybody know why?
import numpy as nm
from scipy.optimize import leastsq, fmin_slsqp
import matplotlib.pyplot as plt
# residuals of linear function
def res(params,x,y_real):
y_fit = params[0] +x*params[1]
res = y_fit-y_real
return res
#generate correlated data
xx = nm.array([-0.51, 51.2])
yy = nm.array([0.33, 51.6])
means = [xx.mean(), yy.mean()]
stds = [xx.std() / 3, yy.std() / 3]
corr = 0.8 # correlation
covs = [[stds[0]**2 , stds[0]*stds[1]*corr],
[stds[0]*stds[1]*corr, stds[1]**2]]
m = nm.random.multivariate_normal(means, covs, 100)
x = m[:,0]
y = m[:,1]
# Initial values of parameters
initvals = [0,0]
fit1,j = leastsq(res, initvals, args=(x,y))
#Plot fit 1
y_fit = fit1[0] + fit1[1]*x
plt.scatter(x,y)
plt.plot(x,y_fit)
plt.show()
fit2 = fmin_slsqp(res, initvals, args=(x,y))
I get the same error when the return from the Objective function is not a scalar. A minimal example which causes this error is
from scipy.optimize import fmin_slsqp
def fn(x):
return [0.,1.]
x = [0, 1., 2.]
minsoln = fmin_slsqp(fn, x)
while the following does not raise the error,
from scipy.optimize import fmin_slsqp
def fn(x):
return 0.
x = [0, 1., 2.]
minsoln = fmin_slsqp(fn, x)
I think this is either a bug or should have a clearer error message. I've raise an issue.
UPDATE:
This has now been resolved by b-carter to give a clear error message,
"Objective function must return a scalar"
with the documentation updated, see this thread for discussion.
Hi I had the same error with the following:
def ptf_returns(weights,returns):
return pd.DataFrame(np.array(returns).T*(weights)).T.mean().mean()
When I add the following it works:
def ptf_returns(weights,returns):
return float(pd.DataFrame(np.array(returns).T*(weights)).T.mean().mean())
The bug seems to be oriented around the type() of the response.
I am trying to calculate the definite integral of a function with multiple variables over just one variable in scipy.
This is kind of like what my code looks like-
from scipy.integrate import quad
import numpy as np
def integrand(x,y):
return x*np.exp(x/y)
quad(integrand, 1,2, args=())
And it returns this type error:
TypeError: integrand() takes exactly 2 arguments (1 given)
However, it works if I put a number into args. But I don't want to, because I want y to remain as y and not a number. Does anyone know how this can be done?
EDIT: Sorry, don't think I was clear. I want the end result to be a function of y, with y still being a symbol.
Thanks to mdurant, here's what works:
from sympy import integrate, Symbol, exp
from sympy.abc import x
y=Symbol('y')
f=x*exp(x/y)
integrate(f, (x, 1, 2))
Answer:
-(-y**2 + y)*exp(1/y) + (-y**2 + 2*y)*exp(2/y)
You probably just want the result to be a function of y right?:
from scipy.integrate import quad
import numpy as np
def integrand(x,y):
return x*np.exp(x/y)
partial_int = lambda y: quad(integrand, 1,2, args=(y,))
print partial_int(5)
#(2.050684698584342, 2.2767173686148355e-14)
The best you can do is use functools.partial, to bind what arguments you have for the moment. But one fundamentally cannot numerically integrate a definite integral if you havnt got the entire domain specified yet; in that case the resulting expression will necessarily still contain symbolic parts, so the intermediate result isn't numerical.
(Assuming that you are talking about computing the definite integral over x given a specific, fixed value of y.)
You could use a lambda:
quad(lambda x:integrand(x, 10), 1, 2, args=())
or functools.partial():
quad(functools.partial(integrand, y=10), 1, 2, args=())
from scipy.integrate import quad
import numpy as np
def integrand(x,y):
return x*np.exp(x/y)
vec_int = np.vectorize(integrand)
y = np.linspace(0, 10, 100)
vec_int(y)