【问题标题】:ValueError: y should be a 1d array, got an array of shape insteadValueError: y 应该是一维数组,得到一个形状数组
【发布时间】:2022-01-26 15:20:21
【问题描述】:

我知道这被问了很多次,但我想不通。

我有这种格式的数据集。前 767 列用于训练并具有训练数据。接下来的 669 列是标签。

标签采用一个热向量的格式,即 [0,0,0......1,0,0]。所以我有 669 列。现在我想使用 XGBoost 对其进行训练。我的代码是。

self.clf = XGBClassifier(objective="multi:softmax", num_classes=669)
data = single_data.iloc[:, 0:767]
label = single_data.iloc[:, 767:]
self.clf.fit(data, label)

我得到的错误是

ValueError: y should be a 1d array, got an array of shape (1638, 670) instead.

我该如何解决这个问题?谢谢

【问题讨论】:

  • 它告诉你不要对标签进行热编码 - 只需使用单个列,其中每个类都有不同的数字
  • 有什么办法可以用一热吗?
  • 把你的一个热点转换成类。我认为最简单的方法是lab = np.argmax(label,axis=1)

标签: python machine-learning scikit-learn xgboost


【解决方案1】:

我假设您的数据如下所示:

import pandas as pd
label = pd.DataFrame({'c0':[0,1,0,0,0], 'c1':[1,0,0,0,0], 'c2':[0,0,1,1,0], 'c3':[0,0,0,0,1]})
print(label)

输出

   c0  c1  c2  c3
0   0   1   0   0
1   1   0   0   0
2   0   0   1   0
3   0   0   1   0
4   0   0   0   1

将它们转换为整数

label = label.apply(lambda x: x.argmax(), axis=1).values

现在你的标签看起来像这样,一个数组:

array([1, 0, 2, 2, 3], dtype=int64)

【讨论】:

    猜你喜欢
    • 2021-10-15
    • 1970-01-01
    • 1970-01-01
    • 2021-11-05
    • 1970-01-01
    • 2022-01-09
    • 2021-10-06
    • 2021-08-20
    • 2019-07-09
    相关资源
    最近更新 更多