Python TypeError:不支持**或pow()的操作数类型:';列表';和';int';和无效参数错误
我目前正在编写一个python脚本,它从.xlsx文件中读取数据。然后我用scipy提供的曲线拟合方法拟合数据。对于我的fitfunction,我首先使用了以下内容:Python TypeError:不支持**或pow()的操作数类型:';列表';和';int';和无效参数错误,python,numpy,curve-fitting,Python,Numpy,Curve Fitting,我目前正在编写一个python脚本,它从.xlsx文件中读取数据。然后我用scipy提供的曲线拟合方法拟合数据。对于我的fitfunction,我首先使用了以下内容: def fitfunc_polynome_OP_BL(x ,a, b, c, d, e, g, h, i): return a*(x**7) + b*(x**6) + c*(x**5) + d*(x**4) + e*(x**3) + g*(x**2) + h*x +i 但是我得到了上面提到的错误。我找到了这个错误的解决方
def fitfunc_polynome_OP_BL(x ,a, b, c, d, e, g, h, i):
return a*(x**7) + b*(x**6) + c*(x**5) + d*(x**4) + e*(x**3) + g*(x**2) + h*x +i
但是我得到了上面提到的错误。我找到了这个错误的解决方案:np.power(),但当我这样做时,我又得到了一个先前没有出现的错误。。因此,当我将“**”替换为“np.power()”以获得:
我得到一个无效参数错误(*),而我保留了相同数量的参数。。。有人能帮忙吗
(*)
在下一行中,您将为
np.power()
但是np.power()
方法需要两个位置参数。现在,使用两个参数调用np.power()
的正确方法,您的函数将是
def fitfunc_polynome_OP_BL(x ,a, b, c, d, e, g, h, i):
return a*(np.power(x,7)) + b*(np.power(x,6))+ c*(np.power(x,5)) + d*(np.power(x,4)) + e*(np.power(x,3)) + g*(np.power(x,2)) + h*x +i
编辑
要修复错误TypeError:“numpy.float64”对象不能解释为索引,请进行以下更改
wavelengths = [400, 450, 500, 550, 600, 650, 700, 800]
到
你需要做一个例子,它表明你不能做[some_list]**some_int
,所以看看你的例子,你提供的x
必须是一个列表。您正在执行类似于[1,2,3]**7的操作,这毫无意义。演示如何调用scipy
的曲线拟合函数,并正确缩进代码。您能否详细说明错误消息(超过无效参数错误
)?我用我的codeIi的一个最小版本编辑了我的帖子。我不清楚我如何解决我的问题。如果你给出了答案,你能详细说明一下吗?你可以用c*np.power((x,5))
部分代码替换为c*(np.power(x,5))
。因为在前一种情况下,您将一个参数(x,5)
传递给方法,但是power方法需要两个位置参数。这清楚吗?好的,但当我这样做的时候,我得到了以下信息:文件“snr_fitting_excel.py”,第72行,在fitfunc_polynome_OP_BL中返回a*(np.power(x,7))+b*(np.power(x,6))+c*(np.power(x,5))+d*(np.power r(x,4))+e*(np.power(x,3))+g*(np.power(x,2))+h*x+i TypeError:“numpy.float64”对象不能解释为index@WouterBloklandnp.power(x,4)
-空间是有意的吗?将波长=[400450500550600650700800]
更改为波长=np.array([400450500550600650700800])
#version in which all data will be read from the excelfile. The resulting fitparameters will be written into the excisting excelfile.
def snr_fitting_excel(number_of_altitude_points, row_begin_position_of_data, number_of_wavelengths):
n_alt = number_of_altitude_points
n_lambda = number_of_wavelengths
begin = row_begin_position_of_data
end = begin + n_alt
xlsx = pd.ExcelFile('Test.xlsx')
df = pd.read_excel(xlsx, 'SNR-COPY')
d = (n_alt, n_lambda)
height = np.zeros(d, dtype=int)
for j in range(begin, end):
for i in range(2,10):
height[j-begin][i-2] = (np.around(df.iat[j,i], decimals =0))
height = np.array(height)
#array with the different wavelengths at which the data was taken
wavelengths = [400, 450, 500, 550, 600, 650, 700, 800]
#fit the points with the desired function from above
for i in range(0, len(height)):
popt, pcov = curve_fit(fitfunc_polynome_OP_BL, wavelengths, height[i])
fig = plt.figure()
plt.plot(wavelengths, height[i], 'o')
plt.plot(wavelengths, fitfunc_polynome_OP_BL(wavelengths, *popt), 'r-', label ='fit: a=%5.3f, b=%5.3f, c=%5.3f, d=%5.3f, e=%5.3f, g=%5.3f, h=%5.3f, i=%5.3f' % tuple(popt))
fig.savefig("snr_op_fit_bl" + str((i+1)*5) +".pdf")
print(popt)
def fitfunc_polynome_OP_BL(x ,a, b, c, d, e, g, h, i):
# return a*(x**7) + b*(x**6) + c*(x**5) + d*(x**4) + e*(x**3) + g*(x**2) + h*x +i
return a*(np.power(x,7)) + b*(np.power(x,6))+ c*np.power((x,5)) + d*(np.power(x,4)) + e*(np.power(x,3)) + g*(np.power(x,2)) + h*x +i
if __name__ == '__main__':
print("\nFitting SNR_UV---------------------------------------------\n")
snr_fitting_excel(18,11,8)
c*np.power((x,5))
def fitfunc_polynome_OP_BL(x ,a, b, c, d, e, g, h, i):
return a*(np.power(x,7)) + b*(np.power(x,6))+ c*(np.power(x,5)) + d*(np.power(x,4)) + e*(np.power(x,3)) + g*(np.power(x,2)) + h*x +i
wavelengths = [400, 450, 500, 550, 600, 650, 700, 800]
wavelengths = np.array([400, 450, 500, 550, 600, 650, 700, 800])