python - 试图按百分比拆分列表

标签 python

我正在尝试通过采用百分比并将元素从主列表中随机抓取到另外 2 个列表中来拆分列表。 trainingSet 是剩下的列表。当我生成一个随机索引以供选择时,我遇到了问题。此代码适用于一个小列表,但当我使用 (len(rawRatings) = 1000) 时它不起作用。

错误:

  File "/Applications/WingIDE.app/Contents/MacOS/src/debug/tserver/_sandbox.py", line 1, in <module>
      # Used internally for debug sandbox under external interpreter
    File "/Applications/WingIDE.app/Contents/MacOS/src/debug/tserver/_sandbox.py", line 29, in partitionRankings
    File "/Users/rderickson9/anaconda/lib/python2.7/random.py", line 241, in randint
return self.randrange(a, b+1)
    File "/Users/rderickson9/anaconda/lib/python2.7/random.py", line 217, in randrange
      raise ValueError, "empty range for randrange() (%d,%d, %d)" % (istart, istop, width)
  ValueError: empty range for randrange() (0,0, 0)

raw Ratings 是一个列表,testPercent 是一个 float 。

例如

rawRatings = [(123,432,4),(23,342,3),(23,123,5),(234,523,3),(34,23,1), (12,32,4)]
testPercent = .2
partitionRankings(rawRatings, testPercent)
[(23,123,5),(234,523,3),(34,23,1),(123,432,4),(12,32,4)],[(23,342,3)]


def partitionRankings(rawRatings, testPercent):
    testSet = []
    trainingSet = []
    howManyNumbers = int(round(testPercent*len(rawRatings)))
    declineRandom = 0
    while True:
        if declineRandom == howManyNumbers:
                    break        
        randomIndex = random.randint(0, (len(rawRatings)-1)-declineRandom)
        testSetTuple = rawRatings[randomIndex]
        del rawRatings[randomIndex]
        testSet.append(testSetTuple)

        declineRandom = declineRandom + 1
    trainingSet = rawRatings[:]
    return (trainingSet), (testSet)

我不想选择相同的随机索引。一次,我选了一个,我不想再乱选了。我认为这是不正确的。这是我遇到问题的部分。

randomIndex = random.randint(0, (len(rawRatings)-1)-declineRandom)

最佳答案

由于训练集的顺序无关紧要,您可以使用完全不同的策略来做到这一点 - 打乱 rawRatings 列表,然后将第一个 howManyNumbers 元素作为您的测试集,然后休息作为你的训练集。

import random

def partitionRankings(rawRatings, testPercent):
    howManyNumbers = int(round(testPercent*len(rawRatings)))
    shuffled = rawRatings[:]
    random.shuffle(shuffled)
    return shuffled[howManyNumbers:], shuffled[:howManyNumbers]

至于为什么你的代码不起作用,正如你猜到的那样,问题在于这一行:

randomIndex = random.randint(0, (len(rawRatings)-1)-declineRandom)

问题出在 -declineRandom 上。

  • 每次执行循环时,都会删除您选择的条目,因此即使您再次获得相同的索引,您也不会选择相同的元素。
  • 如果您没有在每次迭代中从列表中删除元素,这不会阻止两次选择相同的元素 - 这只会阻止您选择任何最后的 declineRandom 元素。
    • 您必须在每次迭代时将元素移动到列表的末尾。
  • 因为您删除元素,然后不替换列表末尾的元素,所以 len(rawRatings) 缩小,而 declineRandom 增长。
    • 如果您有一个包含 1000 个项目的列表并尝试将 600 个项目放入测试集中,那么当您在测试集中有 550 个项目时,您将尝试获得一个大于或等于零且小于或等于零的随机整数等于 (450-1)-550=-101。显然,您实际上不会达到这一点,但希望它能说明问题。

关于python - 试图按百分比拆分列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23299099/

相关文章:

python - 如何从文本匹配组中排除某些字符?

python - Django Rest Framework 发送验证邮件

python - 如何使用Keras TimeseriesGenerator为每n个训练样本获取一个验证样本?

python - 如何在Python Maya中存储floatSliderGrp的值

python - 为什么我安装 virtualenv 后 pip 不可用?

python - 在 Python 中删除列表中的重复字典

python - 了解 Windows 上的 Python 3 argparse()

python - blender 比例值未更新

python - 如何获取我之前使用其类找到的图像的 xpath?

python - 我不小心删除了我的/usr/lib/python3.6/site-packages/*