【发布时间】:2022-01-03 21:06:19
【问题描述】:
似乎每次我重新运行用于对相同测试数据进行预测的 Python 脚本(无需重新训练模型)时,我都会收到不同的预测结果。即使在训练前在模型上设置种子参数之后也会发生这种情况。
我训练模型并将其保存在笔记本中,如下所示。
model = xgboost.XGBClassifier(n_estimators=100, max_depth=8, n_jobs=-1, eval_metric='auc', seed=42)
model.fit(X_train, y_train)
model.save_model("../models/xgbclassifier_01.txt")
从那里,我将此模型加载到另一个脚本中并对新的输入数据进行预测
clf = xgb.XGBClassifier()
clf.load_model(path)
state_pred1 = clf.predict(X_test)
# load and predict again to show that results are the same
clf2 = xgb.XGBClassifier()
clf2.load_model(path)
state_pred_2 = clf2.predict(X_test)
state_pred1 的结果等于state_pred2。
问题在于,每当我重新运行测试脚本时,我都不会重新训练模型,但我仍然会收到不同的预测值(state_pred1 和 state_pred2 仍然相等)。
有没有办法确保每次运行脚本时都能收到相同的预测?或者 XGBClassifier 模型的 .predict() 方法中是否存在随机参数,每次重新运行加载 XGB 模型的脚本时都会在预测中引入一些随机性?
【问题讨论】:
-
究竟如何导入 xgboost?请明确包含相关的导入命令。
标签: python scikit-learn xgboost