简体   繁体   English

形状不匹配:形状X的值数组无法广播到形状的索引结果。 。 。 X?

[英]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? 这仅仅是错误消息中的错误吗?

The error message 错误讯息

It appears that there is a bug in the print out that you received 您收到的打印输出中似乎有一个错误

The code fix 代码修复

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.

相关问题 形状不匹配:形状(2,)的值数组无法广播到形状(1,)的索引结果 - shape mismatch: value array of shape (2,) could not be broadcast to indexing result of shape (1,) ValueError:形状不匹配:形状(30,)的值数组无法广播到形状(9,30)的索引结果 - ValueError: shape mismatch: value array of shape (30,) could not be broadcast to indexing result of shape (9,30) 索引错误:形状不匹配:索引数组无法与形状一起广播 - Indexing error: shape mismatch: indexing arrays could not be broadcast together with shapes 将函数应用于小数据帧:形状不匹配:无法广播形状 (4,) 的值数组 - Applying a function to a small dataframe: shape mismatch: value array of shape (4,) could not be broadcast IndexError:形状不匹配:索引数组不能与形状(2,)(9,)一起广播 - IndexError: shape mismatch: indexing arrays could not be broadcast together with shapes (2,) (9,) 形状不匹配:索引数组不能与形状一起广播 - shape mismatch: indexing arrays could not be broadcast together with shapes IndexError:形状不匹配:索引数组无法与形状(2,)(3,)一起广播 - IndexError: shape mismatch: indexing arrays could not be broadcast together with shapes (2,) (3,) ValueError:对于 arrays,无法将输入数组从形状 (x,y) 广播到形状 (x-1, y),形状使用相同的变量定义 - ValueError: Could not broadcast input array from shape (x,y) into shape (x-1, y) for arrays with shape defined with the same variables IndexError:形状不匹配:索引数组不能与形状一起广播 - IndexError: shape mismatch: indexing arrays could not be broadcast together with shapes 具有lxmxn广播形状的高级整数索引 - Advanced integer indexing with l x m x n broadcast shape
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM