blocks-transformer/processing.py
Admin User 95034efa4e
All checks were successful
Build and Push Docker Image / test (push) Successful in 2m30s
Build and Push Docker Image / build_and_push (push) Successful in 4m50s
Series M v1 model
2025-06-13 18:01:40 +00:00

39 lines
1.2 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import joblib
import xgboost as xgb
import shap
def processing(processed_dict):
# model_path = r"C:\Users\abinisha\citrus\centurion-dataform-mcard\flowx\model_1\artifacts\xgboost_model_v2.joblib"
model_path = "./xgboost_model.joblib"
model = joblib.load(model_path)
explainer = shap.TreeExplainer(model)
feature_names = model.feature_names
# build and score
X = [processed_dict[f] for f in feature_names]
dmatrix = xgb.DMatrix([X], feature_names=feature_names)
pred = float(model.predict(dmatrix)[0])
# get SHAP values as 1×n_features
shap_matrix = explainer.shap_values(dmatrix)
shap_vals = shap_matrix[0] if getattr(shap_matrix, "ndim", 1) > 1 else shap_matrix
# rank & sort features by descending SHAP impact
ranked = sorted(zip(feature_names, shap_vals),
key=lambda fv: fv[1],
reverse=True)
# build a *list* of pre-sorted reason-codes
shape_reasoncode = [
{
"feature": feat,
"value": processed_dict[feat],
"shap_rank": i+1
}
for i, (feat, _) in enumerate(ranked)
]
return {
"prediction": pred,
"shape_reasoncode": shape_reasoncode
}