如何修复:"All the input array dimensions except for the concatenation axis must match exactly" python

How to fix: "All the input array dimensions except for the concatenation axis must match exactly" python

我将循环结果保存在一个数组中,但出现错误:

all the input array dimensions except for the concatenation axis must match exactly

密码是:

r = 24
sp = np.zeros(r)
p = np.zeros(r)
for t in range(r):
    sp[t], p[t] = scipy.stats.spearmanr((df1['Pasto_1'][t:]), (df1['HumedadPasto_1'][:-t]))

我希望输出保存在 sp 中。如果你知道我做错了什么,非常感谢!

第一次迭代最有可能出现问题:当 t 为 0 时,df1['Pasto_1'][t:] 是完整系列,但 df1['HumedadPasto_1'][:-t] 是空的。您可以简单地将 0 视为特例并保持良好状态:

for t in range(r):
    s1 = df1['Pasto_1'][t:]
    s2 = df1['HumedadPasto_1'][:-t] if t > 0 else df1['HumedadPasto_1']
    sp[t], p[t] = scipy.stats.spearmanr(s1, s2)