Example #1
0
def test_skl_converter_iforest(tmpdir, toolchain):  # pylint: disable=W0212
    # pylint: disable=too-many-locals
    """Convert scikit-learn regressor"""
    X = load_boston(return_X_y=True)[0]
    clf = IsolationForest(max_samples=64, random_state=0, n_estimators=10)
    clf.fit(X)
    expected_pred = clf._compute_chunked_score_samples(X)  # pylint: disable=W0212

    model = treelite.sklearn.import_model(clf)
    assert model.num_feature == clf.n_features_
    assert model.num_class == 1
    assert model.num_tree == clf.n_estimators

    dtrain = treelite_runtime.DMatrix(X, dtype='float32')
    annotation_path = os.path.join(tmpdir, 'annotation.json')
    annotator = treelite.Annotator()
    annotator.annotate_branch(model=model, dmat=dtrain, verbose=True)
    annotator.save(path=annotation_path)

    libpath = os.path.join(tmpdir, 'skl' + _libext())
    model.export_lib(toolchain=toolchain,
                     libpath=libpath,
                     params={'annotate_in': annotation_path},
                     verbose=True)
    predictor = treelite_runtime.Predictor(libpath=libpath)
    assert predictor.num_feature == clf.n_features_
    assert model.num_class == 1
    assert predictor.pred_transform == 'exponential_standard_ratio'
    assert predictor.global_bias == 0.0
    assert predictor.sigmoid_alpha == 1.0
    assert predictor.ratio_c > 0
    assert predictor.ratio_c < 64
    out_pred = predictor.predict(dtrain)
    np.testing.assert_almost_equal(out_pred, expected_pred, decimal=2)
Example #2
0
def test_skl_converter_iforest():
    """Scikit-learn isolation forest"""
    X, _ = load_boston(return_X_y=True)
    clf = IsolationForest(max_samples=64, random_state=0, n_estimators=10)
    clf.fit(X)
    expected_pred = clf._compute_chunked_score_samples(X)  # pylint: disable=W0212

    tl_model = treelite.sklearn.import_model(clf)
    out_pred = treelite.gtil.predict(tl_model, X)
    np.testing.assert_almost_equal(out_pred, expected_pred, decimal=2)