[已解决]IndexError: too many indices for array: array is 2-dimensional, but 3 were indexed 或IndexError: boolean index did not match indexed array along dimension 1

在这里插入图片描述
问题描述:
试图用布尔向量y,挑选对应的x。numpy报错如上

y.shape
(100, 1)

x.shape
(100, 3)

解决方案
由于x[y == 1],x[y == 1, -2]这类操作的y只允许是向量(只允许有一个维度)
y.shape (100,)这一类

进行该操作即可↓

y=np.squeeze(y) 

解决效果如下图
在这里插入图片描述

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐