【问题标题】:shape mismatch: value array of shape (2,) could not be broadcast to indexing result of shape (1,)形状不匹配:形状 (2,) 的值数组无法广播到形状 (1,) 的索引结果
【发布时间】:2017-10-23 13:18:46
【问题描述】:

我编写了以下脚本来使用 SkillMetrics 创建一个 taylor diagram,这是一个开源 Python 库,用于根据观察计算模型预测。

我写的脚本是:

import skill_metrics as sm 

# Arrange datasets in array
stats1 = np.array([ref,m1])
stats2 = np.array([ref,m2])
stats3 = np.array([ref,m3])

# calculating statistics
taylor_stats1 = sm.taylor_statistics(stats1[0],stats1[1])
taylor_stats2 = sm.taylor_statistics(stats2[0],stats2[1])
taylor_stats3 = sm.taylor_statistics(stats3[0],stats3[1])

# Store statistics in arrays
sdev = np.array([taylor_stats1['sdev'][0], taylor_stats1['sdev'][1],
                 taylor_stats2['sdev'][1], taylor_stats3['sdev'][1]])
crmsd = np.array([taylor_stats1['crmsd'][0], taylor_stats1['crmsd'][1],
                  taylor_stats2['crmsd'][1], taylor_stats3['crmsd'][1]])
ccoef = np.array([taylor_stats1['ccoef'][0], taylor_stats1['ccoef'][1],
                  taylor_stats2['ccoef'][1], taylor_stats3['ccoef'][1]])

产生:

sdev = array([ 0.02556107,  0.06264893,  0.06264893,  0.06264893])

crmsd = array([ 0.        ,  0.04918169,  0.04824091,  0.05082715])
ccoef = array([ 1.        ,  0.67423927,  0.68388202,  0.64162831])

然后我运行以下命令来创建泰勒图:

sm.taylor_diagram(sdev,crmsd,ccoef)

但是,我随后收到此错误:

shape mismatch: value array of shape (2,) could not be broadcast to indexing result of shape (1,)

【问题讨论】:

    标签: python numpy matplotlib


    【解决方案1】:

    这是一个 python 2 问题。似乎skill_metrics package 仅用于python 3。错误来自包的来源,您在这里没有做错任何事情。

    在 python3 中 the line in question 应该只抛出警告,而不是错误。

    您唯一的机会可能是使用 python 3 或深入研究源代码。

    • 找到文件site-packages\skill_metrics\overlay_taylor_diagram_circles.py
    • 在这个文件中,找到行(应该在第 50 行左右)

      # now really force points on x/y axes to lie on them exactly
      inds = range(0,len(th),(len(th)-1) // 4)
      xunit[inds[1:3:2]] = np.zeros(2)
      yunit[inds[0:4:2]] = np.zeros(3)
      
    • 替换为

      inds = range(0,len(th),(len(th)-1) // 4)
      xunit[74] = 0
      yunit[148] = 0
      

    【讨论】:

    • 你好@ImportanceOfBeingErnes 谢谢你的详细解释。我检查了我的 python 版本,我有 python --version Python 3.5.2 :: Anaconda custom (64-bit),现在我正在尝试查找源文件并让你知道。
    • 非常感谢,现在它可以通过更改源代码来工作 :)
    猜你喜欢
    • 1970-01-01
    • 2021-11-12
    • 2018-02-17
    • 1970-01-01
    • 2018-07-29
    • 2019-12-10
    • 2021-05-22
    • 2020-10-15
    相关资源
    最近更新 更多