TypeError: can't multiply sequence by non-int of type 'numpy.float64' even though I did not
TypeError: can't multiply sequence by non-int of type 'numpy.float64' even though I did not
这段代码在我把它变成函数之前工作得很好,所以有什么问题吗?我看不到代码中的任何地方我将 string/list 乘以浮点数。
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()
如果我们 运行 使用任何输入函数,例如 'a+b*x',这就是我们得到的(运行ning 来自 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'
在我把它变成函数之前,代码做了 运行:
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()
这重现了错误消息:
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'
基于此,我怀疑在 'a+b*x' 表达式中,在求值时,b
是一个列表(或字符串),而 x
是一个 numpy 的元素数组。
使用您的广义评估方法,很难跟踪变量的类型。如果 a
、b
和 x
是 numpy 数组,该表达式应该可以正常工作,但如果一个或多个不是,则很容易失败。
选中 a
、b
、c
"constants"。不要假设它们是正确的。
或者如果 x
是 xdata
,列表:
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'
但是如果 xdata
是一个数组:
In [446]: np.array([1.23])[0]*np.array([2,8,6,7])
Out[446]: array([2.46, 9.84, 7.38, 8.61])
马西莫:
注释掉这一行是否有同样的错误?
Nonlinear_Plot(xdata, ydata,ff, a,b,c)
Essam:如果我评论它就不会出错
正如我所想,问题出在第三行的那个函数中:
plt.plot(xdata, f(xdata, a, b, c), ...
你调用 f 传递扩展数据。
F 不能将序列乘以 'numpy.float64'
类型的非整数
这段代码在我把它变成函数之前工作得很好,所以有什么问题吗?我看不到代码中的任何地方我将 string/list 乘以浮点数。
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()
如果我们 运行 使用任何输入函数,例如 'a+b*x',这就是我们得到的(运行ning 来自 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'
在我把它变成函数之前,代码做了 运行:
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()
这重现了错误消息:
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'
基于此,我怀疑在 'a+b*x' 表达式中,在求值时,b
是一个列表(或字符串),而 x
是一个 numpy 的元素数组。
使用您的广义评估方法,很难跟踪变量的类型。如果 a
、b
和 x
是 numpy 数组,该表达式应该可以正常工作,但如果一个或多个不是,则很容易失败。
选中 a
、b
、c
"constants"。不要假设它们是正确的。
或者如果 x
是 xdata
,列表:
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'
但是如果 xdata
是一个数组:
In [446]: np.array([1.23])[0]*np.array([2,8,6,7])
Out[446]: array([2.46, 9.84, 7.38, 8.61])
马西莫: 注释掉这一行是否有同样的错误?
Nonlinear_Plot(xdata, ydata,ff, a,b,c)
Essam:如果我评论它就不会出错
正如我所想,问题出在第三行的那个函数中:
plt.plot(xdata, f(xdata, a, b, c), ...
你调用 f 传递扩展数据。 F 不能将序列乘以 'numpy.float64'
类型的非整数