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

远程服务器无法安装scikit-learn==1.2.2及脚本报错的原因与解决

apolloadmin@apollo-nginxlb-scale-vm:/home/reportsdeploy/Automation/Jenkins/python_scripts/Sruthi$
python model_v4_deploy.py
/home/apolloadmin/.local/lib/python3.6/site-packages/sklearn/base.py:315:
UserWarning: Trying to unpickle estimator ExtraTreeRegressor from
version 1.2.2 when using version 0.24.2. This might lead to breaking
code or invalid results. Use at your own risk.   UserWarning)
/home/apolloadmin/.local/lib/python3.6/site-packages/sklearn/base.py:315:
UserWarning: Trying to unpickle estimator IsolationForest from version
1.2.2 when using version 0.24.2. This might lead to breaking code or invalid results. Use at your own risk.   UserWarning)
/home/apolloadmin/.local/lib/python3.6/site-packages/sklearn/base.py:315:
UserWarning: Trying to unpickle estimator StandardScaler from version
1.2.2 when using version 0.24.2. This might lead to breaking code or invalid results. Use at your own risk.   UserWarning) Traceback (most
recent call last):   File "model_v4_deploy.py", line 242, in 
users_today['pred']=loaded_model.predict(loaded_scaler.transform(users_today.drop('mobilenumber',axis=1).fillna(0)))
File
"/home/apolloadmin/.local/lib/python3.6/site-packages/sklearn/ensemble/_iforest.py",
line 314, in predict
is_inlier[self.decision_function(X) < 0] = -1   File "/home/apolloadmin/.local/lib/python3.6/site-packages/sklearn/ensemble/_iforest.py",
line 347, in decision_function
return self.score_samples(X) - self.offset_   File "/home/apolloadmin/.local/lib/python3.6/site-packages/sklearn/ensemble/_iforest.py",
line 379, in score_samples
if self.n_features_ != X.shape[1]: AttributeError: 'IsolationForest' object has no attribute 'n_features_'

apolloadmin@apollo-nginxlb-scale-vm:/home/reportsdeploy/Automation/Jenkins/python_scripts/Sruthi$
pip install sklearn Defaulting to user installation because normal
site-packages is not writeable Requirement already satisfied: sklearn
in /home/apolloadmin/.local/lib/python3.6/site-packages (0.0.post1)

apolloadmin@apollo-nginxlb-scale-vm:/home/reportsdeploy/Automation/Jenkins/python_scripts/Sruthi$
pip install scikit-learn 1.2.2 Defaulting to user installation because
normal site-packages is not writeable Requirement already satisfied:
scikit-learn in /home/apolloadmin/.local/lib/python3.6/site-packages
(0.24.2) ERROR: Could not find a version that satisfies the
requirement 1.2.2 (from versions: none) ERROR: No matching
distribution found for 1.2.2

apolloadmin@apollo-nginxlb-scale-vm:/home/reportsdeploy/Automation/Jenkins/python_scripts/Sruthi$
pip install scikit-learn==1.2.2 Defaulting to user installation
because normal site-packages is not writeable ERROR: Could not find a
version that satisfies the requirement scikit-learn==1.2.2 (from
versions: 0.9, 0.10, 0.11, 0.12, 0.12.1, 0.13, 0.13.1, 0.14, 0.14.1,
0.15.0, 0.15.1, 0.15.2, 0.16.0, 0.16.1, 0.17, 0.17.1, 0.18, 0.18.1, 0.18.2, 0.19.0, 0.19.1, 0.19.2, 0.20.0, 0.20.1, 0.20.2, 0.20.3, 0.20.4, 0.21.0, 0.21.1, 0.21.2, 0.21.3, 0.22, 0.22.1, 0.22.2, 0.22.2.post1, 0.23.0, 0.23.1, 0.23.2, 0.24.0, 0.24.1, 0.24.2) ERROR: No matching distribution found for scikit-learn==1.2.2
问题原因与解决方案

一、问题原因

1. 模型版本不兼容

你用scikit-learn 1.2.2训练并保存的模型(ExtraTreeRegressor、IsolationForest、StandardScaler),现在要在安装了0.24.2版本的环境中加载运行。两个版本的scikit-learn内部结构差异大,旧版本无法正确反序列化新版本的模型,直接导致AttributeError: 'IsolationForest' object has no attribute 'n_features_'——因为1.2.2里的n_features_属性在0.24.2中不存在或命名不同。

2. 无法安装1.2.2的核心原因

你的服务器用的是Python 3.6,而scikit-learn 1.2.2不支持Python 3.6。从pip返回的可用版本列表能看到,Python3.6对应的scikit-learn最高版本就是0.24.2,更高版本需要Python3.7及以上。

二、解决方案

方案1:重新生成适配当前环境的模型

如果无法升级Python,需要在本地或其他有Python3.6+scikit-learn0.24.2的环境中,重新训练并保存模型,替换服务器上的旧模型。步骤:

  • 安装scikit-learn==0.24.2:pip install scikit-learn==0.24.2
  • 用相同的代码逻辑重新训练模型、保存
  • 将新模型文件上传到服务器替换原文件

方案2:升级服务器Python版本(推荐)

升级到Python3.7及以上版本(比如3.8或3.9,兼容性更稳定),再安装scikit-learn1.2.2:

  1. 先安装对应版本的Python(注意不要覆盖系统默认Python,建议用pyenv或虚拟环境)
  2. 创建并激活虚拟环境:
    python3.8 -m venv myenv
    source myenv/bin/activate
    
  3. 安装指定版本scikit-learn:pip install scikit-learn==1.2.2
  4. 在该虚拟环境中运行脚本:python model_v4_deploy.py

方案3:临时兼容处理(不推荐)

如果只是临时应急,可以尝试修改模型加载后的属性,但风险极高,容易出现不可预测的结果:

  • 在加载模型后手动添加n_features_属性,值设置为输入数据的特征数量:
    loaded_model.n_features_ = users_today.drop('mobilenumber',axis=1).fillna(0).shape[1]
    
    但这种方法只能解决当前报错,后续可能出现更多版本兼容问题,不建议长期使用。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.18 04:02:02