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

scikit-learn的OneHotEncoder使用categorical_features参数报错如何解决

问题原因

本地scikit-learn版本高于0.22版本,categorical_features 参数在该版本及之后已被正式移除,原参考代码使用的是更早版本的scikit-learn,因此触发参数不存在的报错。

修复方案

方案1:替换OneHotEncoder相关代码(改动最小,推荐)

将你报错位置的两行代码:

onehotencoder = OneHotEncoder(categorical_features=[0])  #Converted  scalar output into vector output where the correct class will be 1 and other will be 0
Y= onehotencoder.fit_transform(y)

直接替换为:

onehotencoder = OneHotEncoder(sparse_output=False)
Y = onehotencoder.fit_transform(y)

你已经将标签y处理为仅1列的数组,不需要额外指定要编码的特征列,OneHotEncoder默认会对所有列做独热编码。sparse_output=False参数指定返回numpy数组,和原代码输出格式一致,不影响后续模型训练逻辑。

方案2:降级scikit-learn版本

如果需要完全复现原仓库运行环境,可以将scikit-learn降级到0.21版本:

pip install scikit-learn==0.21.3

旧版本存在已知安全漏洞,仅作为临时验证方案使用,不推荐长期使用。

额外逻辑问题排查

你的代码还存在缩进错误,会导致训练数据只包含最后一类房间的图片:
原代码中遍历all_rooms添加样本的for循环没有嵌套在遍历类别的循环内部,需要调整缩进为:

for item in room_types:
    #Get all the file names
    all_rooms = os.listdir('rooms_dataset' + '/' + item)
    
    #Adding items to the list
    for room in all_rooms:
        rooms.append((item, str('rooms_dataset' + '/' + item) + '/' + room))

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.28 20:24:03