0

This is just a curiosity question really. My code works. I have a large array of instances that I'm attempting to break into different groups (training, validation, and testing). They're represented in a single list, but it is important that they stay in groups of 23. Here's my implementation:

train_end = int(len(instances)*TRAINING_END)
while train_end % CHANNELS != 0:
    train_end -= 1
valid_end = int(len(instances)*VALIDATION_END)
while valid_end % CHANNELS != 0:
    valid_end += 1

And then I partition the lists using [:train_end], [train_end:valid_end], [valid_end:]. I feel like everything that takes this many steps in python has a simpler way. Any ideas?

4

1 回答 1

1

您可以通过执行以下操作跳过 while 循环:

train_end = (int(len(instances)*TRAINING_END) // CHANNELS) * CHANNELS

这样做的效果是确保 可以int(len(instances)*TRAINING_END)被 整除CHANNELS

对于valid_end,等效效果为:

valid_end = (int(len(instances)*VALIDATION_END) // CHANNELS + 1) * CHANNELS
于 2013-10-14T16:51:13.570 回答