You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

运行kfold.split遇ValueError:目标类型unknown的解决求助

解决kfold.split报错"Supported target types are: ('binary', 'multiclass'). Got 'unknown' instead"

问题场景

运行代码第5行for train, test in kfold.split(X, Y):时触发错误:

ValueError: Supported target types are: ('binary', 'multiclass'). Got 'unknown' instead.

代码中Y是从npy文件加载的一维数组,取值范围0-6,但sklearn.utils.multiclass.type_of_target检测Y的类型为unknown,尝试LabelEncoder处理未解决问题。

可能原因

  • Y的数据类型不是整数(例如float类型),sklearn无法识别为多分类目标
  • Y数组形状不符合一维要求(比如是二维数组)
  • 加载的Y数组存在异常值(如NaN、非数值元素)

解决方案

1. 检查并转换Y的数据类型

先打印Y的类型和结构确认问题:

print(Y.dtype)
print(Y.shape)

如果Y是float类型,转换为整数类型:

Y = Y.astype(int)

2. 确保Y是一维数组

若Y为二维结构(如形状为(n_samples, 1)),压缩为一维:

Y = Y.ravel()

3. 验证目标类型

转换后重新检查目标类型,确认输出为multiclass:

from sklearn.utils.multiclass import type_of_target
print(type_of_target(Y))

4. 排查异常值

若上述操作无效,检查Y数组是否存在异常值:

print(np.unique(Y))
print(np.isnan(Y).any())

若存在NaN或非预期值,需清理对应样本(如删除或替换)。

内容的提问来源于stack exchange,提问作者mahmoud aghvami

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.16 02:15:44