【问题标题】:How to slice a list into 2 parts based on percentage?如何根据百分比将列表分成两部分?
【发布时间】:2021-07-17 08:40:19
【问题描述】:

我需要将我的数组分成两部分,第一个需要有前 90%,下一个应该有其余部分。但我只得到了第二个数组的更正结果。

例如:从 11500 我得到 1150 的 test_images 但我得到 11500 的 tain_images。

tain_images, test_images = np.split(imagesArr, [int(len(imagesArr)*0.9)])

【问题讨论】:

  • 你能提供你的代码吗?

标签: python arrays numpy deep-learning training-data


【解决方案1】:

你应该使用 sklearn train_test_split()

from sklearn.model_selection import train_test_split

train_images, test_images  = train_test_split(imagesArr, test_size=0.1)    

根据所需的测试数据集百分比更改test_size 值。

【讨论】:

    【解决方案2】:

    它对我有用:

    import numpy as np
    imagesArr = []
    for i in range(11500):
        imagesArr.append(str(i+1))
    imagesArr = np.array(imagesArr)
    tain_images, test_images = np.split(imagesArr, [int(len(imagesArr)*0.9)])
    print(len(tain_images))
    print(len(test_images))
    

    结果:

    10350
    1150
    

    【讨论】:

      【解决方案3】:

      您的代码对我有用。可能是您在某处出现拼写错误,例如代码后面某处的tain_images 中的拼写错误。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-07-17
        • 2011-10-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多