TypeError: 不能用'numpy.float64'类型的非int乘以序列,即使我没有这样做。

问题描述 投票:0回答:1

在我把它做成函数之前,这段代码工作得很好,那么有什么问题呢?我看不到代码中的任何地方有我用一个浮点数乘以一个字符串列表。

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'来运行这段代码,这就是我们得到的结果(从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()
python numpy runtime-error curve-fitting
1个回答
1
投票

这重现了错误信息。

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, bx 都是numpy数组,但很容易出现一个或多个不是数组的情况。

检查那些 a,b,c "常数"。 不要以为它们是正确的。

或者,如果 xxdata,一个列表。

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])

0
投票

Massimo:你也有同样的错误,请把这行注释出来?

Nonlinear_Plot(xdata, ydata,ff, a,b,c)

伊珊 我评论它的时候没有错误

正如我所想的,问题出在函数的第三行。

plt.plot(xdata, f(xdata, a, b, c), ...

你调用f传递xdata.F 不能用'numpy.float64'类型的非int乘以序列。

© www.soinside.com 2019 - 2024. All rights reserved.