我正在尝试使用while循环将列表分为训练数据和测试数据,但是在运行一两次迭代后,立即弹出randrange()错误。无法理解出了什么问题。提取的csv文件具有767行数据。下面是代码:

dataset = list(csv.reader(open("Data\diabetes.csv", 'r'), delimiter = ","))

trainSet = []
trainSize = int(0.67* len(dataset))
while len(trainSet) < trainSize:
      x = len(dataset)
      index = random.randint(1, x-1)
      trainSet.append(dataset[index])
      dataset = dataset.pop(index)


以下是错误:

Traceback (most recent call last):
  File "<stdin>", line 3, in <module>
  File "C:\Users\Vikash Patel\AppData\Local\Programs\Python\Python38\lib\random.py", line 248, in randint
   return self.randrange(a, b+1)
  File "C:\Users\Vikash Patel\AppData\Local\Programs\Python\Python38\lib\random.py", line 226, in randrange
   raise ValueError("empty range for randrange() (%d, %d, %d)" % (istart, istop, width))


ValueError:randrange()的空范围(1、1、0)

最佳答案

当您尝试呼叫ValueError: empty range for randrange() (1, 1, 0)时发生random.randint(1, 0)

之所以发生这种情况是因为,当您希望弹出带有项目的数据集时,dataset = dataset.pop(index)行将您的数据集设置为等于弹出的项目(长度为1)。然后,当您的循环在下一次进行迭代时,x = len(dataset)将x设置为1,并且行index = random.randint(1, x-1)调用random.randint(1, 0)

尝试以下方法:

import random

dataset = [10,11,12,13,14,15]

trainSet = []
trainSize = int(0.67* len(dataset))
while len(trainSet) < trainSize:
    x = len(dataset)
    index = random.randint(1, x-1)
    trainSet.append(dataset[index])

    # this changes the size of the dataset
    dataset.pop(index)


输出:

> trainSet
[13, 12, 15, 14]

10-07 15:13