Search Shortcut cmd + k | ctrl + k

Evaluate affine decision trees in SQL

Maintainer(s): jokasimr

Installing and Loading

INSTALL apart FROM community;
LOAD apart;

Example

LOAD apart;

-- Split on x + y = 1, then on x - y = 0.
SELECT x, y,
       decision_tree(
         {
             weights: [
                 [1.0, 1.0],
                 [1.0, -1.0],
                 [1.0, -1.0]
             ],
             thresholds: [1.0, 0.0, 0.0],
             children: [[2, 3], [-1, -2], [-4, -3]],
             values: ['A', 'B', 'C', 'D']
         },
         x, y
       ) AS region
FROM (VALUES (1, 0), (0, 1), (-1, 0), (0, -1)) AS points(x, y);

About apart

apart evaluates affine decision trees. Each node compares a weighted sum of the inputs with a threshold, and each leaf supplies a result.

The tree stays constant for the query while the inputs vary by row. Inputs can be separate columns or a fixed-size array. Leaf values can use any DuckDB type, including lists and structs.

See the documentation and example use cases.

Added Functions

function_name function_type description comment examples
decision_tree scalar NULL NULL  
fixed_depth scalar NULL NULL  

Overloaded Functions

This extension does not add any function overloads.

Added Types

This extension does not add any types.

Added Settings

This extension does not add any settings.