Loading...

Stepwise Plot

General

VerticaPy’s Stepwise Analysis tool is a valuable asset for enhancing machine learning model interpretability and feature selection. It streamlines the process of iteratively adding or removing variables to optimize model performance. By systematically evaluating the impact of different variables, data analysts gain insights into which features contribute most significantly to the model’s accuracy, facilitating data-driven decisions and model refinement. This feature is particularly useful for simplifying complex model structures and improving overall model efficiency.

Let’s begin by importing verticapy.

import verticapy as vp

Let’s generate a dataset using the following data.

N = 500 # 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 vDataFrame with two clusters
data = vp.vDataFrame({
    "x": np.concatenate([x, x + k]),
    "y": np.concatenate([y, y + k]),
    "z": np.concatenate([z, z + k]),
    "c": [0 for i in range(round(N / 2))] + [1 for i in range(round(N / 2))]
})

Let’s proceed by creating a Logistic Regression model using the complete dataset.

# Importing the Vertica ML module
import verticapy.machine_learning.vertica as vml

# Importing the model selection module
import verticapy.machine_learning.model_selection as vms

# Defining the Model
model = vml.LogisticRegression()

In the context of data visualization, we have the flexibility to harness multiple plotting libraries to craft a wide range of graphical representations. VerticaPy, as a versatile tool, provides support for several graphic libraries, such as Matplotlib, Highcharts, and Plotly. Each of these libraries offers unique features and capabilities, allowing us to choose the most suitable one for our specific data visualization needs.

_images/plotting_libs.png

Note

To select the desired plotting library, we simply need to use the set_option() function. VerticaPy offers the flexibility to smoothly transition between different plotting libraries. In instances where a particular graphic is not supported by the chosen library or is not supported within the VerticaPy framework, the tool will automatically generate a warning and then switch to an alternative library where the graphic can be created.

Please click on the tabs to view the various graphics generated by the different plotting libraries.

We can switch to using the plotly module.

vp.set_option("plotting_lib", "plotly")
figs = vms.stepwise(
  model,
  data,
  X = ["x", "y", "z"],
  y = "c",
  direction = "forward",
)

Stepwise

figs.step_wise_

Feature Importance

figs.importance_
figs = vms.stepwise(
  model,
  data,
  X = ["x", "y", "z"],
  y = "c",
  direction = "backward",
)

Stepwise

figs.step_wise_

Feature Importance

figs.importance_

We can switch to using the highcharts module.

vp.set_option("plotting_lib", "highcharts")
figs = vms.stepwise(
  model,
  data,
  X = ["x", "y", "z"],
  y = "c",
  direction = "forward",
)

Stepwise

figs.step_wise_
Loading....

Feature Importance

Loading....
figs = vms.stepwise(
  model,
  data,
  X = ["x", "y", "z"],
  y = "c",
  direction = "backward",
)

Stepwise

figs.step_wise_
Loading....

Feature Importance

figs.importance_
Loading....

We can switch to using the matplotlib module.

vp.set_option("plotting_lib", "matplotlib")
figs = vms.stepwise(
  model,
  data,
  X = ["x", "y", "z"],
  y = "c",
  direction = "forward",
)

Starting Stepwise
[Model 0] bic: -686.932572461523; Variables: []
[Model 1] bic: -18976.660225414; (+) Variable: "z"
[Model 2] bic: -19789.4020868785; (+) Variable: "x"
Selected Model

[Model 2] bic: -19789.4020868785; Variables: ['"z"', '"x"']

Stepwise

figs.step_wise_.get_figure()
../../../docs/figures/plotting_matplotlib_stepwise_forward_stepwise.png

Feature Importance

figs.importance_.get_figure()
../../../docs/figures/plotting_matplotlib_stepwise_backward_importance.png
vms.stepwise(
  model,
  data,
  X = ["x", "y", "z"],
  y = "c",
  direction = "backward",
)

Starting Stepwise
[Model 0] bic: -19441.6636833671; Variables: ['"y"', '"x"', '"z"']
[Model 1] bic: -19789.4020868785; (-) Variable: "y"
Selected Model

[Model 1] bic: -19789.4020868785; Variables: ['"x"', '"z"']
Out[3]: 
None       ...    variable           importance  
0          ...        None                  0.0  
1          ...         "y"                  0.0  
2          ...         "x"    30.95453270272508  
3          ...         "z"    69.04546729727493  
Rows: 1-4 | Columns: 6

Stepwise

figs.step_wise_.get_figure()
../../../docs/figures/plotting_matplotlib_stepwise_forward_stepwise.png

Feature Importance

figs.importance_.get_figure()
../../../docs/figures/plotting_matplotlib_stepwise_backward_importance.png

Hint

VerticaPy supports both backward and forward stepwise analysis. You simply need to select the appropriate method as a parameter.


Chart Customization

VerticaPy 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, VerticaPy’s customization options enable you to craft graphics that suit your exact needs.

Note

As stepwise plots are essentially scatter and bubble plots, customization options are identical to those available for scatter.