ValueError:形状不匹配:对象不能广播到单个形状 [英] ValueError: shape mismatch: objects cannot be broadcast to a single shape

查看:68
本文介绍了ValueError:形状不匹配:对象不能广播到单个形状的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 SciPy 的 pearsonr(x,y) 方法,但我无法弄清楚为什么会发生以下错误:

I am using the SciPy's pearsonr(x,y) method and I cannot figure out why the following error is happening:

ValueError:形状不匹配:对象不能广播到单个形状

ValueError: shape mismatch: objects cannot be broadcast to a single shape

它计算前两个(我在一个循环中运行数千个这样的测试)然后死掉.有没有人对可能出现的问题有任何想法?

It computes the first two (I am running several thousand of these tests in a loop) and then dies. Does anyone have any ideas about what the problem might be?

r_num = n*(np.add.reduce(xm*ym))

这是发生错误的 pearsonr 方法中的行,任何帮助将不胜感激.

this is the line in the pearsonr method that the error occurs on, any help would be much appreciated.

推荐答案

这个特殊的错误意味着在线算术中使用的一个变量的形状与同一行上的另一个变量不兼容(即,既不同又非标量).由于 nnp.add.reduce() 的输出都是标量,这意味着问题出在 xmym,其中两个只是您的 xy 输入减去它们各自的平均值.

This particular error implies that one of the variables being used in the arithmetic on the line has a shape incompatible with another on the same line (i.e., both different and non-scalar). Since n and the output of np.add.reduce() are both scalars, this implies that the problem lies with xm and ym, the two of which are simply your x and y inputs minus their respective means.

基于此,我的猜测是您的 xy 输入具有彼此不同的形状,使它们与元素乘法不兼容.

Based on this, my guess is that your x and y inputs have different shapes from one another, making them incompatible for element-wise multiplication.

** 从技术上讲,并不是同一行上的变量具有不兼容的形状.唯一的问题是当两个变量相加、相乘等具有不兼容的形状时,无论变量是否是临时的(例如,函数输出).同一条线上具有不同形状的两个变量是可以的,只要在计算数学表达式之前通过其他方法纠正问题即可.

** Technically, it's not that variables on the same line have incompatible shapes. The only problem is when two variables being added, multiplied, etc., have incompatible shapes, whether the variables are temporary (e.g., function output) or not. Two variables with different shapes on the same line are fine as long as something else corrects the issue before the mathematical expression is evaluated.

这篇关于ValueError:形状不匹配:对象不能广播到单个形状的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆