python error : too many indices for array

我怕爱的太早我们不能终老 提交于 2019-12-06 04:14:43

Notice that the example in the User Guide shows that X is 2-dimensional while y is 1-dimensional:

>>> X_train.shape, y_train.shape
((90, 4), (90,))

Some programmers use capitalized variables for 2-dimensional arrays and lower-case for 1-dimensional arrays.

Therefore use

Y = result['label']

instead of

Y = result[['label']]

I am assuming that result is a pandas DataFrame. When you index a Dataframe with a list of columns such as ['label'], a sub-DataFrame -- which is 2-dimensional -- is returned. If you index the DataFrame with a single string, a 1-dimensional Series is returned.


Finally, note that the IndexError

IndexError: too many indices for array

is raised on this line

cls_test_folds = test_folds[y == cls]

because y is 2-dimensional so y == cls is a 2-dimensional boolean array and test_folds is 1-dimensional. The situation is similar to the following:

In [72]: test_folds = np.zeros(5, dtype=np.int)
In [73]: y_eq_cls = np.array([(True, ), (False,)])
In [74]: test_folds[y_eq_cls]
IndexError: too many indices for array
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!