【问题标题】:How to split dataset into train and test when the number of samples do not match?当样本数量不匹配时,如何将数据集拆分为训练和测试?
【发布时间】:2020-05-04 11:22:35
【问题描述】:

我正在使用基于 this 示例的基于 Keras 构建的图像分类模型的 PlantVillage 数据集。我正在尝试将数据集分为训练和测试。但是,我有 1676 张图像,只有 15 个标签,每个标签对应于包含图像的文件夹的名称。这是我正在使用的代码:

x_train, x_test, y_train, y_test = train_test_split(np_image_list, image_labels, test_size=0.2, random_state=42)

我得到的错误是:

 ValueError: Found input variables with inconsistent numbers of samples: [1676, 15]

【问题讨论】:

  • 你必须输入 X 和 y。两者都需要相同的尺寸。您没有为 y 使用不同标签的数量,而是为 X 中的每个值使用一个标签
  • 维度并不是真正正确的术语。 X 和 y 的长度必须相同

标签: python jupyter-notebook


【解决方案1】:

当样本不匹配时,您需要先连接它们。例如,如果您有包含目标标签的 100 列的 train.csv 和包含 99 列的 test.csv,那么您需要从 train.csv 中删除目标标签并连接 train.csv 和 test.csv。将两者合并后,您可以使用 .iloc 将它们拆分为您选择的训练和测试数据集样本。

df = 200 columns
train = df.iloc[:100,:]
test = df.iloc[100:,:]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-05-01
    • 1970-01-01
    • 2013-06-16
    • 2019-06-30
    • 2020-06-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多