Python 类型错误:can';t将序列乘以类型为'的非整数;numpy.float64';尽管我没有

Python 类型错误:can';t将序列乘以类型为'的非整数;numpy.float64';尽管我没有,python,numpy,runtime-error,curve-fitting,Python,Numpy,Runtime Error,Curve Fitting,这段代码在我将其转换为函数之前运行良好,那么有什么问题吗?我看不出代码中的任何地方有我将字符串/列表乘以浮点数的情况 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 = cu

这段代码在我将其转换为函数之前运行良好,那么有什么问题吗?我看不出代码中的任何地方有我将字符串/列表乘以浮点数的情况

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运行):


这将复制错误消息:

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'
但是如果扩展数据是一个数组:

In [446]: np.array([1.23])[0]*np.array([2,8,6,7])                                                      
Out[446]: array([2.46, 9.84, 7.38, 8.61])
马西莫:
你在注释这一行时有相同的错误吗

非线性图(扩展数据、ydata、ff、a、b、c)


埃萨姆:如果我评论它,我就不会出错

正如我所想,问题在于该函数的第三行:

绘图(扩展数据,f(扩展数据,a,b,c)

您调用f传递扩展数据。
F不能将序列乘以'numpy.float64'类型的非整数。

注释这一行时是否有相同的错误?#非线性图(扩展数据、ydata、ff、a、b、c)如果我对它进行注释,我不会出错。让我困惑的是,在我将其转换为模块化形式之前,该代码运行得非常好。这不应该是原因,因为即使在我将代码转换为模块化之前传递了扩展数据,它也能顺利运行。@Essam您确定吗?同时发布工作代码,否则就无法帮助您了。我刚写完就这样做了通过编辑来编辑评论。@Essam,我已经给了你正确的答案。请将我的答案标记为已接受,然后我将显示您键入错误的单个字符。通过将列表制作成numpy数组,确实解决了这个问题,谢谢。
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'
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'
In [446]: np.array([1.23])[0]*np.array([2,8,6,7])                                                      
Out[446]: array([2.46, 9.84, 7.38, 8.61])