:orphan: .. _chart_gallery.tree: ============================= Machine Learning - Tree Plots ============================= .. Necessary Code Elements .. ipython:: python :suppress: :okwarning: import random import vastorbit as vo import vastorbit.machine_learning.vast as vml import numpy as np 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)]}) # Defining a Tree Based model model = vml.RandomForestClassifier(n_estimators = 4) # Fitting the model model.fit(data, ["x", "y", "z"], "c") 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``. .. ipython:: python import vastorbit as vo Let's also import ``numpy`` to create a random dataset. .. ipython:: python import numpy as np Let's generate a dataset using the following data. .. code-block:: python 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. .. code-block:: python # 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. .. tab:: Graphviz .. ipython:: python :okwarning: model.plot_tree(pic_path = "figures/plotting_graphviz_tree") .. raw:: html