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

前端 未结 4 2154
攒了一身酷
攒了一身酷 2020-12-17 08:36

Working with Sklearn stratified kfold split, and when I attempt to split using multi-class, I received on error (see below). When I tried and split using binary, it works n

4条回答
  •  心在旅途
    2020-12-17 09:08

    I bumped into the same problem and found out that you can check the type of the target with this util function:

    from sklearn.utils.multiclass import type_of_target
    type_of_target(y)
    
    'multilabel-indicator'
    

    From its docstring:

    • 'binary': y contains <= 2 discrete values and is 1d or a column vector.
    • 'multiclass': y contains more than two discrete values, is not a sequence of sequences, and is 1d or a column vector.
    • 'multiclass-multioutput': y is a 2d array that contains more than two discrete values, is not a sequence of sequences, and both dimensions are of size > 1.
    • 'multilabel-indicator': y is a label indicator matrix, an array of two dimensions with at least two columns, and at most 2 unique values.

    With LabelEncoder you can transform your classes into an 1d array of numbers (given your target labels are in an 1d array of categoricals/object):

    from sklearn.preprocessing import LabelEncoder
    
    label_encoder = LabelEncoder()
    y = label_encoder.fit_transform(target_labels)
    

提交回复
热议问题