another very common setting for cross-validation is when there are groups in the data that are highly related say you want to build system to recognize emotions from pictures of facesand you collect dataset of pictures of people where each person is captured multiple timesshowing various emotions the goal is to build classifier that can correctly identify emotions of people not in the dataset you could use the default stratified cross-validation to measure the performance of classifier here howeverit is likely that pictures of the same person will be in both the training and the test set it will be much easier for classifier to detect emotions in face that is part of the training setcompared to completely new face to accurately evaluate the generalization to new faceswe must therefore ensure that the training and test sets contain images of different people to achieve thiswe can use groupkfoldwhich takes an array of groups as argument that we can use to indicate which person is in the image the groups array here indicates groups in the data that should not be split when creating the training and test setsand should not be confused with the class label this example of groups in the data is common in medical applicationswhere you might have multiple samples from the same patientbut are interested in generalizing to new patients similarlyin speech recognitionyou might have multiple recordings of the same speaker in your datasetbut are interested in recognizing speech of new speakers the following is an example of using synthetic dataset with grouping given by the groups array the dataset consists of data pointsand for each of the data pointsgroups specifies which group (think patientthe point belongs to the groups specify that there are four groupsand the first three samples belong to the first groupthe next four samples belong to the second groupand so onin[ ]from sklearn model_selection import groupkfold create synthetic dataset xy make_blobs(n_samples= random_state= assume the first three samples belong to the same groupthen the next fouretc groups [ scores cross_val_score(logregxygroupscv=groupkfold(n_splits= )print("cross-validation scores:\ {}format(scores)out[ ]cross-validation scores the samples don' need to be ordered by groupwe just did this for illustration purposes the splits that are calculated based on these labels are visualized in figure - cross-validation