列表和int之间的Python类型错误

时间:2018-01-22 21:38:13

标签: python-3.x

以下是从另一位研究人员那里借来的一些代码,他正在调查与我试图解决的问题类似的问题。本质上,我导入3个变量 - x,mjo_amp和mjo_phase。我计算每个MJO阶段(mjo_phase)的x的平均值,拒绝mjo_amp小于1的任何值。然后该函数重新排序数据以尝试运行蒙特卡罗模拟的版本并测试统计显着性。

运行代码时出现以下错误:

>>> pprint( mjo_comp(x, mjo_amp, mjo_phase) )
Traceback (most recent call last):
File "<pyshell#14>", line 1, in <module>
pprint( mjo_comp(x, mjo_amp, mjo_phase) )
File "<pyshell#13>", line 36, in mjo_comp
tt = (mjo_phase==ph) * (mjo_amp>=1)
TypeError: '>=' not supported between instances of 'list' and 'int'

我导入了这样的数据:

import pandas
>>> colnames = ['x', 'mjo_phase', 'mjo_amp']
>>> data = pandas.read_csv('KBRO Daily Rainfall MJO Phase Simplified.csv', names = colnames)
>>> x = data.x.tolist()
>>> mjo_phase = data.mjo_phase.tolist()
>>> mjo_amp = data.mjo_amp.tolist()


def mjo_comp(x, mjo_amp, mjo_phase, N=1000, l0=50, L=1000, alpha=0.05, MCMC=True):
'''
Calculates composites of x based on MJO phase and amplitude.  Divides the
MJO into 8 phases and rejects all points for MJO amplitude less than 1
    phases = vector from 1 to 8
    x_mjo  = composites of x with 8 mjo phases
If MCMC = True then also performs a statistical significance test by
generating N randomly shuffled MJO phase vectors and recalculating the composite
results, returning a confidence interval corresponding to 1-alpha confidence.
'''
n = 8
count = np.zeros(n)
x_mjo = np.zeros(n)
x_mjo_mc = np.zeros((n,N))
phases = range(1,n+1)
#
for ph in phases:
    tt = (mjo_phase==ph) * (mjo_amp>=1)
    x_mjo[ph-1] = np.nanmean(x[tt])
    count[ph-1] = np.sum(tt)
#
if MCMC:
    for i in range(N):
        l = (l0+np.floor(np.random.rand(1)*L)).astype(int)[0]
        mjo_phase_mc = np.append(mjo_phase[l:], mjo_phase[:l])
        for ph in phases:
            tt = (mjo_phase_mc==ph) * (mjo_amp>=1)
            x_mjo_mc[ph-1,i] = np.nanmean(x[tt])
#
    x_mjo_95CI = np.percentile(x_mjo_mc, [100*alpha/2., 100*(1. - alpha/2.)], axis=1).T
else:
    x_mjo_95CI = None
#
return phases, x_mjo, count, x_mjo_95CI

知道这里出了什么问题吗?似乎代码可能期望mjo_amp成为单个数字,但我需要将其作为列表导入,每个数据点的mjo_amp都不同。

0 个答案:

没有答案