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

从sklearn导入DecisionTreeClassifier时出现AttributeError错误求助

解决导入DecisionTreeClassifier时的AttributeError: module 'numpy' has no attribute 'float'错误

你的代码:

import sklearn
print(sklearn.__version__)
from sklearn.tree import DecisionTreeClassifier

运行后输出:

0.23.2

AttributeError: module 'numpy' has no attribute 'float'.
`np.float` was a deprecated alias for the builtin `float`. To avoid this error in existing code, use `float` by itself. Doing this will not modify any behavior and is safe. If you specifically wanted the numpy scalar type, use `np.float64` here.
The aliases was originally deprecated in NumPy 1.20; for more details and guidance see the original release note at:
    https://numpy.org/devdocs/release/1.20.0-notes.html#deprecations

问题原因

你使用的scikit-learn版本(0.23.2)过于老旧,与1.20及以上版本的numpy不兼容。老版本scikit-learn内部仍在调用numpy已经弃用并移除的np.float别名,从而触发该错误。

解决办法

  • 推荐方案:升级scikit-learn
    执行以下命令升级到兼容新版numpy的版本:

    pip install --upgrade scikit-learn
    

    建议升级到0.24.2及以上版本,该版本开始修复了与numpy 1.20的兼容性问题。

  • 备选方案:降级numpy(不推荐)
    若暂时无法升级scikit-learn,可将numpy降级到1.19.x版本:

    pip install numpy==1.19.5
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.29 21:37:04