Machine Learning - Classification Curve¶
General¶
In this classification example, our goal is to develop a predictive model that can determine the likelihood of passengers surviving the ill-fated Titanic voyage based on two critical factors: “age” and “fare”. The objective is to demonstrate how to plot ROC curves, Precision-Recall curves, or even gain curves by building a logistic regression model. Furthermore, we will illustrate how to create these curves using just two columns: one for probabilities and one for predictions.
Let’s begin by importing VerticaPy.
import verticapy as vp
Let’s import the titanic dataset from verticapy.datasets.
from verticapy.datasets import load_titanic
data = load_titanic()
Let’s create a logistic regression model using the entire dataset.
# Importing the Vertica ML module
import verticapy.machine_learning.vertica as vml
# Importing the Metrics module
import verticapy.machine_learning.metrics as vmt
# Defining the Model
model = vml.LogisticRegression()
# Defining Predictors and Response.
X = ["age", "fare"]
y = "survived"
# Fitting the model
model.fit(data, X, y)
# Adding the probabilities to the vDataFrame
model.predict_proba(data, X, name = "survived_proba", inplace = True)
# 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.
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")
In VerticaPy, you have access to various classification curves that can be generated directly from the model. If you opt for this method, please ensure that you specify a test set for accuracy; otherwise, the curve will be based on the training set. Alternatively, you can create these curves using a probability column and a response column.
model.roc_curve()
Creating visualizations using two columns
vmt.roc_curve(y_true = "survived", y_score = "survived_proba_1", input_relation = data)
model.prc_curve()
Creating visualizations using two columns
vmt.prc_curve(y_true = "survived", y_score = "survived_proba_1", input_relation = data)
model.lift_chart()
Creating visualizations using two columns
vmt.lift_chart(y_true = "survived", y_score = "survived_proba_1", input_relation = data)
We can switch to using the highcharts module.
vp.set_option("plotting_lib", "highcharts")
In VerticaPy, you have access to various classification curves that can be generated directly from the model. If you opt for this method, please ensure that you specify a test set for accuracy; otherwise, the curve will be based on the training set. Alternatively, you can create these curves using a probability column and a response column.
model.roc_curve()
Creating visualizations using two columns
vmt.roc_curve(y_true = "survived", y_score = "survived_proba_1", input_relation = data)
model.prc_curve()
Creating visualizations using two columns
vmt.prc_curve(y_true = "survived", y_score = "survived_proba_1", input_relation = data)
model.lift_chart()
Creating visualizations using two columns
vmt.lift_chart(y_true = "survived", y_score = "survived_proba_1", input_relation = data)
We can switch to using the matplotlib module.
vp.set_option("plotting_lib", "matplotlib")
In VerticaPy, you have access to various classification curves that can be generated directly from the model. If you opt for this method, please ensure that you specify a test set for accuracy; otherwise, the curve will be based on the training set. Alternatively, you can create these curves using a probability column and a response column.
model.roc_curve()
Out[2]: <Axes: title={'center': 'ROC Curve'}, xlabel='False Positive Rate (1-Specificity)', ylabel='True Positive Rate (Sensitivity)'>
Creating visualizations using two columns
vmt.roc_curve(y_true = "survived", y_score = "survived_proba_1", input_relation = data)
Out[3]: <Axes: title={'center': 'ROC Curve'}, xlabel='False Positive Rate (1-Specificity)', ylabel='True Positive Rate (Sensitivity)'>
model.prc_curve()
Out[4]: <Axes: title={'center': 'PRC Curve'}, xlabel='Recall', ylabel='Precision'>
Creating visualizations using two columns
vmt.prc_curve(y_true = "survived", y_score = "survived_proba_1", input_relation = data)
Out[5]: <Axes: title={'center': 'PRC Curve'}, xlabel='Recall', ylabel='Precision'>
model.lift_chart()
Out[6]: <Axes: title={'center': 'Lift Table'}, xlabel='Cumulative Data Fraction', ylabel='Values'>
Creating visualizations using two columns
vmt.lift_chart(y_true = "survived", y_score = "survived_proba_1", input_relation = data)
Out[7]: <Axes: title={'center': 'Lift Table'}, xlabel='Cumulative Data Fraction', ylabel='Values'>
Number of Bins¶
Hint
When working with classification charts, you have the flexibility to generate a chart with varying levels of precision by adjusting the number of bins. However, it’s essential to exercise caution because a high number of bins, while potentially providing more detailed results, can significantly impact performance and computational efficiency.
nbins = 10
model.roc_curve(nbins = 10)
nbins = 1000
model.roc_curve(nbins = 1000)
nbins = 10
model.roc_curve(nbins = 10)
nbins = 1000
model.roc_curve(nbins = 1000)
nbins = 10
model.roc_curve(nbins = 10)
Out[8]: <Axes: title={'center': 'ROC Curve'}, xlabel='False Positive Rate (1-Specificity)', ylabel='True Positive Rate (Sensitivity)'>
nbins = 1000
model.roc_curve(nbins = 1000)
Out[9]: <Axes: title={'center': 'ROC Curve'}, xlabel='False Positive Rate (1-Specificity)', ylabel='True Positive Rate (Sensitivity)'>
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
fig = model.roc_curve()
fig.update_traces(marker = dict(color="red"))
Custom colors
model.roc_curve(colors = "red")
Custom colors
model.roc_curve(colors = "red")
Out[10]: <Axes: title={'center': 'ROC Curve'}, xlabel='False Positive Rate (1-Specificity)', ylabel='True Positive Rate (Sensitivity)'>
Size¶
Custom Width and Height
model.roc_curve(width = 300, height = 300)
Custom Width and Height
model.roc_curve(width = 500, height = 200)
Custom Width and Height
model.roc_curve(width = 6, height = 3)
Out[11]: <Axes: title={'center': 'ROC Curve'}, xlabel='False Positive Rate (1-Specificity)', ylabel='True Positive Rate (Sensitivity)'>
Text¶
Custom Title
model.roc_curve().update_layout(title_text = "Custom Title")
Custom Axis Titles
model.roc_curve(yaxis_title = "Custom Y-Axis Title")
Custom Title Text
model.roc_curve(title = {"text": "Custom Title"})
Custom Axis Titles
model.roc_curve(xAxis = {"title": {"text": "Custom X-Axis Title"}})
Custom Title Text
model.roc_curve().set_title("Custom Title")
Out[12]: Text(0.5, 1.0, 'Custom Title')
Custom Axis Titles
model.roc_curve().set_ylabel("Custom Y Axis")
Out[13]: Text(0, 0.5, 'Custom Y Axis')