[英]shape mismatch: value array of shape X could not be broadcast to indexing result of shape . . . X?
Consider: I'm trying to generate s
samples the von mises distribution for every element of k
, which raises an error for k=0
(which is my null hypothesis, so I want it as accurate as possible). 考虑:我正在尝试为k
每个元素生成s
样本的von mises分布,这会引起k=0
的误差(这是我的零假设,因此我希望它尽可能准确)。 I'm trying to "fudge" it by giving a low k
and randomizing the bias direction. 我试图通过给出一个低k
并随机化偏置方向来“捏造”它。
Assume 假设
import numpy as np
s = 1000
k = np.arange(10)
theta = np.zeros_like(k)
shp = (10,)
Then the following code 然后下面的代码
import scipy.stats as stat
rpt = (s,) + tuple(np.ones_like(shp))
theta = np.tile(theta, rpt)
k_zero = np.logical_not(k)
theta[:, k_zero] = np.random.rand(np.sum(k_zero), s) * 2 * np.pi - np.pi
k[k_zero] = .001
ks = np.tile(k, rpt)
gives the error 给出错误
Traceback (most recent call last):
File "<ipython-input-blah>", line 1, in <module>
theta[:, k_zero] = np.random.rand(np.sum(k_zero), s) * np.pi - np.pi / 2
ValueError: shape mismatch: value array of shape (1,1000) could not be broadcast to indexing result of shape (1,1000)
But . 但是。 . 。 . 。 those shapes are the same. 这些形状是相同的。 Why can't I do that? 我为什么不能这样做?
EDIT: as pointed out below - 编辑:如下所示-
theta[:, k_zero] = np.random.rand(s, np.sum(k_zero)) * np.pi - np.pi / 2
works. 作品。 Is this just a bug in the error message? 这仅仅是错误消息中的错误吗?
It appears that there is a bug in the print out that you received 您收到的打印输出中似乎有一个错误
The problem is because your shape in the random number assignment is wrong. 问题是因为您在随机数分配中的形状不正确。 Look at the following... 看下面的...
theta[:, k_zero] = np.random.rand(np.sum(k_zero), s).reshape(s,1)
The way you can debug such a problem is by investigating the shapes of the assigned variable and the variable you want to assign it to. 调试此类问题的方法是研究分配变量的形状以及要将其分配给的变量。
For example what I did was 例如我所做的是
theta.shape
np.random.rand(np.sum(k_zero), s).shape
and I saw that the random number came out with transposed dimensions 我看到随机数是换位后的尺寸
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.