问题
写代码的错误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 <cell 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)后仍产生相同错误,实在不清楚应该如何解决,希望可以有人帮忙指点一下,感激不尽。