Class Prediction Error¶
ClassPredictionError plot is a twist on other and sometimes more familiar classification model diagnostic tools like the Confusion Matrix and Classification Report. Like the Classification Report, this plot shows the support (number of training samples) for each class in the fitted classification model as a stacked bar chart. Each bar is segmented to show the proportion of predictions (including false negatives and false positives, like a Confusion Matrix) for each class. You can use a
ClassPredictionError to visualize which classes your classifier is having a particularly difficult time with, and more importantly, what incorrect answers it is giving on a per-class basis. This can often enable you to better understand strengths and weaknesses of different models and particular challenges unique to your dataset.
The class prediction error chart provides a way to quickly understand how good your classifier is at predicting the right classes.
from sklearn.datasets import make_classification from sklearn.model_selection import train_test_split from sklearn.ensemble import RandomForestClassifier from yellowbrick.classifier import ClassPredictionError # Create classification dataset X, y = make_classification( n_samples=1000, n_classes=5, n_informative=3, n_clusters_per_class=1, random_state=36, ) classes = ["apple", "kiwi", "pear", "banana", "orange"] # Perform 80/20 training/test split X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.20, random_state=42) # Instantiate the classification model and visualizer visualizer = ClassPredictionError( RandomForestClassifier(random_state=42, n_estimators=10), classes=classes ) # Fit the training data to the visualizer visualizer.fit(X_train, y_train) # Evaluate the model on the test data visualizer.score(X_test, y_test) # Draw visualization visualizer.show()
In the above example, while the
RandomForestClassifier appears to be fairly good at correctly predicting apples based on the features of the fruit, it often incorrectly labels pears as kiwis and mistakes kiwis for bananas.
By contrast, in the following example, the
RandomForestClassifier does a great job at correctly predicting accounts in default, but it is a bit of a coin toss in predicting account holders who stayed current on bills.
from sklearn.model_selection import train_test_split from sklearn.ensemble import RandomForestClassifier from yellowbrick.classifier import ClassPredictionError from yellowbrick.datasets import load_credit X, y = load_credit() classes = ['account in default', 'current with bills'] # Perform 80/20 training/test split X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.20, random_state=42) # Instantiate the classification model and visualizer visualizer = ClassPredictionError( RandomForestClassifier(n_estimators=10), classes=classes ) # Fit the training data to the visualizer visualizer.fit(X_train, y_train) # Evaluate the model on the test data visualizer.score(X_test, y_test) # Draw visualization visualizer.show()
Shows the balance of classes and their associated predictions.
ClassPredictionError(model, ax=None, classes=None, encoder=None, is_fitted='auto', force_model=False, **kwargs)¶
Class Prediction Error chart that shows the support for each class in the fitted classification model displayed as a stacked bar. Each bar is segmented to show the distribution of predicted classes for each class. It is initialized with a fitted model and generates a class prediction error chart on draw.
A scikit-learn estimator that should be a classifier. If the model is not a classifier, an exception is raised. If the internal model is not fitted, it is fit when the visualizer is fitted, unless otherwise specified by
- axmatplotlib Axes, default: None
The axes to plot the figure on. If not specified the current axes will be used (or generated if required).
- classeslist of str, defult: None
The class labels to use for the legend ordered by the index of the sorted classes discovered in the
fit()method. Specifying classes in this manner is used to change the class names to a more specific format or to label encoded integer classes. Some visualizers may also use this field to filter the visualization for specific classes. For more advanced usage specify an encoder rather than class labels.
- encoderdict or LabelEncoder, default: None
A mapping of classes to human readable labels. Often there is a mismatch between desired class labels and those contained in the target variable passed to
score(). The encoder disambiguates this mismatch ensuring that classes are labeled correctly in the visualization.
- is_fittedbool or str, default=”auto”
Specify if the wrapped estimator is already fitted. If False, the estimator will be fit when the visualizer is fit, otherwise, the estimator will not be modified. If “auto” (default), a helper method will check if the estimator is fitted before fitting it again.
- force_modelbool, default: False
Do not check to ensure that the underlying estimator is a classifier. This will prevent an exception when the visualizer is initialized but may result in unexpected or unintended behavior.
Keyword arguments passed to the visualizer base classes.
- classes_ndarray of shape (n_classes,)
The class labels observed while fitting.
- class_count_ndarray of shape (n_classes,)
Number of samples encountered for each class during fitting.
An evaluation metric of the classifier on test data produced when
score()is called. This metric is between 0 and 1 – higher scores are generally better. For classifiers, this score is usually accuracy, but ensure you check the underlying model for more details about the score.
An ndarray of predictions whose rows are the true classes and whose columns are the predicted classes
Renders the class prediction error across the axis.
- axMatplotlib Axes
The axes on which the figure is plotted
Adds a title and axis labels to the visualizer, ensuring that the y limits zoom the visualization in to the area of interest. Finalize also calls tight layout to ensure that no parts of the figure are cut off.
Generally this method is called from show and not directly by the user.
score(self, X, y)¶
Generates a 2D array where each row is the count of the predicted classes and each column is the true class
- Xndarray or DataFrame of shape n x m
A matrix of n instances with m features
- yndarray or Series of length n
An array or series of target or class values
Global accuracy score