Exemplo n.º 1
0
def test_export_to_python_after_load():
    train_pool = Pool(TRAIN_FILE, column_description=CD_FILE)
    test_pool = Pool(TEST_FILE, column_description=CD_FILE)
    model = CatBoostClassifier(iterations=40, random_seed=0)
    model.fit(train_pool)
    pred_model = model.predict(test_pool, prediction_type='RawFormulaVal')
    model.save_model(OUTPUT_MODEL_PATH)
    model_loaded = CatBoostClassifier()
    model_loaded.load_model(OUTPUT_MODEL_PATH)
    model_loaded.save_model(OUTPUT_PYTHON_MODEL_PATH,
                            format="python",
                            pool=train_pool)
    pred_model_loaded = model_loaded.predict(test_pool,
                                             prediction_type='RawFormulaVal')
    import sys
    import os.path
    module_dir = os.path.dirname(OUTPUT_PYTHON_MODEL_PATH)
    sys.path.insert(0, module_dir)
    from model import apply_catboost_model as apply_catboost_model_from_python
    pred_python = []
    for test_line in test_pool.get_features():
        float_features, cat_features = _split_features(
            test_line, train_pool.get_cat_feature_indices(),
            test_pool.get_cat_feature_hash_to_string())
        pred_python.append(
            apply_catboost_model_from_python(float_features, cat_features))
    assert _check_data(pred_model, pred_python)
    assert _check_data(pred_model_loaded, pred_python)
Exemplo n.º 2
0
def test_export_model_with_cat_features_to_python_from_app():
    test_pool = Pool(TEST_FILE, column_description=CD_FILE)
    model = CatBoost()
    with open(OUTPUT_MODEL_PATH, "w") as model_file:
        model_file.write(resource.find("cb_adult_model_bin"))
    model.load_model(OUTPUT_MODEL_PATH)
    pred_model = model.predict(test_pool, prediction_type='RawFormulaVal')
    from adult_model import apply_catboost_model as apply_catboost_model_from_app
    pred_python = []
    for test_line in test_pool.get_features():
        float_features, cat_features = _split_features(
            test_line, test_pool.get_cat_feature_indices(),
            test_pool.get_cat_feature_hash_to_string())
        pred_python.append(
            apply_catboost_model_from_app(float_features, cat_features))
    assert _check_data(pred_model, pred_python)