Sklearn
StratifiedKFold
ValueError
target types
multilabel-indicator

Sklearn StratifiedKFold ValueError Supported target types are 'binary', 'multiclass'. Got 'multilabel-indicator' instead

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

Introduction

The StratifiedKFold class in Scikit-learn is a popular cross-validator used for dividing datasets into stratified folds, maintaining the percentage of samples for each class within each fold. When beginning with machine learning projects, engineers often utilize StratifiedKFold due to its efficiency in ensuring class distribution is similar in both training and testing dataset splits. However, users occasionally encounter a ValueError stating: "Supported target types are: ('binary', 'multiclass'). Got 'multilabel-indicator' instead." Understanding the root of this error and how to handle it is crucial for effectively utilizing this tool.

StratifiedKFold and Supported Target Types

What is StratifiedKFold?

The StratifiedKFold is a variation of K-Fold cross-validation in Scikit-learn. Unlike the basic KFold cross-validation, StratifiedKFold ensures each fold is representative of all classes, preserving the original class distribution ratio. This feature is essential, especially in imbalanced datasets, to prevent biased training and testing sets.

Supported Target Types

StratifiedKFold supports stratification for:

  • Binary classification: Where there are exactly two classes (e.g., positive vs. negative).
  • Multiclass classification: Where there are more than two classes, such as classifying types of animals (dogs, cats, horses, etc.).

The ValueError: "Supported target types are: ('binary', 'multiclass'). Got 'multilabel-indicator' instead."

What Does the Error Mean?

ValueError generally indicates a mismatch in the expected input format and the format provided. In this context, the error arises because StratifiedKFold expects single-label class outputs (either binary or multiclass), but the input is a multilabel-indicator matrix.

Multilabel vs. Multiclass

  • Multiclass classification deals with one label per instance but allows for more than two classes.
  • Multilabel classification, on the other hand, involves assigning multiple labels to each instance. It's akin to having multiple binary sub-problems.

Consider the problem of tagging a movie with multiple genres. A movie could simultaneously belong to "Adventure", "Fantasy", and "Thriller". This would qualify as a multilabel classification problem.

Handling the Multilabel-Indicator Error

Transforming the Problem Suitable for StratifiedKFold

To overcome the ValueError, here are a few approaches:

  • Label Powerset: This method transforms the multilabel problem into a multiclass problem by treating each unique combination of labels as a separate class. However, this could lead to a large number of unique classes.
  • Using KFold or MultilabelStratifiedKFold: Instead of StratifiedKFold, you might choose a different cross-validation strategy. The KFold approach does not consider class stratification, but if your dataset is balanced, it might serve well. Alternatively, libraries like iterative-stratification provide MultilabelStratifiedKFold, designed for maintaining label proportions in multilabel datasets.

Example with Multilabel Data

python
1from sklearn.model_selection import KFold
2import numpy as np
3
4# Suppose you have a multilabel target:
5y_multilabel = np.array([[1, 1, 0],
6                         [0, 1, 1],
7                         [1, 0, 1],
8                         [0, 0, 1]])
9
10# Using KFold
11kf = KFold(n_splits=2, shuffle=True, random_state=42)
12for train_index, test_index in kf.split(y_multilabel):
13    print("TRAIN:", train_index, "TEST:", test_index)

This code snippet demonstrates using KFold, which can handle the multilabel matrix without causing a ValueError since it doesn't require stratification.

Key Differences and Considerations

Below is a summary table of considerations when choosing between StratifiedKFold, KFold, and handling multilabel data:

AspectStratifiedKFoldKFoldMultilabel Consideration
Handling of classesPreserves class ratio across foldsNo guarantee on class ratioRequires a different strategy
Supported target typesBinary, MulticlassAnyMultilabel strategies like Label Powerset
Error EncounteredWith multilabel-indicatorNonePotential for high class cardinality
Use Case SuitabilityImbalanced datasets with stratified class distribution needsBalanced datasetsSituations with multiple labels per instance

Conclusion

The ValueError encountered with StratifiedKFold and multilabel-indicator targets arises from the stratification requirements of StratifiedKFold, which expects single-label classes. By understanding this limitation and applying alternative methods, like KFold or specialized utilities, you can efficiently conduct cross-validation on multilabel datasets. Adapting the approach according to the dataset requirements is key, ensuring the evaluation method aligns effectively with the nature of your classification problem.


Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Basis Lab
No course covers this one yet

Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.

Build my course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design