<jupyter_start><jupyter_text>**Корректность проверена на Python 3.6:**+ numpy 1.15.4+ sklearn 0.20.2  Sklearn  sklearn.model_selection документация: http://scikit-learn.org/stable/modules/cross_validation.html<jupyter_code>from sklearn import model_selection, datasets

import numpy as np<jupyter_output><empty_output><jupyter_text>Разовое разбиение данных на обучение и тест с помощью train_test_split<jupyter_code>iris = datasets.load_iris()
train_data, test_data, train_labels, test_labels = model_selection.train_test_split(iris.data, iris.target, 
                                                                                     test_size = 0.3)
#убедимся, что тестовая выборка действительно составляет 0.3 от всех данных
float(len(test_labels))/len(iris.data)
print('Размер обучающей выборки: {} объектов \nРазмер тестовой выборки: {} объектов'.format(len(train_data),
                                                                                            len(test_data)))
print('Обучающая выборка:\n', train_data[:5])
print('\n')
print('Тестовая выборка:\n', test_data[:5])
print('Метки классов на обучающей выборке:\n', train_labels)
print('\n')
print('Метки классов на тестовой выборке:\n', test_labels)<jupyter_output>Метки классов на обучающей выборке:
 [0 1 1 0 2 2 2 2 0 0 0 1 2 2 2 1 2 0 1 2 2 1 0 0 1 1 1 0 2 2 1 0 0 1 2 1 1
 2 0 1 1 0 2 1 1 1 0 1 2 1 1 1 2 0 0 0 1 1 1 2 0 1 2 0 1 2 1 2 2 2 0 1 0 0
 1 0 0 0 1 0 1 0 0 0 1 2 1 2 0 1 0 2 1 1 0 2 1 1 2 2 2 0 0 2 0]


Метки классов на тестовой выборке:
 [1 2 0 1 2 1 0 0 1 0 0 2 0 2 2 0 2 0 1 0 1 2 2 2 2 2 2 2 0 1 1 2 2 0 0 2 2
 1 0 0 2 1 0 2 1]<jupyter_text>Стратегии проведения кросс-валидации<jupyter_code>#сгенерируем короткое подобие датасета, где элементы совпадают с порядковым номером
X = range(0,10)<jupyter_output><empty_output><jupyter_text>KFold<jupyter_code>kf = model_selection.KFold(n_splits = 5)
for train_indices, test_indices in kf.split(X):
    print(train_indices, test_indices)
kf = model_selection.KFold(n_splits = 2, shuffle = True)
for train_indices, test_indices in kf.split(X):
    print(train_indices, test_indices)
kf = model_selection.KFold(n_splits = 2, shuffle = True, random_state = 1)
for train_indices, test_indices in kf.split(X):
    print(train_indices, test_indices)<jupyter_output>[1 3 5 7 8] [0 2 4 6 9]
[0 2 4 6 9] [1 3 5 7 8]<jupyter_text>StratifiedKFold<jupyter_code>y = np.array([0] * 5 + [1] * 5)
print(y)

skf = model_selection.StratifiedKFold(n_splits = 2, shuffle = True, random_state = 0)
for train_indices, test_indices in skf.split(X, y):
    print(train_indices, test_indices)
target = np.array([0, 1] * 5)
print(target)

skf = model_selection.StratifiedKFold(n_splits = 2,shuffle = True)
for train_indices, test_indices in skf.split(X, target):
    print(train_indices, test_indices)<jupyter_output>[0 1 0 1 0 1 0 1 0 1]
[2 5 6 7] [0 1 3 4 8 9]
[0 1 3 4 8 9] [2 5 6 7]<jupyter_text>ShuffleSplit<jupyter_code>ss = model_selection.ShuffleSplit(n_splits = 10, test_size = 0.2)

for train_indices, test_indices in ss.split(X):
    print(train_indices, test_indices)<jupyter_output>[5 4 7 1 0 6 3 9] [8 2]
[0 7 3 1 5 9 2 8] [4 6]
[3 4 0 6 5 8 9 7] [1 2]
[0 2 7 1 6 3 5 9] [8 4]
[4 3 0 2 1 5 8 9] [6 7]
[6 8 7 3 0 5 2 4] [1 9]
[0 7 2 4 6 3 5 8] [1 9]
[0 1 4 2 6 3 8 7] [9 5]
[5 1 8 3 7 2 0 9] [6 4]
[3 0 5 7 2 8 4 6] [9 1]<jupyter_text>StratifiedShuffleSplit<jupyter_code>target = np.array([0] * 5 + [1] * 5)
print(target)

sss = model_selection.StratifiedShuffleSplit(n_splits = 4, test_size = 0.2)
for train_indices, test_indices in sss.split(X, target):
    print(train_indices, test_indices)<jupyter_output>[0 0 0 0 0 1 1 1 1 1]
[8 5 9 2 3 6 1 4] [7 0]
[9 7 2 5 4 0 8 1] [6 3]
[5 9 1 2 3 8 4 7] [6 0]
[5 8 6 2 0 7 3 1] [9 4]<jupyter_text>Leave-One-Out<jupyter_code>loo = model_selection.LeaveOneOut()

for train_indices, test_index in loo.split(X):
    print(train_indices, test_index)<jupyter_output>[1 2 3 4 5 6 7 8 9] [0]
[0 2 3 4 5 6 7 8 9] [1]
[0 1 3 4 5 6 7 8 9] [2]
[0 1 2 4 5 6 7 8 9] [3]
[0 1 2 3 5 6 7 8 9] [4]
[0 1 2 3 4 6 7 8 9] [5]
[0 1 2 3 4 5 7 8 9] [6]
[0 1 2 3 4 5 6 8 9] [7]
[0 1 2 3 4 5 6 7 9] [8]
[0 1 2 3 4 5 6 7 8] [9]
{'fraction_non_alphanumeric': 0.11047070124879924, 'fraction_numerical': 0.1186359269932757, 'mean_word_length': 3.2805755395683454, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 31, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '344840', 'license': 'MIT', 'n_tokens_mistral': 2197, 'n_tokens_neox': 1812, 'n_words': 655, 'path': 'SupervisedLearning/materials/notebooks/2-2.ScikitCrossValidation.ipynb', 'repo_name': 'SmirnovAlexander/MachineLearningDataAnalysis', 'size': '4164'}