Particionamiento con StratifedKFold#
Ultima modificación: 2023-02-27 | `YouTube <>`__
[1]:
from sklearn.model_selection import StratifiedKFold
stratifiedKFold = StratifiedKFold(
# --------------------------------------------------------------------------
# Número de grupos
n_splits=4,
# --------------------------------------------------------------------------
# Mezcla los datos antes de crear los grupos?
shuffle=False,
# --------------------------------------------------------------------------
# Semilla del generador aleatorio
random_state=None,
)
stratifiedKFold
[1]:
StratifiedKFold(n_splits=4, random_state=None, shuffle=False)
[2]:
from mymodule import plot_schema
y_classes = [0] * 16 + [1] * 4
plot_schema(stratifiedKFold, y_classes)
[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])
stratifiedKFold = StratifiedKFold(
n_splits=3,
)
for i, (train_index, test_index) in enumerate(stratifiedKFold.split(X, y)):
print(f"Fold {i}:")
print(f" Train: index={train_index}")
print(f" Test: index={test_index}")
print()
Fold 0:
Train: index=[1 2 4 5]
Test: index=[0 3]
Fold 1:
Train: index=[0 2 3 5]
Test: index=[1 4]
Fold 2:
Train: index=[0 1 3 4]
Test: index=[2 5]