Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add HDFS save/load model support in XGBoost and LightGBM. #8744

Open
lalalapotter opened this issue Aug 11, 2023 · 0 comments
Open

Add HDFS save/load model support in XGBoost and LightGBM. #8744

lalalapotter opened this issue Aug 11, 2023 · 0 comments
Labels

Comments

@lalalapotter
Copy link
Contributor

lalalapotter commented Aug 11, 2023

Per customers requirement, we need to add support for saving/loading model on HDFS in XGBoost, and LightGBM.

Current workaround:

def save_model(model, path):
    if is_local_path(path):
        model.saveModel(path)
    else:
        file_name = str(uuid.uuid1())
        file_name = append_suffix(file_name, path)
        temp_path = os.path.join(tempfile.gettempdir(), file_name)
        try:
            model.saveModel(temp_path)
            put_local_file_to_remote(temp_path, path)
        finally:
            os.remove(temp_path)

def load_model(path, class_num):
    model = None
    if is_local_path(path):
        model = XGBClassifierModel.loadModel(path, class_num)
    else:
        file_name = str(uuid.uuid1())
        file_name = append_suffix(file_name, path)
        temp_path = os.path.join(tempfile.gettempdir(), file_name)
        get_remote_file_to_local(path, temp_path)
        try:
            model = XGBClassifierModel.loadModel(temp_path, class_num)
        finally:
            os.remove(temp_path)
    return model

eta = 0.1
max_depth = 6
num_round =200
params.update({"eta": eta, "max_depth": max_depth, "num_round": num_round})
classifier = XGBClassifier(params)
xgbmodel = classifier.fit(train)
save_model(xgbmodel, "hdfs:///user/kai/zcg/model/xgb.model")
xgbmodel = load_model("hdfs:///user/kai/zcg/model/xgb.model", 2)
xgbmodel.setFeaturesCol("features")
predicts = xgbmodel.transform(test).drop("features")
predicts.cache()
predicts.show(5, False)
predicts.unpersist(blocking=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant