已安装numpy-1.24.4与scikit-learn-1.2.2,导入sklearn.model_selection报错原因?
问题描述
我有一段训练测试集划分代码:
from sklearn.model_selection import train_test_split train_df, test_df = train_test_split(new_cleaned_df, test_size=0.05, random_state=42, shuffle=True) train_df.reset_index(drop=True, inplace=True) test_df.reset_index(drop=True, inplace=True) print(train_df.shape) print(test_df.shape)
已安装numpy-1.24.4和scikit-learn-1.2.2,但导入sklearn.model_selection时出现如下错误:
ValueError: numpy.dtype size changed, may indicate binary incompatibility. Expected 96 from C header, got 88 from PyObject
解决方法
这个错误是scikit-learn编译时依赖的numpy版本和当前运行环境的numpy版本不匹配,导致二进制兼容性冲突,按以下步骤修复:
- 完全卸载numpy和scikit-learn:
pip uninstall -y numpy scikit-learn - 清理pip缓存,避免残留旧版本编译文件:
pip cache purge - 按顺序重新安装,先装numpy再装scikit-learn:
pip install numpy==1.24.4 pip install scikit-learn==1.2.2 - 如果使用conda环境,改用conda命令安装更稳妥:
conda uninstall -y numpy scikit-learn conda install numpy=1.24.4 scikit-learn=1.2.2
注意:不要混合使用pip和conda安装科学计算库,极易引发版本兼容问题;操作前确保激活了正确的虚拟环境。
内容的提问来源于stack exchange,提问作者Moh. Aflah Azzaky
相关产品推荐
相关产品推荐

