-
|
I trained an XGB Regressor in Python and saved it as following: Predictions:
I get the following:
This gives me the following output:
Python XGBoost version: 3.1.0 I also tried saving the models as |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 25 replies
-
|
If the difference is significant, then the tree is taking different branches. I can't provide a definite answer without reproducing it myself. My guess is that the Python code uses f64 by default, and is converted to f32 inside XGBoost. During the text -> binary or during the f64 -> f32, the data is slightly changed due to floating-point parsing or conversion. |
Beta Was this translation helpful? Give feedback.
-
|
To illustrate, using the first number in your input: >>> from decimal import Decimal
>>> a = -3.17492
>>> Decimal(a)
Decimal('-3.1749200000000001864464138634502887725830078125') |
Beta Was this translation helpful? Give feedback.
-
|
I couldn't reproduce the issue using the "P.json" you provided, tested with both 3.1 branch and the development branch. #include <xgboost/c_api.h>
#include <cmath>
#include <iostream>
#include <vector>
int main() {
BoosterHandle booster;
XGBoosterCreate(nullptr, 0, &booster);
if (XGBoosterLoadModel(booster, "P.json") != 0) {
std::cerr << "Error loading model: " << XGBGetLastError() << std::endl;
return 1;
}
std::vector<float> input = {-3.17492f, 17.9437f, 34.5295f, 3.3425f};
DMatrixHandle dmat;
XGDMatrixCreateFromMat(input.data(), 1, input.size(), NAN, &dmat);
const char *json_config = R"({
"type": 0,
"training": false,
"iteration_begin": 0,
"iteration_end": 0,
"strict_shape": true
})";
const bst_ulong *out_shape;
bst_ulong out_dim;
const float *out_result;
if (XGBoosterPredictFromDMatrix(booster, dmat, json_config,
&out_shape, &out_dim, &out_result) != 0) {
std::cerr << "Error predicting: " << XGBGetLastError() << std::endl;
return 1;
}
std::cout << "Prediction: " << out_result[0] << std::endl;
XGDMatrixFree(dmat);
XGBoosterFree(booster);
return 0;
}
import numpy as np
import xgboost as xgb
booster = xgb.Booster()
booster.load_model("P.json")
point = [-3.17492, 17.9437, 34.5295, 3.3425]
dtest = xgb.DMatrix(np.array([point]))
pred = booster.predict(dtest, strict_shape=True)
print(f"Prediction: {pred[0]}") |
Beta Was this translation helpful? Give feedback.
-
|
If you can't reproduce it using the scripts above, then we can narrow the issue down to something more specific:
|
Beta Was this translation helpful? Give feedback.
Alright, It definitely had to do something with the older version. I downloaded the latest (
XGBoost version: 3.3.0), did not touch a single thing in the old code and the problem is resolved! I am getting the correct predictions now. Thank you for your time though.