我正在尝试模仿 Ahmed Besbes的机器学习程序,但已针对多标签分类进行了扩展。似乎任何对数据进行分层的尝试都会返回以下错误:The least populated class in y has only 1 member, which is too few. The minimum number of labels for any class cannot be less than 2.
在我的数据集中,我有 1 列包含干净的标记化文本。其他 8 列用于基于该文本内容的分类。请注意,第 1 - 4 列的样本明显多于 5 - 8 列(来自文本的更模糊的分类)。
这是我的代码中的通用示例:
x = data['cleaned_text']
y = data[['car','truck','ford','chevy','black','white','parked', 'driving']]
x_train, x_test, y_train, y_test = train_test_split(x,
y,
test_size=0.1,
random_state=42)
print(x_train.shape, x_test.shape, y_train.shape, y_test.shape)
输出: (6293,) (700,) (6293, 8) (700, 8)
添加stratify=y
到train_test_split
返回前面提到的错误。即使我将 y 限制为一列,我仍然会收到错误消息。
如何对数据进行分层,以便让程序在训练集中有一个公平的外观?