def convert_tree_ensemble(
    model,
    input_features,
    output_features=("predicted_class", float),
    mode="regressor",
    base_prediction=None,
    class_labels=None,
    post_evaluation_transform=None,
):
    """
    Convert a generic tree regressor model to the protobuf spec.

    This currently supports:
      * Decision tree regression
      * Gradient boosted tree regression
      * Random forest regression
      * Decision tree classifier.
      * Gradient boosted tree classifier.
      * Random forest classifier.

    ----------
    Parameters
    model: [DecisionTreeRegressor | GradientBoostingRegression | RandomForestRegressor]
        A scikit learn tree model.

    feature_names : list of strings, optional (default=None)
        Names of each of the features.

    target: str
        Name of the output column.

    base_prediction: double
        Base prediction value.

    mode: str in ['regressor', 'classifier']
        Mode of the tree model.

    class_labels: list[int]
        List of classes
        
    post_evaluation_transform: list[int]
        Post evaluation transform
        
    Returns
    -------
    model_spec: An object of type Model_pb.
        Protobuf representation of the model
    """

    num_dimensions = get_input_dimension(model)
    features = process_or_validate_features(input_features, num_dimensions)

    n_classes = None
    if mode == "classifier":
        n_classes = model.n_classes_
        if class_labels is None:
            class_labels = range(n_classes)
        else:
            if len(class_labels) != n_classes:
                raise ValueError(
                    "Number of classes in model (%d) does not match "
                    "length of supplied class list (%d)."
                    % (n_classes, len(class_labels))
                )

        coreml_tree = TreeEnsembleClassifier(
            input_features, class_labels, output_features
        )
        if post_evaluation_transform is not None:
            coreml_tree.set_post_evaluation_transform(post_evaluation_transform)

        # Base prediction not provided
        if base_prediction is None:
            if n_classes == 2:
                base_prediction = [0.0]
            else:
                base_prediction = [0.0 for c in range(n_classes)]
        coreml_tree.set_default_prediction_value(base_prediction)
    else:
        if base_prediction is None:
            base_prediction = 0.0
        coreml_tree = TreeEnsembleRegressor(input_features, output_features)
        coreml_tree.set_default_prediction_value(base_prediction)

    # Single tree
    if hasattr(model, "tree_"):
        _recurse(
            coreml_tree,
            model.tree_,
            tree_id=0,
            node_id=0,
            mode=mode,
            n_classes=n_classes,
        )

    # Multiple trees
    elif hasattr(model, "estimators_"):
        is_ensembling_in_separate_trees = False
        if type(model.estimators_) != list:
            is_ensembling_in_separate_trees = (
                len(model.estimators_.shape) > 0 and model.estimators_.shape[1] > 1
            )
            estimators = model.estimators_.flatten()
        else:
            estimators = model.estimators_

        scaling = (
            model.learning_rate
            if hasattr(model, "learning_rate")
            else 1.0 / len(estimators)
        )
        for tree_id, base_model in enumerate(estimators):
            if is_ensembling_in_separate_trees:
                tree_index = tree_id % n_classes
            else:
                tree_index = 0
            _recurse(
                coreml_tree,
                base_model.tree_,
                tree_id,
                node_id=0,
                scaling=scaling,
                mode=mode,
                n_classes=n_classes,
                tree_index=tree_index,
            )
    else:
        raise TypeError("Unknown scikit-learn tree model type.")

    return coreml_tree.spec

    Convert a generic tree regressor model to the protobuf spec.

    This currently supports:
      * Decision tree regression
      * Gradient boosted tree regression
      * Random forest regression
      * Decision tree classifier.
      * Gradient boosted tree classifier.
      * Random forest classifier.

    ----------
    Parameters
    model: [DecisionTreeRegressor | GradientBoostingRegression | RandomForestRegressor]
        A scikit learn tree model.

    feature_names : list of strings, optional (default=None)
        Names of each of the features.

    target: str
        Name of the output column.

    base_prediction: double
        Base prediction value.

    mode: str in ['regressor', 'classifier']
        Mode of the tree model.

    class_labels: list[int]
        List of classes
        
    post_evaluation_transform: list[int]
        Post evaluation transform
        
    Returns
    -------
    model_spec: An object of type Model_pb.
        Protobuf representation of the model
    
coremltools/converters/sklearn/_tree_ensemble.py