Splitting data in x_train and x_test gives error: Too many values to unpack expected 2

Splitting data in x_train and x_test gives error: Too many values to unpack expected 2

每当我尝试将数据拆分为 x_trainx_test 时,我都会收到以下错误:

Too many values to unpack expected 2

我的代码:

import glob
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.image as mpimg

for img in glob.glob("F:/Pics/Training_data/*.jpg"):
  k_images = mpimg.imread(img)
  plt.show()
  plt.imshow(k_images)

  (x_train, _), (x_test, _) = k_images

此处k_imagesnp.ndarray类型,包含10张图片。

请告诉我我应该更改什么以避免在 k_images.

的火车、测试拆分中出现错误

假设ndarray的形状是(10,x,y,c) 如果你不关心随机性,你可以只使用 pythons 列表符号

x = np.arange(10).reshape((10, 1,1,1))
x_train = x[:8]
x_test = x[8:]
x_train.shape
(8, 1, 1, 1)
x_test.shape
(2, 1, 1, 1)

或者只使用 sklearn train_test_split 函数,它更好,并且在选择过程中增加了随机性...

https://scikit-learn.org/stable/modules/generated/sklearn.model_selection.train_test_split.html

我想你要找的是

x_train, x_test = np.array_split(k_images, 2, 2)

Documentation found here.

如果您正在研究 ML,我建议您查看 sklearn,例如 sklearn.model_selection.train_test_split 会对您有很大帮助,documentation found here