Particionamiento con RepeatedKFold#
Ultima modificación: 2023-02-27 | `YouTube <>`__
[1]:
#
# repite el KFold n_repeats veces usando diferentes aleatorios
#
from sklearn.model_selection import RepeatedKFold
repeatedKFold = RepeatedKFold(
# --------------------------------------------------------------------------
# Número de grupos
n_splits=5,
# --------------------------------------------------------------------------
# Número de repeticiones
n_repeats=2,
# --------------------------------------------------------------------------
# Semilla del generador de aleatorios
random_state=123,
)
repeatedKFold
[1]:
RepeatedKFold(n_repeats=2, n_splits=5, random_state=123)
[2]:
from mymodule import plot_schema
y_classes = [0] * 10 + [1] * 10
plot_schema(repeatedKFold, y_classes)
[3]:
repeatedKFold.get_n_splits()
[3]:
10
[4]:
import numpy as np
X = np.array([[1, 2], [3, 4], [5, 6], [7, 8], [9, 10], [11, 12]])
y = np.array([1, 2, 3, 4, 5, 6])
repeatedKFold = RepeatedKFold(n_splits=3, n_repeats=2)
for i, (train_index, test_index) in enumerate(repeatedKFold.split(X)):
print(f"Fold {i}:")
print(f" Train: index={train_index}")
print(f" Test: index={test_index}")
print()
Fold 0:
Train: index=[0 2 3 5]
Test: index=[1 4]
Fold 1:
Train: index=[1 3 4 5]
Test: index=[0 2]
Fold 2:
Train: index=[0 1 2 4]
Test: index=[3 5]
Fold 3:
Train: index=[0 1 3 5]
Test: index=[2 4]
Fold 4:
Train: index=[0 2 4 5]
Test: index=[1 3]
Fold 5:
Train: index=[1 2 3 4]
Test: index=[0 5]