# concrete.ml.sklearn.tree\_to\_numpy.md

[![](https://img.shields.io/badge/-source-cccccc?style=flat-square)](https://github.com/zama-ai/concrete-ml/blob/release/1.1.x/src/concrete/ml/sklearn/tree_to_numpy.py#L0)

## module `concrete.ml.sklearn.tree_to_numpy`

Implements the conversion of a tree model to a numpy function.

### **Global Variables**

* **MAX\_BITWIDTH\_BACKWARD\_COMPATIBLE**
* **OPSET\_VERSION\_FOR\_ONNX\_EXPORT**

***

[![](https://img.shields.io/badge/-source-cccccc?style=flat-square)](https://github.com/zama-ai/concrete-ml/blob/release/1.1.x/src/concrete/ml/sklearn/tree_to_numpy.py#L31)

### function `get_onnx_model`

```python
get_onnx_model(model: Callable, x: ndarray, framework: str) → ModelProto
```

Create ONNX model with Hummingbird convert method.

**Args:**

* `model` (Callable): The tree model to convert.
* `x` (numpy.ndarray): Dataset used to trace the tree inference and convert the model to ONNX.
* `framework` (str): The framework from which the ONNX model is generated.
* `(options`: 'xgboost', 'sklearn')

**Returns:**

* `onnx.ModelProto`: The ONNX model.

***

[![](https://img.shields.io/badge/-source-cccccc?style=flat-square)](https://github.com/zama-ai/concrete-ml/blob/release/1.1.x/src/concrete/ml/sklearn/tree_to_numpy.py#L63)

### function `workaround_squeeze_node_xgboost`

```python
workaround_squeeze_node_xgboost(onnx_model: ModelProto)
```

Workaround to fix torch issue that does not export the proper axis in the ONNX squeeze node.

FIXME: <https://github.com/zama-ai/concrete-ml-internal/issues/2778> The squeeze ops does not have the proper dimensions. remove the following workaround when the issue is fixed Add the axis attribute to the Squeeze node

**Args:**

* `onnx_model` (onnx.ModelProto): The ONNX model.

***

[![](https://img.shields.io/badge/-source-cccccc?style=flat-square)](https://github.com/zama-ai/concrete-ml/blob/release/1.1.x/src/concrete/ml/sklearn/tree_to_numpy.py#L88)

### function `add_transpose_after_last_node`

```python
add_transpose_after_last_node(onnx_model: ModelProto)
```

Add transpose after last node.

**Args:**

* `onnx_model` (onnx.ModelProto): The ONNX model.

***

[![](https://img.shields.io/badge/-source-cccccc?style=flat-square)](https://github.com/zama-ai/concrete-ml/blob/release/1.1.x/src/concrete/ml/sklearn/tree_to_numpy.py#L109)

### function `preprocess_tree_predictions`

```python
preprocess_tree_predictions(
    init_tensor: ndarray,
    output_n_bits: int
) → QuantizedArray
```

Apply post-processing from the graph.

**Args:**

* `init_tensor` (numpy.ndarray): Model parameters to be pre-processed.
* `output_n_bits` (int): The number of bits of the output.

**Returns:**

* `QuantizedArray`: Quantizer for the tree predictions.

***

[![](https://img.shields.io/badge/-source-cccccc?style=flat-square)](https://github.com/zama-ai/concrete-ml/blob/release/1.1.x/src/concrete/ml/sklearn/tree_to_numpy.py#L159)

### function `tree_onnx_graph_preprocessing`

```python
tree_onnx_graph_preprocessing(
    onnx_model: ModelProto,
    framework: str,
    expected_number_of_outputs: int
)
```

Apply pre-processing onto the ONNX graph.

**Args:**

* `onnx_model` (onnx.ModelProto): The ONNX model.
* `framework` (str): The framework from which the ONNX model is generated.
* `(options`: 'xgboost', 'sklearn')
* `expected_number_of_outputs` (int): The expected number of outputs in the ONNX model.

***

[![](https://img.shields.io/badge/-source-cccccc?style=flat-square)](https://github.com/zama-ai/concrete-ml/blob/release/1.1.x/src/concrete/ml/sklearn/tree_to_numpy.py#L208)

### function `tree_values_preprocessing`

```python
tree_values_preprocessing(
    onnx_model: ModelProto,
    framework: str,
    output_n_bits: int
) → QuantizedArray
```

Pre-process tree values.

**Args:**

* `onnx_model` (onnx.ModelProto): The ONNX model.
* `framework` (str): The framework from which the ONNX model is generated.
* `(options`: 'xgboost', 'sklearn')
* `output_n_bits` (int): The number of bits of the output.

**Returns:**

* `QuantizedArray`: Quantizer for the tree predictions.

***

[![](https://img.shields.io/badge/-source-cccccc?style=flat-square)](https://github.com/zama-ai/concrete-ml/blob/release/1.1.x/src/concrete/ml/sklearn/tree_to_numpy.py#L252)

### function `tree_to_numpy`

```python
tree_to_numpy(
    model: Callable,
    x: ndarray,
    framework: str,
    output_n_bits: int = 8
) → Tuple[Callable, List[UniformQuantizer], ModelProto]
```

Convert the tree inference to a numpy functions using Hummingbird.

**Args:**

* `model` (Callable): The tree model to convert.
* `x` (numpy.ndarray): The input data.
* `framework` (str): The framework from which the ONNX model is generated.
* `(options`: 'xgboost', 'sklearn')
* `output_n_bits` (int): The number of bits of the output. Default to 8.

**Returns:**

* `Tuple[Callable, List[QuantizedArray], onnx.ModelProto]`: A tuple with a function that takes a numpy array and returns a numpy array, QuantizedArray object to quantize and de-quantize the output of the tree, and the ONNX model.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.zama.org/concrete-ml/1.1/developer-guide/api/concrete.ml.sklearn.tree_to_numpy.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
