从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
相关产品推荐
相关产品推荐

