我正在尝试从一个pickle文件加载一个序列化的xgboost模型。
import pickle
def load_pkl(fname):
with open(fname, 'rb') as f:
obj = pickle.load(f)
return obj
model = load_pkl('model_0_unrestricted.pkl')
打印模型对象时,我在linux中遇到以下错误(AWS Sagemaker Notebook)
~/anaconda3/envs/python3/lib/python3.6/site-packages/xgboost/sklearn.py in get_params(self, deep)
436 if k == 'type' and type(self).__name__ != v:
437 msg = 'Current model type: {}, '.format(type(self).__name__) + \
--> 438 'type of model in file: {}'.format(v)
439 raise TypeError(msg)
440 if k == 'type':
~/anaconda3/envs/python3/lib/python3.6/site-packages/sklearn/base.py in get_params(self, deep)
193 out = dict()
194 for key in self._get_param_names():
--> 195 value = getattr(self, key)
196 if deep and hasattr(value, 'get_params'):
197 deep_items = value.get_params().items()
AttributeError: 'XGBClassifier' object has no attribute 'use_label_encoder'
你能帮我解决这个问题吗?
它在我本地的mac上运行得很好。
Ref: xgboost:1.4.1安装日志(Mac)
Collecting xgboost
Downloading xgboost-1.4.1-py3-none-macosx_10_14_x86_64.macosx_10_15_x86_64.macosx_11_0_x86_64.whl (1.2 MB)
但不在AWS上工作
参考: xgboost:1.4.1安装日志(SM笔记本,linux机器)
Collecting xgboost
Using cached xgboost-1.4.1-py3-none-manylinux2010_x86_64.whl (166.7 MB)
谢谢
发布于 2021-04-30 13:02:29
我怀疑您正在加载的酸洗模型在保存之前以某种方式进行了修改,以具有该附加方法。或者正如@vbhatt所说,在加载分类器之前,您可能需要修改分类器的某些方面。当我在Pytorch Lightning中使用自定义模型时,这已经发生在我身上了。
如果你根本没有修改过基本模型,请确保你也在笔记本中使用相同的版本,笔记本中的venv会有不同的版本吗?
发布于 2021-06-17 22:46:31
看起来你升级了xgboost。您可以考虑通过以下方式降级到1.2.0:
pip install xgboost==1.2.0
发布于 2021-04-30 12:33:55
我试着在运行在ubuntu上的笔记本上测试,它似乎工作得很好,但是你能检查一下你是如何初始化分类器的吗?这是我尝试过的:
import numpy as np
import pickle
from scipy.stats import uniform, randint
from sklearn.datasets import load_breast_cancer, load_diabetes, load_wine
from sklearn.metrics import auc, accuracy_score, confusion_matrix, mean_squared_error
from sklearn.model_selection import cross_val_score, GridSearchCV, KFold,RandomizedSearchCV, train_test_split
import xgboost as xgb
cancer = load_breast_cancer()
X = cancer.data
y = cancer.target
xgb_model = xgb.XGBClassifier(objective="binary:logistic", random_state=45)
xgb_model.fit(X, y)
pickle.dump(xgb_model, open("xgb_model.pkl", "wb"))
使用您的函数重新加载模型并输出:
def load_pkl(fname):
with open(fname, 'rb') as f:
obj = pickle.load(f)
return obj
model = load_pkl('xgb_model.pkl')
model
以下是输出:
XGBClassifier(base_score=0.5, booster='gbtree', colsample_bylevel=1,
colsample_bynode=1, colsample_bytree=1, gamma=0, gpu_id=-1,
importance_type='gain', interaction_constraints='',
learning_rate=0.300000012, max_delta_step=0, max_depth=6,
min_child_weight=1, missing=nan, monotone_constraints='()',
n_estimators=100, n_jobs=8, num_parallel_tree=1, random_state=45,
reg_alpha=0, reg_lambda=1, scale_pos_weight=1, subsample=1,
tree_method='exact', validate_parameters=1, verbosity=None)
https://stackoverflow.com/questions/67327106
复制相似问题