There are a number of advanced features that are available when creating interactive browswer visualizations of tree learners. To access this additional functionality, use the TreePlot and Questionnaire objects for tree visualizations and questionnaires, respectively.

Each of these objects takes a tree learner as the first argument. Keyword arguments are used to control the additional functionality, as described below. Use write_html or show_in_browser to save or view the visualization. In a Jupyter notebook, the resulting visualization is displayed automatically.

Changing Names

The following keyword arguments enable you to rename various aspects of the data:

In the following example, we use feature_renames to replace some feature codes in the data with more descriptive names:

vis_renamed_features = TreePlot(lnr, feature_renames=Dict(
    "Disp" => "Displacement",
    "HP" => "Horsepower",
    "WT" => "Weight",
Optimal Trees Visualization

Extra Content Outputs

The extra_content keyword argument allows you to include additional information for each node of the tree in the visualization. To do this, simply construct a vector with one entry for each node of the tree containing the HTML content you would like to display for this node.

For example, the following code calculates the mean feature value for all the points that fall into each node of the tree:

using Statistics
node_inds = IAI.apply_nodes(lnr, X)
extras = map(node_inds) do inds
  string("<b>Mean horsepower in node:</b> ",
         round(mean(X[inds, :HP]), digits=1))
7-element Array{String,1}:
 "<b>Mean horsepower in node:</b> 146.7"
 "<b>Mean horsepower in node:</b> 72.4"
 "<b>Mean horsepower in node:</b> 160.4"
 "<b>Mean horsepower in node:</b> 129.6"
 "<b>Mean horsepower in node:</b> 105.2"
 "<b>Mean horsepower in node:</b> 154.1"
 "<b>Mean horsepower in node:</b> 248.4"

We can then include this information in the visualization by passing the extra_content keyword argument to any of the visualization functions:

vis_extra_text = IAI.TreePlot(lnr, extra_content=extras)
Optimal Trees Visualization

You can incorporate additional information using any combination of HTML/CSS/JavaScript. Note that if you include any <script> tags, the closing tag must be terminated with <\/script> for the code to function correctly.

For example, the following code uses billboard.js to visualize the split threshold at each split node in the tree:

node_inds = IAI.apply_nodes(lnr, X)
extras = map(enumerate(node_inds)) do (t, inds)
  IAI.is_leaf(lnr, t) && return ""

  feature = IAI.get_split_feature(lnr, t)
  threshold = round(IAI.get_split_threshold(lnr, t), digits=2)

  <div id="node-plot-$t" style="width: 280px;">
    var chart = bb.generate({
      data: {
        xs: {
          y: "x"
        columns: [
          ["x", $(join(X[inds, feature], ","))],
          ["y", $(join(y[inds], ","))],
        type: "scatter",
      axis: {
        x: {
          label: "$feature",
          tick: {
            count: 3,
            format: function(x) { return x.toFixed(2); }
      grid: {
        x: {
          lines: [
              value: $threshold,
              text: "$feature < $threshold"
      legend: {
        show: false
      bindto: "#node-plot-$t"

vis_extra_plots = IAI.TreePlot(lnr, extra_content=extras)
Optimal Trees Visualization

Disable Default Content Outputs

By default, the visualizations include some information about each node (as a tooltip for tree visualizations and at the end of questionnaires). You can disable the default content by passing the argument include_default_content=false to any visualization functions. A common use case is when you want to replace the default display with the extra_content materials.

Advanced Multi-learner Visualization

It is possible to use the advanced visualization controls in conjunction with multi-learner visualizations. To do this, simply pass TreePlot or Questionnaire objects with the appropriate options instead of learners when constructing the visualization.

For example, the following visualization combines the earlier examples with renamed features and extra outputs:

questions = ("Use learner with" => [
    "renamed features" => vis_renamed_features,
    "extra text output" => vis_extra_text,
    "extra plots" => vis_extra_plots,
Optimal Trees Visualization