TensorTonic
Problems
Stratified Train/Test Split
Data Processing
Hard

Split features and labels while approximating the same class proportions in train and test. For each class with ncn_cnc​ samples, compute:

nctest=round⁡(nc t)n_c^{\mathrm{test}} = \operatorname{round}(n_c\,t)nctest​=round(nc​t)

Here, ttt is test_size. When a class has more than one sample, cap its test count at nc−1n_c-1nc​−1 so at least one remains in training. Create one generator with np.random.default_rng(seed) and reuse it for every class, visiting the classes in ascending label order. Shuffle the indices of each class and send the first nctestn_c^{\mathrm{test}}nctest​ shuffled indices to the test set and the remaining ones to the training set. Combine the selected indices, sort each final index set, and return X_train, X_test, y_train, and y_test as NumPy arrays in a dictionary.

Loading visualization...

Examples

Input: X = [0, 1, 2, 3, 4, 5], y = [0, 0, 0, 1, 1, 1], test_size = 0.33, seed = 42

Output: {"X_train": [0, 1, 4, 5], "X_test": [2, 3], "y_train": [0, 0, 1, 1], "y_test": [0, 1]}

Explanation: One seeded sample from each class enters the test split.

Input: X = [[1, 0], [2, 0], [3, 0], [4, 0], [5, 0], [6, 0], [7, 0], [8, 0], [9, 0], [10, 0]], y = [0, 0, 0, 0, 0, 0, 0, 1, 1, 1], test_size = 0.3, seed = 42

Output: {"X_train": [[1, 0], [2, 0], [5, 0], [6, 0], [7, 0], [8, 0], [9, 0]], "X_test": [[3, 0], [4, 0], [10, 0]], "y_train": [0, 0, 0, 0, 0, 1, 1], "y_test": [0, 0, 1]}

Hint 1

Use np.flatnonzero(y == label) and rng.permutation for each class.

Hint 2

Accumulate class indices, then apply np.sort before indexing X and y.

Requirements

  • Shuffle indices within each class using one generator created from seed, visiting classes in ascending label order
  • Assign the first shuffled indices of each class to the test set and the rest to the training set
  • Round each class test count and preserve one training sample when possible
  • Sort the combined train and test indices before indexing
  • Return exactly X_train, X_test, y_train, and y_test in a dictionary of NumPy arrays

Constraints

  • X and y contain the same number of samples
  • y is a nonempty one-dimensional label list
  • test_size is greater than 0 and less than 1
  • Use NumPy only
Try Similar Problems
K-Fold Split (Indices Only)HardImpute Missing Values (mean/median)HardMean Rating ImputationMediumBatch Shuffling & Mini-Batch GeneratorMediumCompute Accuracy, Precision, Recall, F1MediumImplement BM25 Ranking in PythonHardApply a Go Action with Captures and RepetitionHardBuild the Legal Go Action MaskHardApply Custom TransformsHardBoolean MaskingHardUnstack Long to WideHardNorm-Gated Linear TransformHard
Scratchpad

Accepts: array

Accepts: array

Accepts: number

Accepts: number

You must run your code first.
Stratified Train/Test Split

Stratified Train/Test Split

Data Processing
Hard

Split features and labels while approximating the same class proportions in train and test. For each class with ncn_cnc​ samples, compute:

nctest=round⁡(nc t)n_c^{\mathrm{test}} = \operatorname{round}(n_c\,t)nctest​=round(nc​t)

Here, ttt is test_size. When a class has more than one sample, cap its test count at nc−1n_c-1nc​−1 so at least one remains in training. Create one generator with np.random.default_rng(seed) and reuse it for every class, visiting the classes in ascending label order. Shuffle the indices of each class and send the first nctestn_c^{\mathrm{test}}nctest​ shuffled indices to the test set and the remaining ones to the training set. Combine the selected indices, sort each final index set, and return X_train, X_test, y_train, and y_test as NumPy arrays in a dictionary.

Loading visualization...

Examples

Input: X = [0, 1, 2, 3, 4, 5], y = [0, 0, 0, 1, 1, 1], test_size = 0.33, seed = 42

Output: {"X_train": [0, 1, 4, 5], "X_test": [2, 3], "y_train": [0, 0, 1, 1], "y_test": [0, 1]}

Explanation: One seeded sample from each class enters the test split.

Input: X = [[1, 0], [2, 0], [3, 0], [4, 0], [5, 0], [6, 0], [7, 0], [8, 0], [9, 0], [10, 0]], y = [0, 0, 0, 0, 0, 0, 0, 1, 1, 1], test_size = 0.3, seed = 42

Output: {"X_train": [[1, 0], [2, 0], [5, 0], [6, 0], [7, 0], [8, 0], [9, 0]], "X_test": [[3, 0], [4, 0], [10, 0]], "y_train": [0, 0, 0, 0, 0, 1, 1], "y_test": [0, 0, 1]}

Hint 1

Use np.flatnonzero(y == label) and rng.permutation for each class.

Hint 2

Accumulate class indices, then apply np.sort before indexing X and y.

Requirements

  • Shuffle indices within each class using one generator created from seed, visiting classes in ascending label order
  • Assign the first shuffled indices of each class to the test set and the rest to the training set
  • Round each class test count and preserve one training sample when possible
  • Sort the combined train and test indices before indexing
  • Return exactly X_train, X_test, y_train, and y_test in a dictionary of NumPy arrays

Constraints

  • X and y contain the same number of samples
  • y is a nonempty one-dimensional label list
  • test_size is greater than 0 and less than 1
  • Use NumPy only
Try Similar Problems
K-Fold Split (Indices Only)HardImpute Missing Values (mean/median)HardMean Rating ImputationMediumBatch Shuffling & Mini-Batch GeneratorMediumCompute Accuracy, Precision, Recall, F1MediumImplement BM25 Ranking in PythonHardApply a Go Action with Captures and RepetitionHardBuild the Legal Go Action MaskHardApply Custom TransformsHardBoolean MaskingHardUnstack Long to WideHardNorm-Gated Linear TransformHard
Scratchpad

Accepts: array

Accepts: array

Accepts: number

Accepts: number

You must run your code first.
Problems
Loading...
1 / 1