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

如何解决AttributeError: 'OneHotEncoder'无_infrequent_enabled属性问题

解决OneHotEncoder的AttributeError: '_infrequent_enabled'缺失问题

问题说明

运行Streamlit笔记本价格预测应用时触发以下错误:

AttributeError: 'OneHotEncoder' object has no attribute '_infrequent_enabled'

完整回溯信息:

Traceback:
File "C:\Users\spand\AppData\Local\Programs\Python\Python310\lib\site-packages\streamlit\runtime\scriptrunner\script_runner.py", line 565, in _run_script
exec(code, module.__dict__)
File "C:\Users\spand\Desktop\laptop price prediction\app.py", line 68, in 
st.title(int(np.exp(pipe.predict(query))))
File "C:\Users\spand\AppData\Local\Programs\Python\Python310\lib\site-packages\sklearn\pipeline.py", line 457, in predict
Xt = transform.transform(Xt)
File "C:\Users\spand\AppData\Local\Programs\Python\Python310\lib\site-packages\sklearn\compose\_column_transformer.py", line 763, in transform
Xs = self._fit_transform(
File "C:\Users\spand\AppData\Local\Programs\Python\Python310\lib\site-packages\sklearn\compose\_column_transformer.py", line 621, in _fit_transform
return Parallel(n_jobs=self.n_jobs)(
File "C:\Users\spand\AppData\Local\Programs\Python\Python310\lib\site-packages\joblib\parallel.py", line 1085, in __call__
if self.dispatch_one_batch(iterator):
File "C:\Users\spand\AppData\Local\Programs\Python\Python310\lib\site-packages\joblib\parallel.py", line 901, in dispatch_one_batch
self._dispatch(tasks)
File "C:\Users\spand\AppData\Local\Programs\Python\Python310\lib\site-packages\joblib\parallel.py", line 819, in _dispatch
job = self._backend.apply_async(batch, callback=cb)
File "C:\Users\spand\AppData\Local\Programs\Python\Python310\lib\site-packages\joblib\_parallel_backends.py", line 208, in apply_async
result = ImmediateResult(func)
File "C:\Users\spand\AppData\Local\Programs\Python\Python310\lib\site-packages\joblib\_parallel_backends.py", line 597, in __init__
self.results = batch()
File "C:\Users\spand\AppData\Local\Programs\Python\Python310\lib\site-packages\joblib\parallel.py", line 288, in __call__
return [func(*args, **kwargs)
File "C:\Users\spand\AppData\Local\Programs\Python\Python310\lib\site-packages\joblib\parallel.py", line 288, in 
return [func(*args, **kwargs)
File "C:\Users\spand\AppData\Local\Programs\Python\Python310\lib\site-packages\sklearn\utils\fixes.py", line 117, in __call__
return self.function(*args, **kwargs)
File "C:\Users\spand\AppData\Local\Programs\Python\Python310\lib\site-packages\sklearn\pipeline.py", line 853, in _transform_one
res = transformer.transform(X)
File "C:\Users\spand\AppData\Local\Programs\Python\Python310\lib\site-packages\sklearn\preprocessing\_encoders.py", line 888, in transform
self._map_infrequent_categories(X_int, X_mask)
File "C:\Users\spand\AppData\Local\Programs\Python\Python310\lib\site-packages\sklearn\preprocessing\_encoders.py", line 726, in _map_infrequent_categories
if not self._infrequent_enabled:

问题根源

这个错误本质是scikit-learn版本不兼容:

  • _infrequent_enabled是scikit-learn 1.0+版本中为OneHotEncoder新增的属性,用于支持低频类别处理(比如min_frequency、max_categories参数)。
  • 如果预测模型是用旧版sklearn训练保存的,现在用新版sklearn加载运行;或者模型用新版训练,运行环境是旧版sklearn,都会出现属性不匹配的问题。

解决方案

  • 统一scikit-learn版本

    1. 找到训练模型时使用的sklearn版本:在训练模型的Python环境中执行pip show scikit-learn,记录版本号(比如1.2.2)。
    2. 在Streamlit应用的运行环境中安装相同版本:执行命令pip install scikit-learn==x.x.x(将x.x.x替换为记录的版本号)。
  • 重新训练并保存模型
    如果允许重新训练,直接在当前Streamlit环境的sklearn版本下,重新训练模型并保存,确保模型文件与运行环境完全兼容。

  • 检查OneHotEncoder参数
    若训练时使用了handle_unknown='infrequent_if_exist'、min_frequency或max_categories这类低频类别处理参数,需确认运行环境的sklearn版本支持这些参数——旧版sklearn(低于1.0)不支持这些参数,会导致模型加载时属性缺失。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.02 13:21:24