查找相同形状数组的统计信息

时间:2016-05-08 03:26:06

标签: python arrays matplotlib statistics

您好我正在尝试为某些数据映射pearson R统计量。但是数据是通过数组。我已经使阵列具有相同的形状和大小,但无论我做什么,代码都不会运行而不是给我错误:
'具有多个元素的数组的真值是不明确的。使用a.any()或a.all()'

任何帮助将不胜感激!!!

f = netCDF4.Dataset('C:/data/Feb.nc',
                        'r')
    # Import variables
    v = f.variables['store_Bio'][0:28, 0:24, 0, 0:60, 0:60])/81
    t = f.variables['temp_a'][0:28, 0:24, 0, :, :]
    g = f.variables['skin_temp'][0:28, 0:24, :, :]
    cc = f.variables['cloud_cover'][0:28, 0:24, 0, :, :]


    # Mean over array to make them the same shape
    v1 = v.mean(axis=(0, 1))
    t1 = t.mean(axis=(0, 1))
    g1 = g.mean(axis=(0, 1))
    cc1 = cc.mean(axis=(0, 1))

    # Pearson R stats
    p11 = pearsonr(v1, t1)
    p21 = pearsonr(v1, g1)
    p31 = pearsonr(v1, cc1)

1 个答案:

答案 0 :(得分:0)

pearsonr的输入类型应该是数组。

>>> a = np.array([[1,2],[3,4]])
>>> b = np.array([[1,2],[3,4]])
>>> pearsonr(a,b)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.7/dist-packages/scipy/stats/stats.py", line 2518, in pearsonr
    r = max(min(r, 1.0), -1.0)
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

>>> pearsonr(a.ravel(),b.ravel())
(1.0, 0.0)

对于每个相应的值,您可以尝试以下代码剪切。它将为您提供一个等于输入大小的result数组。

>>> result = [pearsonr(x, y) for x, y in zip(a,b)]