### Install Treelite using pip or conda Source: https://context7.com/dmlc/treelite/llms.txt Install Treelite using pip or conda for quick setup. ```bash # Install from PyPI pip install treelite ``` ```bash # Install from Conda conda install -c conda-forge treelite ``` -------------------------------- ### Install Package Configuration Files Source: https://github.com/dmlc/treelite/blob/mainline/CMakeLists.txt Installs the generated `TreeliteConfig.cmake` and `TreeliteConfigVersion.cmake` files to the installation directory. This makes Treelite discoverable by other projects using `find_package`. ```cmake install(FILES "${PROJECT_BINARY_DIR}/cmake/TreeliteConfig.cmake" "${PROJECT_BINARY_DIR}/cmake/TreeliteConfigVersion.cmake" DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/treelite") ``` -------------------------------- ### Install Treelite Targets Source: https://github.com/dmlc/treelite/blob/mainline/CMakeLists.txt Installs the main Treelite targets, including libraries and executables, to their designated installation directories. This is typically used in the main CMakeLists.txt file. ```cmake if(CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR) # Include CPack only if the current project is top level. include(CPack) endif() include(GNUInstallDirs) include(CMakePackageConfigHelpers) set(INSTALL_TARGETS ${TREELITE_TARGETS} objtreelite rapidjson) if(NOT mdspan_FOUND) # Found mdspan via FetchContent list(APPEND INSTALL_TARGETS mdspan) endif() if(NOT nlohmann_json_FOUND) # Fetched nlohmann/json via FetchContent list(APPEND INSTALL_TARGETS nlohmann_json) endif() install(TARGETS ${INSTALL_TARGETS} EXPORT TreeliteTargets ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}" LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}" RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}" INCLUDES DESTINATION include) ``` -------------------------------- ### Install OpenMP runtime on macOS Source: https://github.com/dmlc/treelite/blob/mainline/docs/install.md Required for Treelite functionality on macOS. Install using Homebrew. ```bash brew install libomp ``` -------------------------------- ### Install Treelite using pip Source: https://github.com/dmlc/treelite/blob/mainline/docs/install.md Use this command to install the Treelite package from PyPI. This method is recommended for Windows, macOS, and Linux. ```bash pip install treelite ``` -------------------------------- ### Install Treelite Source: https://github.com/dmlc/treelite/blob/mainline/docs/index.md Install Treelite using pip or conda. Ensure you have the correct package manager configured. ```console # From PyPI pip install treelite # From Conda conda install -c conda-forge treelite ``` -------------------------------- ### Verify Treelite installation Source: https://github.com/dmlc/treelite/blob/mainline/docs/install.md Run this Python code in an interactive session to confirm that Treelite has been installed correctly. ```python import treelite ``` -------------------------------- ### Install Treelite Python package from source Source: https://github.com/dmlc/treelite/blob/mainline/docs/install.md Install the Python package after building the native libraries. This command re-uses the compiled native library. ```bash cd python pip install . ``` -------------------------------- ### Install Treelite Headers Source: https://github.com/dmlc/treelite/blob/mainline/CMakeLists.txt Installs the Treelite header files to the appropriate include directory. This ensures that other projects can include Treelite headers. ```cmake install(DIRECTORY include/treelite "${PROJECT_BINARY_DIR}/include/treelite" DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}") ``` -------------------------------- ### CMakeLists.txt for Treelite C/C++ Examples Source: https://github.com/dmlc/treelite/blob/mainline/tests/example_app/CMakeLists.txt This CMake script sets up a build environment for C and C++ applications using Treelite. It finds the Treelite package, defines executables for both C and C++ source files, and links them against the Treelite library. It also configures C++17 and C99 standards for the respective targets and checks for Conda environments. ```cmake cmake_minimum_required(VERSION 3.16) project(example_app LANGUAGES C CXX) if(DEFINED ENV{CONDA_PREFIX}) set(CMAKE_PREFIX_PATH "$ENV{CONDA_PREFIX};${CMAKE_PREFIX_PATH}") message(STATUS "Detected Conda environment, CMAKE_PREFIX_PATH set to: ${CMAKE_PREFIX_PATH}") else() message(STATUS "No Conda environment detected") endif() find_package(Treelite REQUIRED) add_executable(cpp_example example.cc) target_link_libraries(cpp_example PRIVATE treelite::treelite) add_executable(c_example example.c) target_link_libraries(c_example PRIVATE treelite::treelite) set_target_properties(cpp_example PROPERTIES CXX_STANDARD 17 CXX_STANDARD_REQUIRED YES ) set_target_properties(c_example PROPERTIES C_STANDARD 99 C_STANDARD_REQUIRED YES ) ``` -------------------------------- ### Install Treelite using Conda Source: https://github.com/dmlc/treelite/blob/mainline/docs/install.md Install Treelite from the conda-forge channel. Check the provided link for available platforms. ```bash conda install -c conda-forge treelite ``` -------------------------------- ### Build Binary Classifier Model Source: https://context7.com/dmlc/treelite/llms.txt Creates a binary classification model with sigmoid transformation. This example shows setting up metadata for binary classification and defining a simple tree stump. Requires numpy and Treelite components. ```python import numpy as np from treelite.model_builder import ( Metadata, ModelBuilder, PostProcessorFunc, TreeAnnotation, ) builder = ModelBuilder( threshold_type="float32", leaf_output_type="float32", metadata=Metadata( num_feature=2, task_type="kBinaryClf", average_tree_output=False, num_target=1, num_class=[1], leaf_vector_shape=(1, 1), ), tree_annotation=TreeAnnotation(num_tree=1, target_id=[0], class_id=[0]), postprocessor=PostProcessorFunc(name="sigmoid", sigmoid_alpha=1.0), base_scores=[0.0], ) # Build a simple tree stump builder.start_tree() builder.start_node(0) builder.numerical_test( feature_id=0, threshold=0.5, default_left=True, opname="<", left_child_key=1, right_child_key=2, ) builder.end_node() builder.start_node(1) builder.leaf(-1.0) # Negative class builder.end_node() builder.start_node(2) builder.leaf(1.0) # Positive class builder.end_node() builder.end_tree() model = builder.commit() # Predictions are probability scores in [0, 1] X = np.array([[-1.0, 0.0], [1.0, 0.0]], dtype=np.float32) print(treelite.gtil.predict(model, X)) ``` -------------------------------- ### Set Include Directories Source: https://github.com/dmlc/treelite/blob/mainline/src/CMakeLists.txt Configures include directories for the Treelite object library, specifying build and install interfaces. ```cmake target_include_directories(objtreelite PUBLIC $ $ $/include>) ``` -------------------------------- ### Configure Package Configuration File Source: https://github.com/dmlc/treelite/blob/mainline/CMakeLists.txt Generates the `TreeliteConfig.cmake` file, which is essential for `find_package(Treelite)` to work correctly. It uses a template and installs it to the CMake package directory. ```cmake configure_package_config_file( cmake/TreeliteConfig.cmake.in "${PROJECT_BINARY_DIR}/cmake/TreeliteConfig.cmake" INSTALL_DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/treelite") ``` -------------------------------- ### Generate Visual Studio project with CMake (Windows) Source: https://github.com/dmlc/treelite/blob/mainline/docs/install.md Use CMake to generate a Visual Studio project for building Treelite shared libraries on Windows. Assumes Visual Studio 2022 is installed. ```dosbatch mkdir build cd build cmake .. -G"Visual Studio 17 2022" -A x64 ``` -------------------------------- ### Build Multi-class Classifier Model Source: https://context7.com/dmlc/treelite/llms.txt Constructs a multi-class classification model with softmax transformation. This example demonstrates using vector leaf outputs and setting up metadata for multiple classes. Requires numpy and Treelite components. ```python import numpy as np from treelite.model_builder import ( Metadata, ModelBuilder, PostProcessorFunc, TreeAnnotation, ) # Multi-class classifier with vector leaf outputs builder = ModelBuilder( threshold_type="float32", leaf_output_type="float32", metadata=Metadata( num_feature=1, task_type="kMultiClf", average_tree_output=False, num_target=1, num_class=[3], # 3 classes leaf_vector_shape=(1, 3), # Vector output per leaf ), tree_annotation=TreeAnnotation( num_tree=1, target_id=[0], class_id=[-1], # -1 means tree outputs for all classes ), postprocessor=PostProcessorFunc(name="softmax"), base_scores=[0.0, 0.0, 0.0], # One base score per class ) builder.start_tree() builder.start_node(0) builder.numerical_test( feature_id=0, threshold=0.0, default_left=True, opname="<", left_child_key=1, right_child_key=2, ) builder.end_node() builder.start_node(1) builder.leaf([0.5, 0.5, 0.0]) # Vector output for 3 classes builder.end_node() builder.start_node(2) builder.leaf([0.0, 0.0, 1.0]) builder.end_node() builder.end_tree() model = builder.commit() X = np.array([[-1.0], [1.0]], dtype=np.float32) print(treelite.gtil.predict(model, X)) ``` -------------------------------- ### Construct Trees for Multi-Class Classifier Source: https://github.com/dmlc/treelite/blob/mainline/docs/tutorials/builder.md Programmatically build individual decision trees, defining nodes, tests, and leaf values. This example creates three trees, each contributing to a different class score. ```python for tree_id in range(3): builder.start_tree() builder.start_node(0) builder.numerical_test( feature_id=0, threshold=0.0, default_left=True, opname="<", left_child_key=1, right_child_key=2, ) builder.end_node() builder.start_node(1) builder.leaf(0.5 if tree_id < 2 else 0.0) builder.end_node() builder.start_node(2) builder.leaf(1.0 if tree_id == 2 else 0.0) builder.end_node() builder.end_tree() model = builder.commit() X = np.array([[-1.0], [1.0]]) print(treelite.gtil.predict(model, X)) ``` -------------------------------- ### Model JSON Structure Source: https://github.com/dmlc/treelite/blob/mainline/docs/tutorials/builder.md This is an example JSON output representing a Treelite model, detailing its features, task type, and tree structures. ```json { "num_feature": 3, "task_type": "kRegressor", "average_tree_output": false, "num_target": 1, "num_class": [1], "leaf_vector_shape": [1, 1], "target_id": [0, 0], "class_id": [0, 0], "postprocessor": "identity", "sigmoid_alpha": 1.0, "ratio_c": 1.0, "base_scores": [0.0], "attributes": "{}", "trees": [{ "num_nodes": 5, "has_categorical_split": false, "nodes": [{ "node_id": 0, "split_feature_id": 0, "default_left": true, "node_type": "numerical_test_node", "comparison_op": "<", "threshold": 5.0, "left_child": 1, "right_child": 2 }, { "node_id": 1, "split_feature_id": 2, "default_left": false, "node_type": "numerical_test_node", "comparison_op": "<", "threshold": -3.0, "left_child": 3, "right_child": 4 }, { "node_id": 2, "leaf_value": 0.6000000238418579 }, { "node_id": 3, "leaf_value": -0.4000000059604645 }, { "node_id": 4, "leaf_value": 1.2000000476837159 }] }, { "num_nodes": 5, "has_categorical_split": false, "nodes": [{ "node_id": 0, "split_feature_id": 1, "default_left": false, "node_type": "numerical_test_node", "comparison_op": "<", "threshold": 2.5, "left_child": 1, "right_child": 2 }, { "node_id": 1, "leaf_value": 1.600000023841858 }, { "node_id": 2, "split_feature_id": 2, "default_left": true, "node_type": "numerical_test_node", "comparison_op": "<", "threshold": -1.2000000476837159, "left_child": 3, "right_child": 4 }, { "node_id": 3, "leaf_value": 0.10000000149011612 }, { "node_id": 4, "leaf_value": -0.30000001192092898 }] }] } ``` -------------------------------- ### Write Version and Set Configuration Source: https://github.com/dmlc/treelite/blob/mainline/CMakeLists.txt Helper commands to write the version information and set the default build configuration to Release. These are standard CMake practices. ```cmake write_version() set_default_configuration_release() ``` -------------------------------- ### Load LightGBM models Source: https://context7.com/dmlc/treelite/llms.txt Import LightGBM gradient boosting models into Treelite format, either from a file or directly from a LightGBM Booster object. ```python import treelite # Load LightGBM model from file model = treelite.frontend.load_lightgbm_model("lightgbm_model.txt") ``` ```python # Load directly from LightGBM Booster object import lightgbm as lgb bst = lgb.Booster(model_file="lightgbm_model.txt") model = treelite.frontend.from_lightgbm(bst) ``` -------------------------------- ### Import LightGBM Booster Object Source: https://github.com/dmlc/treelite/blob/mainline/docs/treelite-api.md Load a tree ensemble model directly from a LightGBM Booster object in Python. Use this when you have an active LightGBM model in memory. ```python treelite.frontend.from_lightgbm(booster) ``` -------------------------------- ### HeaderAccessor Class Source: https://github.com/dmlc/treelite/blob/mainline/docs/treelite-api.md Provides methods to get and set fields in the model's header. ```APIDOC ### *class* treelite.model.HeaderAccessor(model) #### Description Accessor for fields in the header #### Parameters - **model** (Model) - Required - The model object #### get_field(name) ##### Description Get a field ##### Parameters - **name** (str) - Required - Name of the field. Consult [the model spec](serialization/v4.md) for the list of fields. ##### Returns - **field** (numpy.ndarray | str) - Value in the field (`str` for a string field, `np.ndarray` for other fields) #### set_field(name, value) ##### Description Set a field ##### Parameters - **name** (str) - Required - Name of the field. Consult [the model spec](serialization/v4.md) for the list of fields. - **value** (ndarray | str) - Required - New value for the field (`str` for a string field, `np.ndarray` for other fields) ##### Return type None ``` -------------------------------- ### TreeAccessor Class Source: https://github.com/dmlc/treelite/blob/mainline/docs/treelite-api.md Provides methods to get and set fields within a specific tree of the model. ```APIDOC ### *class* treelite.model.TreeAccessor(model, tree_id) #### Description Accessor for fields in a tree #### Parameters - **model** (Model) - Required - The model object - **tree_id** (int) - Required - ID of the tree #### get_field(name) ##### Description Get a field ##### Parameters - **name** (str) - Required - Name of the field. Consult [the model spec](serialization/v4.md) for the list of fields. ##### Returns - **field** (numpy.ndarray) - Value in the field #### set_field(name, value) ##### Description Set a field ##### Parameters - **name** (str) - Required - Name of the field. Consult [the model spec](serialization/v4.md) for the list of fields. - **value** (ndarray) - Required - New value for the field ##### Return type None ``` -------------------------------- ### Configure Treelite C++ Test Executable Source: https://github.com/dmlc/treelite/blob/mainline/tests/cpp/CMakeLists.txt Sets up the Treelite C++ test executable, specifying C++17 standard, linking necessary libraries like objtreelite, rapidjson, GTest, and fmt, and setting the output directory. ```cmake add_executable(treelite_cpp_test) set_target_properties(treelite_cpp_test PROPERTIES CXX_STANDARD 17 CXX_STANDARD_REQUIRED ON) target_link_libraries(treelite_cpp_test PRIVATE objtreelite rapidjson GTest::gtest GTest::gmock fmt::fmt-header-only std::mdspan) set_output_directory(treelite_cpp_test ${PROJECT_BINARY_DIR}) ``` -------------------------------- ### Load LightGBM Model Source: https://github.com/dmlc/treelite/blob/mainline/docs/tutorials/import.md Import a LightGBM model from its text file representation using the `load_lightgbm_model` function. ```python model = treelite.frontend.load_lightgbm_model("lightgbm_model.txt") ``` -------------------------------- ### Access and Modify Header Fields Source: https://github.com/dmlc/treelite/blob/mainline/docs/tutorials/edit.md Use the header accessor to get and set fields like 'num_feature' and 'postprocessor'. For scalar fields, provide a length-1 NumPy array. ```python import treelite import numpy as np # model is treelite.Model object # Get the "num_feature" field in the header model.get_header_accessor().get_field("num_feature") # Modify the "num_feature" field in the header. Use length-1 array to indicate scalar new_value = np.array([100], dtype=np.int32) model.get_header_accessor().set_field("num_feature", new_value) # Get the "postprocessor" field in the header model.get_header_accessor().get_field("postprocessor") # Modify the "postprocessor" field in the header model.get_header_accessor().set_field("postprocessor", "identity") ``` -------------------------------- ### Configure OpenMP Support Source: https://github.com/dmlc/treelite/blob/mainline/src/CMakeLists.txt Configures OpenMP support, including platform-specific handling for macOS with Homebrew and a fallback mechanism for locating libomp. ```cmake if(USE_OPENMP) if(APPLE) find_package(OpenMP) if (NOT OpenMP_FOUND) # Try again with extra path info; required for libomp 15+ from Homebrew message(STATUS "OpenMP not found; attempting to locate libomp from Homebrew...") execute_process(COMMAND brew --prefix libomp OUTPUT_VARIABLE HOMEBREW_LIBOMP_PREFIX OUTPUT_STRIP_TRAILING_WHITESPACE) set(OpenMP_C_FLAGS "-Xpreprocessor -fopenmp -I${HOMEBREW_LIBOMP_PREFIX}/include") set(OpenMP_CXX_FLAGS "-Xpreprocessor -fopenmp -I${HOMEBREW_LIBOMP_PREFIX}/include") set(OpenMP_C_LIB_NAMES omp) set(OpenMP_CXX_LIB_NAMES omp) set(OpenMP_omp_LIBRARY ${HOMEBREW_LIBOMP_PREFIX}/lib/libomp.dylib) find_package(OpenMP REQUIRED) endif() else() find_package(OpenMP REQUIRED) endif() else() message(STATUS "Disabling OpenMP") endif() ``` -------------------------------- ### Load XGBoost Model from File Source: https://github.com/dmlc/treelite/blob/mainline/docs/tutorials/import.md Import XGBoost models from model files. Supports both JSON and legacy binary formats. ```python # JSON format model = treelite.frontend.load_xgboost_model("my_model.json") # Legacy binary format model = treelite.frontend.load_xgboost_model_legacy_binary("my_model.model") ``` -------------------------------- ### Load, Serialize, and Deserialize Treelite Models Source: https://context7.com/dmlc/treelite/llms.txt Demonstrates loading an XGBoost model, serializing it to a file and bytes, and deserializing it back. Use serialization for saving checkpoints or network transfer. ```python import treelite # Load or create a model model = treelite.frontend.load_xgboost_model("model.json") # Serialize to file model.serialize("model_checkpoint.bin") # Deserialize from file loaded_model = treelite.Model.deserialize("model_checkpoint.bin") # Serialize to bytes (for network transfer or in-memory storage) model_bytes = model.serialize_bytes() # Deserialize from bytes loaded_model = treelite.Model.deserialize_bytes(model_bytes) ``` -------------------------------- ### Access and Modify Tree Leaf Values Source: https://github.com/dmlc/treelite/blob/mainline/docs/tutorials/edit.md Use the tree accessor to get and set fields within a specific tree, such as 'leaf_value'. Ensure the NumPy array matches the expected data type and size. ```python # Get the "leaf_value" field in the first tree model.get_tree_accessor(0).get_field("leaf_value") # Modify the "leaf_value" field in the first tree new_value = np.array([0, 0, 0.5, 1, -0.5], dtype=np.float32) model.get_tree_accessor(0).set_field("leaf_value", new_value) ``` -------------------------------- ### Build Treelite Model with Categorical Splits Source: https://context7.com/dmlc/treelite/llms.txt Illustrates how to construct a Treelite model programmatically using `ModelBuilder`, specifically demonstrating how to define categorical feature tests. This is useful for creating models with complex decision logic. ```python import numpy as np from treelite.model_builder import ( Metadata, ModelBuilder, PostProcessorFunc, TreeAnnotation, ) builder = ModelBuilder( threshold_type="float32", leaf_output_type="float32", metadata=Metadata( num_feature=2, task_type="kRegressor", average_tree_output=False, num_target=1, num_class=[1], leaf_vector_shape=(1, 1), ), tree_annotation=TreeAnnotation(num_tree=1, target_id=[0], class_id=[0]), postprocessor=PostProcessorFunc(name="identity"), base_scores=[0.0], ) builder.start_tree() builder.start_node(0) # Categorical test: if feature[0] in {1, 3, 5} go to right child builder.categorical_test( feature_id=0, default_left=True, category_list=[1, 3, 5], # Categories to match category_list_right_child=True, # Match goes right left_child_key=1, right_child_key=2, ) builder.end_node() builder.start_node(1) builder.leaf(-1.0) builder.end_node() builder.start_node(2) builder.leaf(1.0) builder.end_node() builder.end_tree() model = builder.commit() ``` -------------------------------- ### Generate build files with CMake (Linux/macOS) Source: https://github.com/dmlc/treelite/blob/mainline/docs/install.md Use CMake to generate Makefiles for building Treelite shared libraries on Linux and macOS. ```bash mkdir build cd build cmake .. ``` -------------------------------- ### Construct Tree 0 using ModelBuilder Source: https://github.com/dmlc/treelite/blob/mainline/docs/tutorials/builder.md Defines the structure of the first tree (Tree 0) using the ModelBuilder. This includes setting up numerical tests with feature IDs, thresholds, default directions, child nodes, and leaf nodes with their respective values. ```python # Tree 0 builder.start_tree() # Tree 0, Node 0 builder.start_node(0) builder.numerical_test( feature_id=0, threshold=5.0, default_left=True, opname="<", left_child_key=1, right_child_key=2, ) builder.end_node() # Tree 0, Node 1 builder.start_node(1) builder.numerical_test( feature_id=2, threshold=-3.0, default_left=False, opname="<", left_child_key=3, right_child_key=4, ) builder.end_node() # Tree 0, Node 2 builder.start_node(2) builder.leaf(0.6) builder.end_node() # Tree 0, Node 3 builder.start_node(3) builder.leaf(-0.4) builder.end_node() # Tree 0, Node 4 builder.start_node(4) builder.leaf(1.2) builder.end_node() builder.end_tree() ``` -------------------------------- ### Import XGBoost Model from Booster Object Source: https://github.com/dmlc/treelite/blob/mainline/docs/tutorials/import.md Use this method to import an XGBoost model directly from an existing `xgboost.Booster` object. ```python # bst = an object of type xgboost.Booster model = treelite.frontend.from_xgboost(bst) ``` -------------------------------- ### Test Coverage Compile and Link Options Source: https://github.com/dmlc/treelite/blob/mainline/tests/cpp/CMakeLists.txt Enables test coverage by adding specific compile and link options. This is not available on Windows and will cause a fatal error if attempted. ```cmake if(TEST_COVERAGE) if(MSVC) message(FATAL_ERROR "Test coverage not available on Windows") endif() target_compile_options(treelite_cpp_test PUBLIC -g3 --coverage) target_link_options(treelite_cpp_test PUBLIC --coverage) endif() ``` -------------------------------- ### Model Loaders - From LightGBM Booster Object Source: https://github.com/dmlc/treelite/blob/mainline/docs/treelite-api.md Loads a tree ensemble model directly from a LightGBM Booster object. ```APIDOC ## treelite.frontend.from_lightgbm(booster) ### Description Loads a tree ensemble model from a LightGBM Booster object. ### Parameters #### Path Parameters - **booster** (object of type `lightgbm.Booster`) - Required - Python handle to LightGBM model ### Returns - **model** (Model) - Loaded model ``` -------------------------------- ### Inspect Treelite Model Properties and Export as JSON Source: https://context7.com/dmlc/treelite/llms.txt Shows how to query model properties like the number of trees and features, and export the model structure to human-readable or compact JSON formats. Useful for debugging and understanding model composition. ```python import treelite model = treelite.frontend.load_xgboost_model("model.json") # Query model properties print(f"Number of trees: {model.num_tree}") print(f"Number of features: {model.num_feature}") print(f"Input type: {model.input_type}") print(f"Output type: {model.output_type}") # Get depth of each tree tree_depths = model.get_tree_depth() print(f"Tree depths: {tree_depths}") # Export as JSON for inspection json_str = model.dump_as_json(pretty_print=True) print(json_str) # Compact JSON (no pretty printing) compact_json = model.dump_as_json(pretty_print=False) ``` -------------------------------- ### Initialize ModelBuilder for Regression Source: https://github.com/dmlc/treelite/blob/mainline/docs/tutorials/builder.md Initializes a ModelBuilder for a regression task. Ensure metadata like feature count, task type, and output shapes are correctly set. The postprocessor is set to 'identity' for regression, and base scores are provided. ```python import treelite from treelite.model_builder import ( Metadata, ModelBuilder, PostProcessorFunc, TreeAnnotation, ) builder = ModelBuilder( threshold_type="float32", leaf_output_type="float32", metadata=Metadata( num_feature=3, task_type="kRegressor", # Regression model average_tree_output=False, num_target=1, num_class=[1], # Set num_class=1 for regression model leaf_vector_shape=(1, 1), # Each tree outputs a scalar ), # Every tree generates output for target 0, class 0 tree_annotation=TreeAnnotation(num_tree=2, target_id=[0, 0], class_id=[0, 0]), # The link function for the output is the identity function postprocessor=PostProcessorFunc(name="identity"), # Add this value for all outputs. Also known as the intercept. base_scores=[0.0], ) ``` -------------------------------- ### Build and Modify a Treelite Tree Source: https://github.com/dmlc/treelite/blob/mainline/docs/tutorials/edit.md Demonstrates building a simple Treelite tree and then modifying its fields, including adding nodes and updating node properties. Ensure NumPy arrays are used with correct dtypes and lengths. ```python import treelite from treelite.model_builder import ( Metadata, ModelBuilder, PostProcessorFunc, TreeAnnotation, ) # Tree stump with 3 nodes builder = ModelBuilder( threshold_type="float32", leaf_output_type="float32", metadata=Metadata( num_feature=2, task_type="kRegressor", average_tree_output=False, num_target=1, num_class=[1], leaf_vector_shape=(1, 1), ), tree_annotation=TreeAnnotation(num_tree=1, target_id=[0], class_id=[0]), postprocessor=PostProcessorFunc(name="identity"), base_scores=[0.0], ) builder.start_tree() builder.start_node(0) builder.numerical_test( feature_id=0, threshold=0.0, default_left=False, opname="<=", left_child_key=1, right_child_key=2, ) builder.end_node() builder.start_node(1) builder.leaf(-1.0) builder.end_node() builder.start_node(2) builder.leaf(1.0) builder.end_node() builder.end_tree() model = builder.commit() tree = model.get_tree_accessor(0) # Add a test node. The tree now has 5 nodes total tree.set_field("num_nodes", np.array([5], dtype=np.int32)) tree.set_field("node_type", np.array([1, 0, 1, 0, 0], dtype=np.int8)) tree.set_field("cleft", np.array([1, -1, 3, -1, -1], dtype=np.int32)) tree.set_field("cright", np.array([2, -1, 4, -1, -1], dtype=np.int32)) tree.set_field("split_index", np.array([0, -1, 1, -1, 1], dtype=np.int32)) tree.set_field("default_left", np.array([0, 0, 0, 0, 0], dtype=np.int8)) tree.set_field("leaf_value", np.array([0.0, 1.0, 0.0, 2.0, 3.0], dtype=np.float32)) tree.set_field("threshold", np.array([1.0, 0.0, 2.0, 0.0, 0.0], dtype=np.float32)) tree.set_field("cmp", np.array([2, 0, 2, 0, 0], dtype=np.int8)) tree.set_field("category_list_right_child", np.array([0] * 5, dtype=np.uint8)) tree.set_field("leaf_vector_begin", np.array([0] * 5, dtype=np.uint64)) tree.set_field("leaf_vector_end", np.array([0] * 5, dtype=np.uint64)) tree.set_field("category_list_begin", np.array([0] * 5, dtype=np.uint64)) tree.set_field("category_list_end", np.array([0] * 5, dtype=np.uint64)) ``` -------------------------------- ### Build Tree Nodes with ModelBuilder Source: https://github.com/dmlc/treelite/blob/mainline/docs/tutorials/builder.md Use `start_node` and `end_node` to define tree structures. Ensure nodes are declared in arbitrary order, but the first specified node is the root. Failure to call these methods will result in an error. ```python # Tree 1, Node 3 builder.start_node(3) builder.leaf(0.1) builder.end_node() # Tree 1, Node 4 builder.start_node(4) builder.leaf(-0.3) builder.end_node() builder.end_tree() ``` -------------------------------- ### Construct Tree 1 using ModelBuilder Source: https://github.com/dmlc/treelite/blob/mainline/docs/tutorials/builder.md Defines the structure of the second tree (Tree 1) using the ModelBuilder. This involves setting up numerical tests and leaf nodes, similar to Tree 0, but with different feature IDs, thresholds, and child node configurations. ```python # Tree 1 builder.start_tree() # Tree 1, Node 0 builder.start_node(0) builder.numerical_test( feature_id=1, threshold=2.5, default_left=False, opname="<", left_child_key=1, right_child_key=2, ) builder.end_node() # Tree 1, Node 1 builder.start_node(1) builder.leaf(1.6) builder.end_node() # Tree 1, Node 2 builder.start_node(2) builder.numerical_test( feature_id=2, threshold=-1.2, default_left=True, opname="<", left_child_key=3, right_child_key=4, ) builder.end_node() ``` -------------------------------- ### Initialize Treelite ModelBuilder Source: https://github.com/dmlc/treelite/blob/mainline/docs/treelite-api.md Initialize the Treelite ModelBuilder with essential configurations including threshold type, leaf output type, metadata, and post-processor. This object is used to iteratively construct a tree ensemble model. ```python from treelite.model_builder import ModelBuilder, Metadata, TreeAnnotation, PostProcessorFunc metadata = Metadata( num_feature=10, task_type="kRegressor", average_tree_output=True, num_target=1, num_class=[0], leaf_vector_shape=(1,) ) tree_annotation = TreeAnnotation( num_tree=5, target_id=[-1, 0, 0, 1, 1], class_id=[0, 0, 1, 0, 1] ) postprocessor = PostProcessorFunc( name="sigmoid", sigmoid_alpha=2.0 ) builder = ModelBuilder( threshold_type="float32", leaf_output_type="float32", metadata=metadata, tree_annotation=tree_annotation, postprocessor=postprocessor ) ``` -------------------------------- ### Model Loaders - LightGBM Source: https://github.com/dmlc/treelite/blob/mainline/docs/treelite-api.md Loads a tree ensemble model from a LightGBM model file. ```APIDOC ## treelite.frontend.load_lightgbm_model(filename) ### Description Loads a tree ensemble model from a LightGBM model file. ### Parameters #### Path Parameters - **filename** (str | Path) - Required - Path to model file ### Returns - **model** (Model) - Loaded model ### Request Example ```python lgb_model = treelite.frontend.load_lightgbm_model("lightgbm_model.txt") ``` ``` -------------------------------- ### Build Regression Model with 2 Trees Source: https://context7.com/dmlc/treelite/llms.txt Constructs a regression model with two trees using ModelBuilder. Demonstrates defining metadata, tree structure, numerical tests, and leaf nodes. Requires importing numpy and Treelite components. ```python import numpy as np from treelite.model_builder import ( Metadata, ModelBuilder, PostProcessorFunc, TreeAnnotation, ) # Create a regression model with 2 trees builder = ModelBuilder( threshold_type="float32", leaf_output_type="float32", metadata=Metadata( num_feature=3, task_type="kRegressor", # Options: kRegressor, kBinaryClf, kMultiClf, kLearningToRank, kIsolationForest average_tree_output=False, num_target=1, num_class=[1], # Set [1] for regression leaf_vector_shape=(1, 1), # Scalar output per leaf ), tree_annotation=TreeAnnotation(num_tree=2, target_id=[0, 0], class_id=[0, 0]), postprocessor=PostProcessorFunc(name="identity"), # No transformation base_scores=[0.0], # Intercept/bias term ) # Build Tree 0: if feature[0] < 5.0 then go left builder.start_tree() builder.start_node(0) # Root node (key=0) builder.numerical_test( feature_id=0, threshold=5.0, default_left=True, # Missing values go left opname="<", left_child_key=1, right_child_key=2, ) builder.end_node() builder.start_node(1) # Left child - internal node builder.numerical_test( feature_id=2, threshold=-3.0, default_left=False, opname="<", left_child_key=3, right_child_key=4, ) builder.end_node() builder.start_node(2) # Right child - leaf builder.leaf(0.6) builder.end_node() builder.start_node(3) # Leaf node builder.leaf(-0.4) builder.end_node() builder.start_node(4) # Leaf node builder.leaf(1.2) builder.end_node() builder.end_tree() # Build Tree 1 builder.start_tree() builder.start_node(0) builder.numerical_test( feature_id=1, threshold=2.5, default_left=False, opname="<", left_child_key=1, right_child_key=2, ) builder.end_node() builder.start_node(1) builder.leaf(1.6) builder.end_node() builder.start_node(2) builder.leaf(-0.3) builder.end_node() builder.end_tree() # Finalize the model model = builder.commit() # Run predictions X = np.array([[0.0, 0.0, -5.0], [10.0, 5.0, 1.0]], dtype=np.float32) print(treelite.gtil.predict(model, X)) ``` -------------------------------- ### Specify Treelite C++ Test Source Files Source: https://github.com/dmlc/treelite/blob/mainline/tests/cpp/CMakeLists.txt Lists the source files to be compiled for the Treelite C++ test executable. ```cmake target_sources(treelite_cpp_test PRIVATE test_main.cc test_gtil.cc test_model_builder.cc test_model_concat.cc test_model_loader.cc test_serializer.cc test_utils.cc ) ``` -------------------------------- ### Clone Treelite repository Source: https://github.com/dmlc/treelite/blob/mainline/docs/install.md Clone the Treelite repository from GitHub to compile from source. ```bash git clone https://github.com/dmlc/treelite.git cd treelite ``` -------------------------------- ### Build shared libraries with Make (Linux/macOS) Source: https://github.com/dmlc/treelite/blob/mainline/docs/install.md Invoke GNU Make to compile the Treelite shared libraries after CMake has generated the build files. ```bash make ``` -------------------------------- ### Inspect Model with JSON Dump Source: https://github.com/dmlc/treelite/blob/mainline/docs/tutorials/builder.md Use `dump_as_json()` to inspect the structure and content of the built Treelite model. ```python print(model.dump_as_json()) ``` -------------------------------- ### Import XGBoost Booster Object Source: https://github.com/dmlc/treelite/blob/mainline/docs/treelite-api.md Load a tree ensemble model directly from an XGBoost Booster object in Python. This is useful when you have an active XGBoost model in memory. ```python treelite.frontend.from_xgboost(booster) ``` -------------------------------- ### Predict with Built Multi-Class Model Source: https://github.com/dmlc/treelite/blob/mainline/docs/tutorials/builder.md Print the predictions of the constructed multi-class classification model on a sample input array. ```none [[0.38365173 0.38365173 0.23269653] [0.21194156 0.21194156 0.57611686]] ``` -------------------------------- ### Build Binary Classifier with Sigmoid Postprocessor Source: https://github.com/dmlc/treelite/blob/mainline/docs/tutorials/builder.md Configure a ModelBuilder for binary classification using the sigmoid postprocessor to generate probability scores. Ensure metadata and tree annotations are set correctly for binary classification tasks. ```python builder = ModelBuilder( threshold_type="float32", leaf_output_type="float32", metadata=Metadata( num_feature=3, task_type="kBinaryClf", average_tree_output=False, num_target=1, num_class=[1], leaf_vector_shape=(1, 1), ), # Every tree generates output for target 0, class 0 tree_annotation=TreeAnnotation(num_tree=2, target_id=[0, 0], class_id=[0, 0]), # The link function for the output is the sigmoid function postprocessor=PostProcessorFunc(name="sigmoid"), # Add this value for all outputs. Also known as the intercept. base_scores=[0.0], ) ``` -------------------------------- ### Link OpenMP Support Source: https://github.com/dmlc/treelite/blob/mainline/src/CMakeLists.txt Links the OpenMP library and defines a preprocessor macro if OpenMP support is enabled. ```cmake if(USE_OPENMP) target_link_libraries(objtreelite PUBLIC OpenMP::OpenMP_CXX) target_compile_definitions(objtreelite PUBLIC -DTREELITE_OPENMP_SUPPORT) endif() ``` -------------------------------- ### Specify Source Files for Treelite Object Library Source: https://github.com/dmlc/treelite/blob/mainline/src/CMakeLists.txt Lists all source files to be compiled into the Treelite object library, including header files and implementation files across various modules. ```cmake target_sources(objtreelite PRIVATE ${PROJECT_SOURCE_DIR}/include/treelite/c_api.h ${PROJECT_SOURCE_DIR}/include/treelite/c_api_error.h ${PROJECT_SOURCE_DIR}/include/treelite/contiguous_array.h ${PROJECT_SOURCE_DIR}/include/treelite/error.h ${PROJECT_SOURCE_DIR}/include/treelite/gtil.h ${PROJECT_SOURCE_DIR}/include/treelite/logging.h ${PROJECT_SOURCE_DIR}/include/treelite/model_builder.h ${PROJECT_SOURCE_DIR}/include/treelite/model_loader.h ${PROJECT_SOURCE_DIR}/include/treelite/pybuffer_frame.h ${PROJECT_SOURCE_DIR}/include/treelite/thread_local.h ${PROJECT_SOURCE_DIR}/include/treelite/tree.h ${PROJECT_SOURCE_DIR}/include/treelite/detail/contiguous_array.h ${PROJECT_SOURCE_DIR}/include/treelite/detail/file_utils.h ${PROJECT_SOURCE_DIR}/include/treelite/detail/omp_exception.h ${PROJECT_SOURCE_DIR}/include/treelite/detail/serializer.h ${PROJECT_SOURCE_DIR}/include/treelite/detail/serializer_mixins.h ${PROJECT_SOURCE_DIR}/include/treelite/detail/threading_utils.h ${PROJECT_SOURCE_DIR}/include/treelite/detail/tree.h ${PROJECT_SOURCE_DIR}/include/treelite/enum/operator.h ${PROJECT_SOURCE_DIR}/include/treelite/enum/task_type.h ${PROJECT_SOURCE_DIR}/include/treelite/enum/tree_node_type.h ${PROJECT_SOURCE_DIR}/include/treelite/enum/typeinfo.h field_accessor.cc json_serializer.cc logging.cc model_concat.cc model_query.cc serializer.cc c_api/c_api_error.cc c_api/c_api_utils.h c_api/field_accessor.cc c_api/gtil.cc c_api/logging.cc c_api/model.cc c_api/model_builder.cc c_api/model_loader.cc c_api/serializer.cc c_api/sklearn.cc enum/operator.cc enum/task_type.cc enum/tree_node_type.cc enum/typeinfo.cc gtil/config.cc gtil/output_shape.cc gtil/postprocessor.cc gtil/postprocessor.h gtil/predict.cc model_builder/metadata.cc model_builder/model_builder.cc model_builder/detail/json_parsing.h model_loader/lightgbm.cc model_loader/sklearn.cc model_loader/sklearn_bulk.cc model_loader/xgboost_json.cc model_loader/xgboost_legacy.cc model_loader/xgboost_ubjson.cc model_loader/detail/lightgbm.h model_loader/detail/string_utils.h model_loader/detail/xgboost.cc model_loader/detail/xgboost.h model_loader/detail/xgboost_json/sax_adapters.h model_loader/detail/xgboost_json/sax_adapters.cc model_loader/detail/xgboost_json/delegated_handler.h model_loader/detail/xgboost_json/delegated_handler.cc ) ``` -------------------------------- ### Windows Specific Compile Options Source: https://github.com/dmlc/treelite/blob/mainline/tests/cpp/CMakeLists.txt Applies Windows-specific compile options for the Treelite C++ test executable, including UTF-8 encoding and disabling secure warnings. ```cmake if(MSVC) target_compile_options(treelite_cpp_test PRIVATE /utf-8 -D_CRT_SECURE_NO_WARNINGS -D_CRT_SECURE_NO_DEPRECATE) endif() ``` -------------------------------- ### Load LightGBM Model Source: https://github.com/dmlc/treelite/blob/mainline/docs/treelite-api.md Load a tree ensemble model from a LightGBM model file. This function is used for models saved in LightGBM's standard file format. ```python lgb_model = treelite.frontend.load_lightgbm_model("lightgbm_model.txt") ``` -------------------------------- ### Import scikit-learn models into Treelite Source: https://context7.com/dmlc/treelite/llms.txt Import various scikit-learn tree ensemble models including RandomForest, ExtraTrees, GradientBoosting, and IsolationForest into Treelite format. ```python import sklearn.datasets import sklearn.ensemble import treelite.sklearn # Train a RandomForestRegressor X, y = sklearn.datasets.load_diabetes(return_X_y=True) clf = sklearn.ensemble.RandomForestRegressor(n_estimators=10) clf.fit(X, y) # Import into Treelite model = treelite.sklearn.import_model(clf) ``` ```python # Example with IsolationForest iso_clf = sklearn.ensemble.IsolationForest(n_estimators=100) iso_clf.fit(X) iso_model = treelite.sklearn.import_model(iso_clf) ``` -------------------------------- ### Build Multi-Class Classifier with Vector Leaf and Softmax Source: https://github.com/dmlc/treelite/blob/mainline/docs/tutorials/builder.md Construct a multi-class classifier using ModelBuilder with vector leaf outputs and the softmax postprocessor. This configuration is suitable for tasks where the model predicts probabilities for multiple distinct classes. ```python builder = ModelBuilder( threshold_type="float32", leaf_output_type="float32", metadata=Metadata( num_feature=1, task_type="kMultiClf", # To indicate multi-class classification average_tree_output=False, num_target=1, num_class=[3], leaf_vector_shape=(1, 3), ), # Every tree generates probability scores for all classes, so class_id=-1 tree_annotation=TreeAnnotation(num_tree=1, target_id=[0], class_id=[-1]), # The link function for the output is the softmax function postprocessor=PostProcessorFunc(name="softmax"), # base_scores must have length (num_target * max(num_class)) base_scores=[0.0, 0.0, 0.0], ) ``` ```python builder.start_tree() builder.start_node(0) builder.numerical_test( feature_id=0, threshold=0.0, default_left=True, opname="<", left_child_key=1, right_child_key=2, ) builder.end_node() builder.start_node(1) builder.leaf([0.5, 0.5, 0.0]) builder.end_node() builder.start_node(2) builder.leaf([0.0, 0.0, 1.0]) builder.end_node() builder.end_tree() ``` -------------------------------- ### Build Models Programmatically Source: https://context7.com/dmlc/treelite/llms.txt Use the ModelBuilder API to construct custom tree ensemble models from scratch. ```python import numpy as np import treelite from treelite.model_builder import ( Metadata, ModelBuilder, PostProcessorFunc, TreeAnnotation, ) ``` -------------------------------- ### Write Package Version File Source: https://github.com/dmlc/treelite/blob/mainline/CMakeLists.txt Generates the `TreeliteConfigVersion.cmake` file, which specifies the version of Treelite. This is used by CMake's package management system to ensure compatibility. ```cmake write_basic_package_version_file( "${PROJECT_BINARY_DIR}/cmake/TreeliteConfigVersion.cmake" VERSION ${PROJECT_VERSION} COMPATIBILITY SameMajorVersion) ```