0

我在 3D 数组中有一组图像(尺寸索引 * 高度 * 宽度)。

x_train, x_test, y_train, y_test = train_test_split(X, yy, test_size=0.2, random_state=42, stratify=y)
print(x_train.shape, x_test.shape, y_train.shape, y_test.shape)

dtrain = xgb.DMatrix(data=x_train, label=y_train)
dtest = xgb.DMatrix(data=x_test)

我从 XGBoost DMatrix 输入中得到一个错误:

ValueError: ('Expecting 2 dimensional numpy.ndarray, got: ', (2164, 120, 431))

上面打印的数组的形状:

(2164, 120, 431) (542, 120, 431) (2164, 3) (542, 3)

我对如何重塑数据感到困惑。是否需要为 2164 行 * 1 列?

4

2 回答 2

2

阅读文档似乎 X 需要是二维的,而 Y 需要是一维的。因此 X 需要具有形状 (index_of_sample, features),因此宽度和高度需要展平为单个向量(这对图像来说不是一个好主意,因为您失去了结构价值,但由于模型是 xgb,这又必须发生)

因此,您需要将 X 重塑为

x_train = x_train.reshape(x_train.shape[0], -1)
x_test = x_test.reshape(x_test.shape[0], -1)

此外,文档指出 Y 需要是一维的。因此,您需要以某种方式将 Y 更改为分类值,而不是当前(我假设)单热编码。

于 2020-05-03T11:00:07.873 回答
1

只需重塑您的xnumpy 数组。

x_train = x_train.reshape(x_train.shape[0], -1)

x_test = x_test.reshape(x_test.shape[0], -1)

于 2020-05-03T10:59:38.510 回答