Catboost save model python

Saved searches

Use saved searches to filter your results more quickly

You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.

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

How to properly save/load model #2294

How to properly save/load model #2294

Comments

Hi
I am really new to catboost. I would like some clarification about saving/loading model.
The way I save/load model, I did it as in here : https://catboost.ai/en/docs/concepts/python-usages-examples#load-from-file

I saved trained model using .cbm format. The problem is when I a load model back and I want to call model.get_best_iteration() it return None . model.feature_importance_ also returned None . Here is my code

params = < 'iterations' : 10000, 'early_stopping_rounds' : 1000, 'loss_function' : 'Logloss', 'eval_metric' : 'F1', 'task_type' : 'GPU', 'gpu_ram_part' : 0.3, 'train_dir' : 'model/catboost_info', >model = CatBoostClassifier(**params) print('fit the model. ') start = time.time() model.fit( X=train_data, use_best_model=True, eval_set=valid_data, ) train_time = time.time() - start print('done training. ') print('save the model and train time..') model.save_model('model/catboost.cbm') print('load model and train time. ') cat = CatBoostClassifier() cat.load_model('model/catboost.cbm') with open('dump/catboost_train_time.pickle', 'rb') as f: train_time = pickle.load(f) print('finish..') print(f'train time : ') print(f'last iteration: ') 

The text was updated successfully, but these errors were encountered:

Читайте также:  What is code coverage in java

Источник

mlflow.catboost

The mlflow.catboost module provides an API for logging and loading CatBoost models. This module exports CatBoost models with the following flavors:

This is the main flavor that can be loaded back into CatBoost.

Produced for use by generic pyfunc-based deployment tools and batch inference.

mlflow.catboost. get_default_conda_env ( ) [source] Returns

The default Conda environment for MLflow Models produced by calls to save_model() and log_model() .

mlflow.catboost. get_default_pip_requirements ( ) [source] Returns

A list of default pip requirements for MLflow Models produced by this flavor. Calls to save_model() and log_model() produce a pip environment that, at minimum, contains these requirements.

mlflow.catboost. load_model ( model_uri , dst_path = None ) [source]

Load a CatBoost model from a local file or a run.

  • model_uri – The location, in URI format, of the MLflow model. For example:
    • /Users/me/path/to/local/model
    • relative/path/to/local/model
    • s3://my_bucket/path/to/model
    • runs://run-relative/path/to/model

    For more information about supported URI schemes, see Referencing Artifacts.

    mlflow.catboost. log_model ( cb_model , artifact_path , conda_env = None , code_paths = None , registered_model_name = None , signature : mlflow.models.signature.ModelSignature = None , input_example : Union [ pandas.core.frame.DataFrame , numpy.ndarray , dict , list , csr_matrix , csc_matrix , str , bytes ] = None , await_registration_for = 300 , pip_requirements = None , extra_pip_requirements = None , metadata = None , ** kwargs ) [source]

    Log a CatBoost model as an MLflow artifact for the current run.

    • cb_model – CatBoost model (an instance of CatBoost, CatBoostClassifier, CatBoostRanker, or CatBoostRegressor) to be saved.
    • artifact_path – Run-relative artifact path.
    • conda_env – Either a dictionary representation of a Conda environment or the path to a conda environment yaml file. If provided, this describes the environment this model should be run in. At minimum, it should specify the dependencies contained in get_default_conda_env() . If None , a conda environment with pip requirements inferred by mlflow.models.infer_pip_requirements() is added to the model. If the requirement inference fails, it falls back to using get_default_pip_requirements() . pip requirements from conda_env are written to a pip requirements.txt file and the full conda environment is written to conda.yaml . The following is an example dictionary representation of a conda environment:
     "name": "mlflow-env", "channels": ["conda-forge"], "dependencies": [ "python=3.8.15",  "pip": [ "catboost==x.y.z" ], >, ], > 
    from mlflow.models import infer_signature train = df.drop_column("target_label") predictions = . # compute model predictions signature = infer_signature(train, predictions) 

    This example demonstrates how to specify pip requirements using pip_requirements and extra_pip_requirements .

    A ModelInfo instance that contains the metadata of the logged model.

    mlflow.catboost. save_model ( cb_model , path , conda_env = None , code_paths = None , mlflow_model = None , signature : mlflow.models.signature.ModelSignature = None , input_example : Union [ pandas.core.frame.DataFrame , numpy.ndarray , dict , list , csr_matrix , csc_matrix , str , bytes ] = None , pip_requirements = None , extra_pip_requirements = None , metadata = None , ** kwargs ) [source]

    Save a CatBoost model to a path on the local file system.

    • cb_model – CatBoost model (an instance of CatBoost, CatBoostClassifier, CatBoostRanker, or CatBoostRegressor) to be saved.
    • path – Local path where the model is to be saved.
    • conda_env – Either a dictionary representation of a Conda environment or the path to a conda environment yaml file. If provided, this describes the environment this model should be run in. At minimum, it should specify the dependencies contained in get_default_conda_env() . If None , a conda environment with pip requirements inferred by mlflow.models.infer_pip_requirements() is added to the model. If the requirement inference fails, it falls back to using get_default_pip_requirements() . pip requirements from conda_env are written to a pip requirements.txt file and the full conda environment is written to conda.yaml . The following is an example dictionary representation of a conda environment:
     "name": "mlflow-env", "channels": ["conda-forge"], "dependencies": [ "python=3.8.15",  "pip": [ "catboost==x.y.z" ], >, ], > 
    from mlflow.models import infer_signature train = df.drop_column("target_label") predictions = . # compute model predictions signature = infer_signature(train, predictions) 

    This example demonstrates how to specify pip requirements using pip_requirements and extra_pip_requirements .

    © MLflow Project, a Series of LF Projects, LLC. All rights reserved.

    Источник

Оцените статью