m0_55056201 2022-12-30 12:22 采纳率: 100%
浏览 303
已结题

【python】TypeError: unsupported operand type(s) for +: 'builtin_function_or_method' and

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

  • 写回答

3条回答 默认 最新

  • |__WhoAmI__| 2022-12-30 12:47
    关注

    看到这里遇到了 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 数组,不会再出现错误。
    望采纳。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
    1人已打赏
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 1月7日
  • 已采纳回答 12月30日
  • 创建了问题 12月30日

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么