Loading...

Machine Learning - Tree Plots

General

In this example, we will demonstrate how to create a model tree visualization using Graphviz. It’s important to note that these plots are purely illustrative and are based on generated data.

Let’s begin by importing vastorbit.

import vastorbit as vo

Let’s also import numpy to create a random dataset.

import numpy as np

Let’s generate a dataset using the following data.

N = 100 # Number of Records
k = 10 # step

# Normal Distributions
x = np.random.normal(5, 1, round(N / 2))
y = np.random.normal(3, 1, round(N / 2))
z = np.random.normal(3, 1, round(N / 2))

# Creating a VastFrame with two clusters
data = vo.VastFrame({
    "x": np.concatenate([x, x + k]),
    "y": np.concatenate([y, y + k]),
    "z": np.concatenate([z, z + k]),
    "c": [random.randint(0, 1) for _ in range(N)]
})

Let’s proceed by creating a Random Forest Classifier model using the complete dataset.

# Importing the VAST ML module
import vastorbit.machine_learning.vast as vml

# Defining the Models
model = vml.RandomForestClassifier(n_estimators = 4)

# Fitting the models
model.fit(data, ["x", "y", "z"], "c")

vastorbit provides the option to create various types of geospatial plots, including scatter plots and heat maps. To leverage these capabilities, it’s important to have geospatial data stored within VAST, specifically in either GEOMETRY or GEOGRAPHY data types. This data forms the foundation for generating insightful geospatial visualizations using vastorbit.

Note

vastorbit offers a straightforward method to generate tree visualizations using Graphviz, making it easy to interpret and analyze decision tree models. We have plans to further enhance this functionality by extending it to Plotly in the future, providing even more dynamic and interactive visualization options for decision trees.

model.plot_tree(pic_path = "figures/plotting_graphviz_tree")
PDF Viewer

Chart Customization

vastorbit empowers users with a high degree of flexibility when it comes to tailoring the visual aspects of their plots. This customization extends to essential elements such as color schemes, text labels, and plot sizes, as well as a wide range of other attributes that can be fine-tuned to align with specific design preferences and analytical requirements. Whether you want to make your visualizations more visually appealing or need to convey specific insights with precision, vastorbit’s customization options enable you to craft graphics that suit your exact needs.

Note

vastorbit’s tree plots, generated using Graphviz, can be tailored to your preferences by utilizing Graphviz parameters. You can explore the full list of available parameters and their descriptions by visiting the following link: graphviz

Example

Changing different parameters

model.plot_tree(
    pic_path = "figures/plotting_graphviz_tree_custom",
    node_style={"shape": "box", "style": "filled"},
    edge_style={"color": "blue"},
    leaf_style={"shape": "circle", "style": "filled"},
)
PDF Viewer

Hint

In vastorbit, when a model consists of multiple trees, you can utilize the tree_id parameter to access and analyze specific trees within the model.