holisticai.robustness.plots.plot_label_and_prediction#
- holisticai.robustness.plots.plot_label_and_prediction(X, y, y_pred, vertical_offset=0.1, features_to_plot=None)[source]#
Plots a 2D scatter plot of a dataset, displaying both the true labels (y) and the predicted labels (y_pred) with a slight vertical offset for distinction.
This function generates a scatter plot where each point represents a sample from the dataset. The true labels are shown in a darker shade, while the predicted labels are displayed with a vertical offset to distinguish them. The axes can be labeled using feature names provided by the user or inferred from the data.
Parameters
- Xnp.ndarray or pd.DataFrame
The feature matrix where each row represents a sample and each column represents a feature. This can be either a NumPy array or a pandas DataFrame.
- ynp.ndarray or pd.Series
The true labels for each sample. This can be a one-dimensional NumPy array or a pandas Series.
- y_prednp.ndarray or pd.Series
The predicted labels for each sample. This can be a one-dimensional NumPy array or a pandas Series.
- vertical_offsetfloat, optional (default=0.1)
The vertical offset applied to the predicted labels on the plot to distinguish them from the true labels.
- features_to_plotlist, optional
A list of feature names (strings) to label the x and y axes of the plot. If not provided, the function will infer the names of X and y from the argument names.
Returns
- None
This function does not return any value. It displays the scatter plot with true labels and predicted labels.
Example
>>> import numpy as np >>> import pandas as pd >>> from matplotlib import pyplot as plt >>> >>> # Example dataset >>> X = pd.DataFrame( ... {"Feature1": np.random.rand(100), "Feature2": np.random.rand(100)} ... ) >>> y = pd.Series(np.random.randint(0, 2, size=100)) >>> y_pred = pd.Series(np.random.randint(0, 2, size=100)) >>> >>> # Plot with labels and predictions >>> plot_label_and_prediction( ... X, y, y_pred, vertical_offset=0.1, features_to_plot=["Feature1", "Feature2"] ... ) This will display a 2D scatter plot with both the true labels and the predicted labels, where the predicted labels are slightly offset for clarity.
Scatter Plot of a 2D dataset with y_test and y_pred together in the same graph. The predicted values (y_pred, shaded circles) are shifted vertically by a small amount to allow better visualization. The plot highlights areas where the classifier incorrectly predicted the true labels, evident by differing colors between y_test and y_pred.