Particionamiento con StratifiedShuffleSplit#

[1]:
from sklearn.model_selection import StratifiedShuffleSplit

stratifiedShuffleSplit = StratifiedShuffleSplit(
    # --------------------------------------------------------------------------
    # Número de grupos
    n_splits=8,
    # --------------------------------------------------------------------------
    # Tamaño del conjunto de prueba
    # int: número de ejemplos
    # float: porcentaje de la muestra
    test_size=4,
    # --------------------------------------------------------------------------
    # Tamaño del conjunto de entrenamiento
    # int: número de ejemplos
    # float: porcentaje de la muestra
    train_size=None,
    # --------------------------------------------------------------------------
    # Semilla del generador de aleatorios
    random_state=0,
)

stratifiedShuffleSplit
[1]:
StratifiedShuffleSplit(n_splits=8, random_state=0, test_size=4,
            train_size=None)
[2]:
from mymodule import plot_schema

y_classes = [0] * 16 + [1] * 4

plot_schema(stratifiedShuffleSplit, y_classes)
../_images/05_iteradores_07_StratifiedShuffleSplit_2_0.png
[3]:
import numpy as np

X = np.array([[1, 2], [3, 4], [5, 6], [7, 8], [9, 10], [11, 12]])
y = np.array([0, 0, 0, 1, 1, 1])

stratifiedShuffleSplit = StratifiedShuffleSplit(
    n_splits=8,
    test_size=3,
)

for i, (train_index, test_index) in enumerate(stratifiedShuffleSplit.split(X, y)):
    print(f"Fold {i}:")
    print(f"  Train: index={train_index}")
    print(f"  Test:  index={test_index}")
    print()
Fold 0:
  Train: index=[0 3 5]
  Test:  index=[1 4 2]

Fold 1:
  Train: index=[2 3 4]
  Test:  index=[1 5 0]

Fold 2:
  Train: index=[4 2 3]
  Test:  index=[1 5 0]

Fold 3:
  Train: index=[4 1 2]
  Test:  index=[0 3 5]

Fold 4:
  Train: index=[5 2 4]
  Test:  index=[1 3 0]

Fold 5:
  Train: index=[4 3 1]
  Test:  index=[5 0 2]

Fold 6:
  Train: index=[2 0 5]
  Test:  index=[1 3 4]

Fold 7:
  Train: index=[2 4 5]
  Test:  index=[1 0 3]