Train Test Split
easy· Machine Learningruns: 0Implement a function that shuffles a dataset and splits it into training and test sets. Return four arrays: X_train, X_test, y_train, y_test. The function accepts a test_size fraction and a random seed for reproducibility.
sign in to paste and practice your own solution
wpm 0acc 100%time 0:000 / 402
import numpy as np
def train_test_split(
X, y, test_size: float = 0.2, random_state: int = 0
):
rng = np.random.default_rng(random_state)
n_samples = len(X)
indices = np.arange(n_samples)
rng.shuffle(indices)
split = int(n_samples * (1 - test_size))
train_idx = indices[:split]
test_idx = indices[split:]
return X[train_idx], X[test_idx], y[train_idx], y[test_idx]
click the box to focus · tab inserts 4 spaces · backspace to correct · esc to pause
desktop only
codedrill is a typing game and needs a real keyboard. open this on a laptop or desktop to practice.
you can still browse problems and sections from your phone.