<jupyter_start><jupyter_text>Objective:- learn how to evaluate your model by using k-fold cross validation<jupyter_code>import numpy as np
import pandas as pd
from xgboost import XGBClassifier
from sklearn.preprocessing import LabelEncoder, OneHotEncoder
from sklearn.model_selection import KFold, cross_val_score
# read in data
df = pd.read_csv("../../data/breast-cancer.data", header=None)
# split data into features and label
df_X, y = df.iloc[:, :-1], df.iloc[:, -1].values
# label encode string class values
label_encoder = LabelEncoder()
y = label_encoder.fit_transform(y)
# one-hot encode features
# XGBoost may assume that each input variables have an ordinal relationship
onehot_encoder = OneHotEncoder(sparse=False)
X = onehot_encoder.fit_transform(df_X)
# k-fold cross validation
kfold = KFold(n_splits=5, shuffle=True, random_state=89)
clf = XGBClassifier()
scores = cross_val_score(clf, X, y, cv=kfold, scoring="accuracy")
print(
    "[INFO] The test accuracy of XGBoost is: %.2f%% (+/- %.2f%%)" 
    % (scores.mean() * 100, scores.std() * 100)
)<jupyter_output>[INFO] The test accuracy of XGBoost is: 72.04% (+/- 7.56%)
{'fraction_non_alphanumeric': 0.12180052956751986, 'fraction_numerical': 0.0176522506619594, 'mean_word_length': 5.370786516853933, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '387881', 'license': 'MIT', 'n_tokens_mistral': 402, 'n_tokens_neox': 380, 'n_words': 116, 'path': 'ipython/i_basic/04_k-fold_cv.ipynb', 'repo_name': 'Genpeng/xgboost-examples', 'size': '1133'}