<jupyter_start><jupyter_text>Cross validation with KFoldSplit the train set in k folds and use each fold as a validation set with the other folds as train set.The result is then averaged.Every observations will be in the testing set at least once.<jupyter_code>from sklearn.model_selection import KFold, cross_val_score
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn import datasets
from sklearn import svm
X = np.array([[1, 2], [3, 4], [1, 2], [3, 4]])
y = np.array([1, 2, 3, 4])
for train_index, test_index in KFold(n_splits=2).split(X):
    print("Train:", train_index, "Test:", test_index)
    X_train, X_test = X[train_index], X[test_index]
    y_train, y_test = y[train_index], y[test_index]
for train_index, test_index in KFold(n_splits=4).split(X):
    print("Train:", train_index, "Test:", test_index)
    X_train, X_test = X[train_index], X[test_index]
    y_train, y_test = y[train_index], y[test_index]<jupyter_output>Train: [1 2 3] Test: [0]
Train: [0 2 3] Test: [1]
Train: [0 1 3] Test: [2]
Train: [0 1 2] Test: [3]<jupyter_text>Using K-Fold cross validation with a classifier<jupyter_code>X, y = datasets.load_iris(return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.4, random_state=0)
clf = svm.SVC(kernel='linear', C=1).fit(X_train, y_train)
clf.score(X_test, y_test)
clf = svm.SVC(kernel='linear', C=1)
scores = cross_val_score(clf, X, y, cv=5)
scores
print("Accuracy: %0.2f (+/- %0.2f)" % (scores.mean(), scores.std() * 2))<jupyter_output>Accuracy: 0.98 (+/- 0.03)
{'fraction_non_alphanumeric': 0.14460154241645246, 'fraction_numerical': 0.030205655526992288, 'mean_word_length': 5.653846153846154, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '10745', 'license': 'MIT', 'n_tokens_mistral': 647, 'n_tokens_neox': 610, 'n_words': 169, 'path': 'notebooks/016. cross validation.ipynb', 'repo_name': 'gillouche/exploratory-data-analysis', 'size': '1556'}