I am fitting a very simple curve having three points. with leastsq method, following all the rules. But still I am getting error. I cannot understand. Can anyone help. Thank you so much
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import leastsq
x = np.array([2.0,30.2,15.0])
y = np.array([45.0,56.2,30.0])
print(x)
print(y)
# model
def t(x,a,b,c):
return a*x**2 + b*x + c
#residual fucntion
def residual_t(x,y,a,b,c):
return y-t(x,a,b,c)
#initial parameters
g0 = np.array([0.0,0.0,0.0])
#leastsq method
coeffs, cov = leastsq(residual_t, g0, args=(x,y))
plt.plot(x,t(x,*coeffs),'r')
plt.plot(x,y,'b')
plt.show()
#finding out Rsquared and Radj squared value
absError = residual_t(y,x,*coeffs)
se = np.square(absError) # squared errors
Rsquared = 1.0 - (np.var(absError) / np.var(y))
n = len(x)
k = len(coeffs)
Radj_sq = (1-((1-Rsquared)/(n-1)))/(n-k-1)
print (f'Rsquared value: {Rsquared} adjusted R saquared value: {Radj_sq}')
TypeError: residual_t() missing 2 required positional arguments: 'b' and 'c'
Why??
coeffs is already a array containing best it values of a, b,c. coeffs is also showing undefined and residual_t is also showing problem. Could you please help me to understand.
With a copy-n-paste of your code (including the *coeffs change), I get
1135:~/mypy$ python3 stack58206395.py
[ 2. 30.2 15. ]
[45. 56.2 30. ]
Traceback (most recent call last):
File "stack58206395.py", line 24, in <module>
coeffs, cov = leastsq(residual_t, g0, args=(x,y))
File "/usr/local/lib/python3.6/dist-packages/scipy/optimize/minpack.py", line 383, in leastsq
shape, dtype = _check_func('leastsq', 'func', func, x0, args, n)
File "/usr/local/lib/python3.6/dist-packages/scipy/optimize/minpack.py", line 26, in _check_func
res = atleast_1d(thefunc(*((x0[:numinputs],) + args)))
TypeError: residual_t() missing 2 required positional arguments: 'b' and 'c'
That is the error is in the use of residual_t within the leastsq call.
If I add
residual_t(g0, x, y)
right after the g0 definition I get the same error:
1136:~/mypy$ python3 stack58206395.py
[ 2. 30.2 15. ]
[45. 56.2 30. ]
Traceback (most recent call last):
File "stack58206395.py", line 23, in <module>
residual_t(g0, x, y)
TypeError: residual_t() missing 2 required positional arguments: 'b' and 'c'
So you need to define residual_t to work with a call like this. I'm not going to take a guess as to what you really want, so I'll leave the fix up to you.
Just remember that residual_t will be called with the x0, spliced with the args tuple. This is typical usage for scipy.optimize functions. Review the docs if necessary.
edit
Defining the function as:
def residual_t(abc, x, y):
a,b,c = abc
return y-t(x,a,b,c)
runs without error.
Related
This is my standalone code to reproduce the problem:
import numpy as np
from scipy.optimize import curve_fit
def find_vector_of_minor_axis_from_chunk(data):
n = 20 # number of points
time = np.linspace(0, 2 * np.pi, n)
guess_center_point = data.mean(1)
guess_center_point = guess_center_point[np.newaxis, :].transpose()
guess_a_phase = 0
guess_b_phase = 0
guess_a = 1
guess_b = 1
guess_a_axis_vector = np.array([[1], [0], [0]])
guess_b_axis_vector = np.array([[0], [1], [0]])
p0 = np.array([guess_center_point,
guess_a, guess_a_axis_vector, guess_a_phase,
guess_b, guess_b_axis_vector, guess_b_phase])
def ellipse_func(t, center_point, a, a_axis_vector, a_phase, b, b_axis_vector, b_phase):
return center_point + a * a_axis_vector * np.sin(t * a_phase) + b * b_axis_vector * np.sin(t + b_phase)
popt, pcov = curve_fit(ellipse_func, time, data, p0=p0)
center_point, a, a_axis_vector, a_phase, b, b_axis_vector, b_phase = popt
print(str(a_axis_vector, b_axis_vector))
shorter_vector = a_axis_vector
if np.abs(a_axis_vector) > np.aps(b_axis_vector):
shorter_vector = b_axis_vector
return shorter_vector
def main():
data = np.array([[-4.62767933, -4.6275775, -4.62735346, -4.62719652, -4.62711625, -4.62717975,
-4.62723845, -4.62722407, -4.62713901, -4.62708749, -4.62703238, -4.62689101,
-4.62687185, -4.62694013, -4.62701082, -4.62700483, -4.62697488, -4.62686825,
-4.62675683, -4.62675204],
[-1.58625998, -1.58625039, -1.58619648, -1.58617611, -1.58620606, -1.5861833,
-1.5861821, -1.58619169, -1.58615814, -1.58616893, -1.58613179, -1.58615934,
-1.58611262, -1.58610782, -1.58613179, -1.58614017, -1.58613059, -1.58612699,
-1.58607428, -1.58610183],
[-0.96714786, -0.96713827, -0.96715984, -0.96715145, -0.96716703, -0.96712869,
-0.96716104, -0.96713228, -0.96719698, -0.9671838, -0.96717062, -0.96717062,
-0.96715744, -0.96707717, -0.96709275, -0.96706519, -0.96715026, -0.96711791,
-0.96713588, -0.96714786]])
print(str(find_vector_of_minor_axis_from_chunk(data)))
if __name__ == '__main__':
main()
That gives me this traceback:
Traceback (most recent call last):
File "C:/Users/X/PycharmProjects/lissajous-achse/ellipse_fit.py", line 52, in <module>
main()
File "C:/Users/X/PycharmProjects/lissajous-achse/ellipse_fit.py", line 49, in main
print(str(find_vector_of_minor_axis_from_chunk(data)))
File "C:/Users/X/PycharmProjects/lissajous-achse/ellipse_fit.py", line 25, in find_vector_of_minor_axis_from_chunk
popt, pcov = curve_fit(ellipse_func, time, data, p0=p0)
File "C:\Users\X\PycharmProjects\lissajous-achse\venv\lib\site-packages\scipy\optimize\minpack.py", line 763, in curve_fit
res = leastsq(func, p0, Dfun=jac, full_output=1, **kwargs)
File "C:\Users\X\PycharmProjects\lissajous-achse\venv\lib\site-packages\scipy\optimize\minpack.py", line 392, in leastsq
raise TypeError('Improper input: N=%s must not exceed M=%s' % (n, m))
TypeError: Improper input: N=7 must not exceed M=3
Process finished with exit code 1
My code is an adaption of the second answer here. The problem causing the error message is solved by simple packing of variables here.
Why does the problem not surface in the mentioned second answer? And how can I pack my variables, which consist of several 3d vectors and individual scalars, to solve this problem? How do i pass in my t, which is a constant and should not be optimized?
Apparently python is quite smart regarding the length of the fields of the arguments, depending on the the initial guesses. So i could just pass in ONE variable, and split it up inside the function like so:
import numpy as np
from scipy.optimize import minimize
def find_vector_of_minor_axis_from_chunk(data):
n = 20 # number of points
guess_center_point = data.mean(1)
guess_center_point = guess_center_point[np.newaxis, :].transpose()
guess_a_phase = 0.0
guess_b_phase = 0.0
guess_a = 1.0
guess_b = 1.0
guess_a_axis_vector = np.array([[1.0], [0.0], [0.0]])
guess_b_axis_vector = np.array([[0.0], [1.0], [0.0]])
p0 = np.array([guess_center_point,
guess_a, guess_a_axis_vector, guess_a_phase,
guess_b, guess_b_axis_vector, guess_b_phase])
def ellipse_func(x, data):
center_point = x[0]
a = x[1]
a_axis_vector = x[2]
a_phase = x[3]
b = x[4]
b_axis_vector = x[5]
b_phase = x[6]
t = np.linspace(0, 2 * np.pi, n)
error = center_point + a * a_axis_vector * np.sin(t * a_phase) + b * b_axis_vector * np.sin(t + b_phase) - data
error_sum = np.sum(error**2)
print(str(error_sum))
return error_sum
popt, pcov = minimize(ellipse_func, p0, args=(data))
center_point, a, a_axis_vector, a_phase, b, b_axis_vector, b_phase = popt
print(str(a_axis_vector, b_axis_vector))
shorter_vector = a_axis_vector
if np.abs(a_axis_vector) > np.aps(b_axis_vector):
shorter_vector = b_axis_vector
return shorter_vector
def main():
data = np.array([[-4.62767933, -4.6275775, -4.62735346, -4.62719652, -4.62711625, -4.62717975,
-4.62723845, -4.62722407, -4.62713901, -4.62708749, -4.62703238, -4.62689101,
-4.62687185, -4.62694013, -4.62701082, -4.62700483, -4.62697488, -4.62686825,
-4.62675683, -4.62675204],
[-1.58625998, -1.58625039, -1.58619648, -1.58617611, -1.58620606, -1.5861833,
-1.5861821, -1.58619169, -1.58615814, -1.58616893, -1.58613179, -1.58615934,
-1.58611262, -1.58610782, -1.58613179, -1.58614017, -1.58613059, -1.58612699,
-1.58607428, -1.58610183],
[-0.96714786, -0.96713827, -0.96715984, -0.96715145, -0.96716703, -0.96712869,
-0.96716104, -0.96713228, -0.96719698, -0.9671838, -0.96717062, -0.96717062,
-0.96715744, -0.96707717, -0.96709275, -0.96706519, -0.96715026, -0.96711791,
-0.96713588, -0.96714786]])
print(str(find_vector_of_minor_axis_from_chunk(data)))
if __name__ == '__main__':
main()
Also i fixed some floating point vs integer errors in the vector for the initial values.
However now I get a different error:
Traceback (most recent call last):
File "C:/Users/X/PycharmProjects/lissajous-achse/ellipse_fit.py", line 61, in <module>
main()
File "C:/Users/X/PycharmProjects/lissajous-achse/ellipse_fit.py", line 58, in main
print(str(find_vector_of_minor_axis_from_chunk(data)))
File "C:/Users/X/PycharmProjects/lissajous-achse/ellipse_fit.py", line 34, in find_vector_of_minor_axis_from_chunk
popt, pcov = minimize(ellipse_func, p0, args=(data))
File "C:\Users\X\PycharmProjects\lissajous-achse\venv\lib\site-packages\scipy\optimize\_minimize.py", line 604, in minimize
return _minimize_bfgs(fun, x0, args, jac, callback, **options)
File "C:\Users\X\PycharmProjects\lissajous-achse\venv\lib\site-packages\scipy\optimize\optimize.py", line 1063, in _minimize_bfgs
if isinf(rhok): # this is patch for numpy
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
I guess that
The truth value of an array with more than one element is ambiguous.
Use a.any() or a.all()
is some internal error, stemming from the internal decision matrix how to proceed. I don't know how I caused it and how to fix it. When i figure out how it is done properly, I will come back and edit this answer.
This code worked fine before I made it into functions, so what's wrong? I can't see that anywhere in the code have I multipled a string/list by a float number.
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
import numpy as np
from numpy import * #This is temporary
def NonlinearReg(xdata,ydata,f):
Constants, Covariance = curve_fit(f, xdata, ydata)
return Constants
def Nonlinear_Plot(xdata,ydata,f,a,b,c):
plt.figure(figsize=(6, 4))
plt.scatter(xdata, ydata, label='Data')
plt.plot(xdata, f(xdata, a, b, c), label='Best Fit')
plt.legend(loc='best')
plt.show()
def main():
xdata = [2,8,6,7]
ydata = [9,6,5,4]
NonlinearFunction = input("Type in the Nonlinear Function : \n")
ff= lambda x,a,b,c: eval(NonlinearFunction)
a,b,c=NonlinearReg(xdata,ydata,ff)
if (c==1): #The initial guess is as it is; the given function doesn't involve in c
print('\n', '[a b] for the best fit= ', '['+str(a) +' '+str(b)+ ']' ,'\n')
else:
print('\n', '[a b c] for the best fit= ', '['+str(a) +' '+str(b)+' '+str(c)+ ']' ,'\n')
Nonlinear_Plot(xdata, ydata,ff, a,b,c)
main()
If we run this with any input function such as 'a+b*x' this is what we get ( running from visual studio 2019):
Type in the Nonlinear Function :
a+b*x
C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python37_64\lib\site-packages\scipy\optimize\minpack.py:808: OptimizeWarning: Covariance of the parameters could not be estimated #How can I stop this error from coming up?
category=OptimizeWarning)
[a b] for the best fit= [9.879518072308521 -0.6746987951843755] #it does provide the constants a,b
Traceback (most recent call last):
File "C:\Users\Essam\source\repos\PythonApplication2\PythonApplication2\PythonApplication2.py", line 44, in <module>
main()
File "C:\Users\Essam\source\repos\PythonApplication2\PythonApplication2\PythonApplication2.py", line 42, in main
Nonlinear_Plot(xdata, ydata,ff, a,b,c)
File "C:\Users\Essam\source\repos\PythonApplication2\PythonApplication2\PythonApplication2.py", line 13, in Nonlinear_Plot
plt.plot(xdata, f(xdata, a, b, c), label='Best Fit')
File "C:\Users\Essam\source\repos\PythonApplication2\PythonApplication2\PythonApplication2.py", line 34, in <lambda>
ff= lambda x,a,b,c: eval(NonlinearFunction)
File "<string>", line 1, in <module>
TypeError: can't multiply sequence by non-int of type 'numpy.float64'
The code did run before I made it into functions:
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
import numpy as np
from numpy import *
def func(x, a, b,c):
return ff(x,a,b,c)
ff= lambda x,a,b,c: eval("a*x**b")
xdata = [0 ,866, 2753, 4763, 6942, 10593]
ydata = [30, 23, 27, 26, 23, 20]
popt, pcov = curve_fit(func, xdata, ydata)
print('\n', '[a b] for agmad fitting = ', popt,'\n')
plt.figure(figsize=(6, 4))
plt.scatter(xdata, ydata, label='Data')
plt.plot(xdata, ff(xdata, popt[0], popt[1], popt[2]), label='Agmad Fit')
plt.legend(loc='best')
plt.show()
This reproduces the error message:
In [442]: [1,2,3]*np.float(1.2)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-442-84f5d06cd969> in <module>
----> 1 [1,2,3]*np.float(1.2)
TypeError: can't multiply sequence by non-int of type 'float'
In [443]: [1,2,3]*np.float64(1.2)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-443-9e2c2f15c70b> in <module>
----> 1 [1,2,3]*np.float64(1.2)
TypeError: can't multiply sequence by non-int of type 'numpy.float64'
Based on that I suspect that in the 'a+b*x' expression, at time of evaluation, b is a list (or string), and x is an element of a numpy array.
With your generalized evaluation method, it is hard to keep track of the type of variables. That expression should work ok if a, b and x are numpy arrays, but can easily fail is one or more is not.
Check those a,b,c "constants". Don't assume they are correct.
Or if the x is xdata, a list:
In [445]: np.array([1.23])[0]*[2,8,6,7]
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-445-fd8778299d95> in <module>
----> 1 np.array([1.23])[0]*[2,8,6,7]
TypeError: can't multiply sequence by non-int of type 'numpy.float64'
but if xdata is an array:
In [446]: np.array([1.23])[0]*np.array([2,8,6,7])
Out[446]: array([2.46, 9.84, 7.38, 8.61])
Massimo:
Do you have the same error commenting out this line?
Nonlinear_Plot(xdata, ydata,ff, a,b,c)
Essam: I get no error if I comment it
As I tought, the problem is in that function, on the third line:
plt.plot(xdata, f(xdata, a, b, c), ...
You call f passing xdata.
F can't multiply sequence by non-int of type 'numpy.float64'
I know there are plenty of subject on this error and I've been on many of them trying to understand what is going on with sush a simple system. Here is my code, solving a very simple equation to test the efficiency of solve_ivp vs odeint.
import numpy as np
import numpy as np
import matplotlib.pyplot as plt
from scipy.integrate import solve_ivp
def fun(t,x,a) :
return -x/a
t = np.linspace(0,10,1000)
tspan = [t[0], t[-1]]
x0 = [10]
sol = solve_ivp(fun, tspan, x0, t_eval = t, args = 1)
plt.plot(t,sol.y.T)
plt.show()
And this is the full error report :
/home/anthony/.local/lib/python3.8/site-packages/scipy/integrate/_ivp/common.py:40:
/home/anthony/.local/lib/python3.8/site-packages/scipy/integrate/_ivp/common.py:40:
UserWarning: The following arguments have no effect for a chosen solver: `args`.
warn("The following arguments have no effect for a chosen solver: {}."
Traceback (most recent call last):
File "test.py", line 25, in <module>
sol = solve_ivp(fun, tspan, x0, t_eval = t, args = 1)
File "/home/anthony/.local/lib/python3.8/site-packages/scipy/integrate/_ivp/ivp.py", line
477, in solve_ivp
solver = method(fun, t0, y0, tf, vectorized=vectorized, **options)
File "/home/anthony/.local/lib/python3.8/site-packages/scipy/integrate/_ivp/rk.py", line
100, in __init__
self.f = self.fun(self.t, self.y)
File "/home/anthony/.local/lib/python3.8/site-packages/scipy/integrate/_ivp/base.py", line
139, in fun
return self.fun_single(t, y)
File "/home/anthony/.local/lib/python3.8/site-packages/scipy/integrate/_ivp/base.py", line
21, in fun_wrapped
return np.asarray(fun(t, y), dtype=dtype)
TypeError: fun() missing 1 required positional argument: 'a'
To me, the error is pretty clear but it is also very obvious that I put in the right place my argument according to the documentation of this solver doc scipy.integrate.solve_ivp
I also upgraded to the latest my scipy version, any advices would be very helful.
args is supposed to be a tuple
In [281]: sol = solve_ivp(fun, tspan, x0, t_eval = t, args = (1,))
In [282]: sol
Out[282]:
message: 'The solver successfully reached the end of the integration interval.'
nfev: 80
njev: 0
nlu: 0
sol: None
status: 0
success: True
....
Messing up the args is one of the most common scipy.integrate (and optimize) SO errors.
I seem to be getting an error when I use the root-finder in scipy. I was wondering if anyone could point out what I'm doing wrong.
The function I'm finding the root of is just an easy example, and not particularly important.
If I run this code with scipy 0.9.0:
import numpy as np
from scipy.optimize import fsolve
tmpFunc = lambda xIn: (xIn[0]-4)**2 + (xIn[1]-5)**2 + (xIn[2]-7)**3
x0 = [3,4,5]
xFinal = fsolve(tmpFunc, x0 )
print xFinal
I get the following error message:
Traceback (most recent call last):
File "tmpStack.py", line 7, in <module>
xFinal = fsolve(tmpFunc, x0 )
File "/usr/lib/python2.7/dist-packages/scipy/optimize/minpack.py", line 115, in fsolve
_check_func('fsolve', 'func', func, x0, args, n, (n,))
File "/usr/lib/python2.7/dist-packages/scipy/optimize/minpack.py", line 26, in _check_func
raise TypeError(msg)
TypeError: fsolve: there is a mismatch between the input and output shape of the 'func' argument '<lambda>'.
Well it looks like I was trying to use this routine incorrectly. This routine requires the same number of equations and variables vs. the one equation with three variables I gave it. So if the input to the function to be minimized is a 3-D array the output should be a 3-D array. This code works:
import numpy as np
from scipy.optimize import fsolve
tmpFunc = lambda xIn: np.array( [(xIn[0]-4)**2 + xIn[1], (xIn[1]-5)**2 - xIn[2]) \
, (xIn[2]-7)**3 + xIn[0] ] )
x0 = [3,4,5]
xFinal = fsolve(tmpFunc, x0 )
print xFinal
Which represents solving three equations simultaneously.
I'm getting this error message:
Traceback (most recent call last):
File "C:/Python27/test", line 14, in <module>
tck = interpolate.bisplrep(X,Y,Z)
File "C:\Python27\lib\site-packages\scipy\interpolate\fitpack.py", line 850, in bisplrep
raise TypeError('m >= (kx+1)(ky+1) must hold')
TypeError: m >= (kx+1)(ky+1) must hold
The error says that len(X) = m is <=(kx+1)(ky+1). How can I solve this? Here's my program:
import scipy
import math
import numpy
from scipy import interpolate
x= [1000,2000,3000,4000,5000,6000]
y= [1000]
Y = numpy.array([[i]*len(x) for i in y])
X = numpy.array([x for i in y])
Z = numpy.array([[21284473.74,2574509.71,453334.97,95761.64,30580.45,25580.60]])
tck = interpolate.bisplrep(x,y,Z)
print interpolate.bisplev(3500,1000,tck)
Have you read the documentation?
If you don't specify kx and ky, default values will be 3:
scipy.interpolate.bisplrep(x, y, z, w=None, xb=None, xe=None, yb=None, ye=None,
kx=3, ky=3, task=0, s=None, eps=1e-16, tx=None, ty=None,
full_output=0, nxest=None, nyest=None, quiet=1)
And of course, len(X) = 6 < 16 = (3+1)(3+1).
Even if you give kx=1 and ky=1 explicitly while calling, you have another problem. Your (x,y) values form a line, and you can not define a surface from a line. Therefore it gives you ValueError: Invalid inputs.. First, you should fix your data. If this is your data, as you have no variation in Y, skip it and do a spline in 2D with X and Z.