np。试图选择两列时出现错误

问题描述 投票:0回答:1

我正在尝试对sklearn的'Linnerud'数据集执行多元回归。我有一个20x3的np数组,但是我只想选择三列中的两列。我可以使用以下方法添加一个自变量:

X_for_1D_LR = X[:,np.where(np.array([feature_names_X])[0] == 'Situps')[0]]

但是在尝试添加另一个自变量时遇到问题。

X_for_2D_LR = X[:,np.where(np.array([feature_names_X])[0] == 'Situps', np.array([feature_names_X])[0] == 'Chins')[0]]

并且得到错误“ ValueError:x和y均应给出,或者都不应该给出”任何帮助将不胜感激!

python numpy scikit-learn regression valueerror
1个回答
0
投票

重新组织代码,使逻辑更加明显。 Python是一种带有大量空格的语言。利用这个优势:

X_for_2D_LR = X[:,
                np.where(
                    np.array([feature_names_X])[0] == 'Situps',
                    np.array([feature_names_X])[0] == 'Chins'
                )[0]]

也许现在,如果您阅读错误消息ValueError: either both or neither of x and y should be givendocumentation代表np.where,则可以看到您的错误。>>

您的条件(例如np.array([feature_names_X])[0] == 'Situps')在列表中,并且应由布尔运算符而不是逗号分隔:

X_for_2D_LR = X[:,
                np.where(
                    np.array([feature_names_X])[0] == 'Situps' or
                    np.array([feature_names_X])[0] == 'Chins'
                )[0]]
© www.soinside.com 2019 - 2024. All rights reserved.