Skip to content
DeepForgeby svx
All problems
MediumML Fundamentalsml-022

Shuffled Train/Test Split

Problem

Split indices 0..n-1 after a seeded shuffle. Call random.seed(seed), shuffle the index list, then assign the first n_test = int(n * test_fraction) shuffled indices to the test set and the remainder to train.

Return [train_indices, test_indices], both sorted in ascending order.

Hint

Seed first, shuffle in place, then slice with n_test and sort each side.

Test cases

case 1
in: [10,0.3,42]
expected: [[0,1,4,5,6,8,9],[2,3,7]]
case 2
in: [8,0.25,7]
expected: [[0,1,2,3,4,5],[6,7]]
case 3
in: [6,0.5,0]
expected: [[0,3,5],[1,2,4]]
case 4
in: [5,0.4,123]
expected: [[0,2,4],[1,3]]
Starter codePython
import random
def shuffled_split_indices(n, test_fraction, seed):
    # Returns [train_indices, test_indices]
    # Your code here
    pass

4 test cases · run your code against them in the editor