numpy Python索引问题-“索引错误:数组索引太多:数组是一维,但对2个进行了索引'

ttcibm8c  于 2022-11-24  发布在  Python
关注(0)|答案(1)|浏览(543)

请有人能告诉我为什么下面的代码不工作,以及什么是最好的解决办法?

Choices # variable containing True or False in each element.
Choices.shape = (18978,)
BestOption # variable containing 1 or 2 in each element.
BestOption.shape = (18978, 1)

Choices[BestOption==1] # I want to look up the values in choices for all instances where BestOption is 1.

出现以下错误:

IndexError: too many indices for array: array is 1-dimensional, but 2 were indexed
b0zn9rqh

b0zn9rqh1#

BestOption是一个一维的“列向量”,它实际上是由许多行组成的,被当作二维矩阵来处理。您可以简单地将它重新整形为一维的“行向量”:

Choices[BestOption.reshape(-1)==1]

相关问题