问题
写代码的错误TypeError: unsupported operand type(s) for +: 'builtin_function_or_method' and 'builtin_function_or_method'
源代码
import xarray as xr
import pandas as pd
import scipy.signal
import numpy as np
import datetime as dt
from scipy.stats import linregress
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
# 读取海温sst========
ds = xr.open_dataset(r'E:\huibao\task3\HadISST_sst.nc')
ds
sst = ds['sst']
sst = sst.loc[sst.time.dt.month.isin([12,1,2])].loc['1979-12-01':'2020-03-01','80':'30','-100':'40']
sst = sst.to_numpy()
sst = sst[:,:,:].reshape(41,3,sst.shape[1],sst.shape[2]).mean(1)
# 读取nao指数=========
naom = pd.read_csv(r'E:\huibao\task3\NAO_monthly.txt',header=None)
nao = scipy.signal.detrend(naom,type='linear')#去趋势化
naod=[]
for i in range(41):
naod.append(nao[11+12*i:13+12*i].mean)
nao1 = np.array(naod)
sst = np.array(sst)
s,r,p=np.zeros((sst.shape[1],sst.shape[2])),np.zeros((sst.shape[1],sst.shape[2])),np.zeros((sst.shape[1],sst.shape[2]))
# print(type(sst))
for i in range(sst.shape[1]):
for j in range(sst.shape[2]):
s[i,j],_,r[i,j],p[i,j],_,_ = linregress(sst[:,i,j],nao1[:])
出错代码
for i in range(sst.shape[1]):
for j in range(sst.shape[2]):
s[i,j],_,r[i,j],p[i,j],_,_ = linregress(sst[:,i,j],nao1[:])
出现错误
TypeError Traceback (most recent call last)
Input In [17], in line: 29>()
29 for i in range(sst.shape[1]):
30 for j in range(sst.shape[2]):
---> 31 s[i,j],_,r[i,j],p[i,j],_,_ = linregress(sst[:,i,j],nao1[:])
File D:\anacconda\envs\myenv1\lib\site-packages\scipy\stats\_stats_mstats_common.py:155, in linregress(x, y, alternative)
153 n = len(x)
154 xmean = np.mean(x, None)
--> 155 ymean = np.mean(y, None)
157 # Average sums of square differences from the mean
158 # ssxm = mean( (x-mean(x))^2 )
159 # ssxym = mean( (x-mean(x)) * (y-mean(y)) )
160 ssxm, ssxym, _, ssym = np.cov(x, y, bias=1).flat
File <__array_function__ internals>:180, in mean(*args, **kwargs)
File D:\anacconda\envs\myenv1\lib\site-packages\numpy\core\fromnumeric.py:3474, in mean(a, axis, dtype, out, keepdims, where)
3471 else:
3472 return mean(axis=axis, dtype=dtype, out=out, **kwargs)
-> 3474 return _methods._mean(a, axis=axis, dtype=dtype,
3475 out=out, **kwargs)
File D:\anacconda\envs\myenv1\lib\site-packages\numpy\core\_methods.py:179, in _mean(a, axis, dtype, out, keepdims, where)
176 dtype = mu.dtype('f4')
177 is_float16_result = True
--> 179 ret = umr_sum(arr, axis, dtype, out, keepdims, where=where)
180 if isinstance(ret, mu.ndarray):
181 ret = um.true_divide(
182 ret, rcount, out=ret, casting='unsafe', subok=False)
TypeError: unsupported operand type(s) for +: 'builtin_function_or_method' and 'builtin_function_or_method'
|
开始时,以为产生上述错误的原因是:虽python变量没有提前声明,但定义了nao1 = np.array(naod)
sst = np.array(sst)后仍产生相同错误,实在不清楚应该如何解决,希望可以有人帮忙指点一下,感激不尽。
看到这里遇到了 TypeError: unsupported operand type(s) for +: 'builtin_function_or_method' and 'builtin_function_or_method' 的错误。
这个错误通常是由于在代码中使用了不支持的操作符导致的。在这种情况下,可以试试使用加号(+)操作符来进行某些操作,但是操作数的类型不支持这个操作。
在代码中,这个错误发生在下列代码的第 31 行:
s[i,j],_,r[i,j],p[i,j],_,_ = linregress(sst[:,i,j],nao1[:])
看起来 linregress 函数中的参数 x 和 y 都是 NumPy 数组,但是它们的类型并不是数组,而是 builtin_function_or_method。这就是为什么会发生错误的原因。
要解决这个问题,需要检查 nao1 变量的类型,并确保它是一个 NumPy 数组。可能是因为忘记使用括号调用了 mean 函数,导致 nao1 的类型是函数。
要修复这个问题,可以试试使用括号来调用 mean 函数,例如:
naod.append(nao[11+12*i:13+12*i].mean())
这样 nao1 就应该是一个 NumPy 数组,不会再出现错误。
望采纳。
这个错误的意思是在执行等号右边的代码的时候发现了不能支持的操作符。在这里,它指的是你在使用加号(+)时遇到了不支持的类型。
仔细看一下代码,发现在调用 linregress 函数的时候,你传入了两个参数:sst[:,i,j] 和 nao1[:]。在这里,sst[:,i,j] 应该是一个数组,而 nao1[:] 应该是一个函数。
看起来你的代码是想要计算两个数组之间的线性关系。但是,由于在调用 nao1[:] 时没有加上圆括号,实际上调用的是 nao1 这个数组的 getitem 方法,而不是调用 nao1 这个列表中的函数。
为了解决这个问题,你需要加上圆括号来调用 nao1 中的函数:
s[i,j],_,r[i,j],p[i,j],_,_ = linregress(sst[:,i,j],nao1[:])
改成:
s[i,j],_,r[i,j],p[i,j],_,_ = linregress(sst[:,i,j],nao1[:]())