![](/img/trans.png)
[英]TypeError: unsupported operand type(s) for ** or pow(): 'list' and 'int'
[英]TypeError: unsupported operand type(s) for ** or pow(): 'list' and 'int' and Invalid arguments error
我正在編寫一個python腳本,它從.xlsx文件中讀取數據。 然后我使用scipy提供的curve_fit方法擬合數據。 對於我的功能,我首先使用以下內容:
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()”替換“**”時得到:
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
我得到一個無效的參數錯誤(*),而我保持相同數量的參數...是否有人可以提供幫助?
(*)
Traceback (most recent call last):
File "snr_fitting_excel.py", line 81, in <module>
snr_fitting_excel(18,11,8)
File "snr_fitting_excel.py", line 45, in snr_fitting_excel
popt, pcov = curve_fit(fitfunc_polynome_OP_BL, wavelengths, height[i])
File "C:\ProgramData\Anaconda2\lib\site-packages\scipy\optimize\minpack.py", l
ine 736, in curve_fit
res = leastsq(func, p0, Dfun=jac, full_output=1, **kwargs)
File "C:\ProgramData\Anaconda2\lib\site-packages\scipy\optimize\minpack.py", l
ine 377, in leastsq
shape, dtype = _check_func('leastsq', 'func', func, x0, args, n)
File "C:\ProgramData\Anaconda2\lib\site-packages\scipy\optimize\minpack.py", l
ine 26, in _check_func
res = atleast_1d(thefunc(*((x0[:numinputs],) + args)))
File "C:\ProgramData\Anaconda2\lib\site-packages\scipy\optimize\minpack.py", l
ine 454, in func_wrapped
return func(xdata, *params) - ydata
File "snr_fitting_excel.py", line 70, in fitfunc_polynome_OP_BL
return a*(np.power(x,7)) + b*(np.power(x,6))+ c*np.power((x,5)) + d*(np.powe
r(x,4)) + e*(np.power(x,3)) + g*(np.power(x,2)) + h*x +i
ValueError:參數數量無效
PS這是代碼:
#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)
在下一行中,您將為np.power()
傳遞單個參數(元組類型np.power()
c*np.power((x,5))
但是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' object cannot be interpreted as an index
進行以下更改。
wavelengths = [400, 450, 500, 550, 600, 650, 700, 800]
至
wavelengths = np.array([400, 450, 500, 550, 600, 650, 700, 800])
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.