Loading...

Machine Learning - Classification Plots

General

In this example, we aim to present several regression plots, including linear regression, tree-based algorithms, and various residual plots. It’s important to note that these plots are purely illustrative and are based on generated data. To make the data more realistically representative, we introduce some noise, resulting in an approximately linear relationship.

Let’s begin by importing verticapy.

import verticapy as vp

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 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 both a logistic regression model and a linear SVC model using the complete dataset.

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

# Defining the Models
model_logit_1d = vml.LogisticRegression()
model_svc_1d = vml.LinearSVC()
model_svc_2d = vml.LinearSVC()
model_svc_3d = vml.LinearSVC()

# Fitting the models
model_logit_1d.fit(data, "x", "c")
model_svc_1d.fit(data, "x", "c")
model_svc_2d.fit(data, ["x", "y"], "c")
model_svc_3d.fit(data, ["x", "y", "z"], "c")

# Displaying the vDataFrame
display(data)

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")
model_svc_1d.plot()

Logit Plot

model_logit_1d.plot()
model_svc_2d.plot()
model_svc_3d.plot()

We can switch to using the highcharts module.

vp.set_option("plotting_lib", "highcharts")
model_svc_1d.plot()
Loading....

Logit Plot

model_logit_1d.plot()
Loading....
model_svc_2d.plot()
Loading....

We can switch to using the matplotlib module.

vp.set_option("plotting_lib", "matplotlib")
model_svc_1d.plot()
Out[3]: <Axes: xlabel='x'>
_images/plotting_matplotlib_svc_1d_1.png

Logit Plot

model_logit_1d.plot()
Out[4]: <Axes: xlabel='x', ylabel='c'>
_images/plotting_matplotlib_logit_1d_2.png
model_svc_2d.plot()
Out[5]: <Axes: xlabel='x', ylabel='y'>
_images/plotting_matplotlib_svc_2d_1.png
model_svc_3d.plot()
Out[6]: <Axes3D: xlabel='x', ylabel='y', zlabel='z'>
_images/plotting_matplotlib_svc_3d_1.png

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.

Important

Different customization parameters are available for Plotly, Highcharts, and Matplotlib. For a comprehensive list of customization features, please consult the documentation of the respective libraries: plotly, matplotlib and highcharts.

Colors

Custom colors

model_svc_2d.plot(colors = ["red", "blue"])

Custom colors

model_svc_2d.plot(colors = ["red", "blue"])
Loading....

Custom colors

model_svc_2d.plot(colors = ["red", "blue"])
Out[7]: <Axes: xlabel='x', ylabel='y'>
_images/plotting_matplotlib_svc_2d_plot_custom_color_1.png

Size

Custom Width and Height

model_svc_2d.plot(width = 300, height = 300)

Custom Width and Height

model_svc_2d.plot(width = 500, height = 200)
Loading....

Custom Width and Height

model_svc_2d.plot(width = 6, height = 3)
Out[8]: <Axes: xlabel='x', ylabel='y'>
_images/plotting_matplotlib_svc_2d_plot_single_custom_size.png

Text

Custom Title

model_svc_2d.plot().update_layout(title_text = "Custom Title")

Custom Axis Titles

model_svc_2d.plot(yaxis_title = "Custom Y-Axis Title")

Custom Title Text

model_svc_2d.plot(title = {"text": "Custom Title"})
Loading....

Custom Axis Titles

model_svc_2d.plot(xAxis = {"title": {"text": "Custom X-Axis Title"}})
Loading....

Custom Title Text

model_svc_2d.plot().set_title("Custom Title")
Out[9]: Text(0.5, 1.0, 'Custom Title')
_images/plotting_matplotlib_svc_2d_plot_custom_title_label.png

Custom Axis Titles

model_svc_2d.plot().set_ylabel("Custom Y Axis")
Out[10]: Text(0, 0.5, 'Custom Y Axis')
_images/plotting_matplotlib_svc_2d_plot_custom_yaxis_label.png