Reputation: 484
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()
Upvotes: 1
Views: 1435
Reputation: 3470
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'
Upvotes: 0
Reputation: 231550
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])
Upvotes: 1