#!/usr/bin/env python # coding: utf-8 # # Class Prediction Error Visualizer # The `ClassPredictionError` visualizer is a ScoreVisualizer that takes a fitted scikit-learn classifier and a set of test X and y values and returns a stacked bar graph showing a color-coded break down of predicted classes compared to their actual classes. This visualizer provides a way to quickly understand how good your classifier is at predicting the right classes. # # Below is an example with the visualizer # In[1]: import matplotlib import matplotlib.pyplot as plt get_ipython().run_line_magic('matplotlib', 'inline') from yellowbrick.classifier import ClassPredictionError from sklearn.datasets import make_classification from sklearn.model_selection import train_test_split as tts from sklearn.ensemble import RandomForestClassifier # In[2]: # We create our own classification dataset # The data set contains 5 classess and 1000 samples # We use RandomForest for modeling the data # I came up with arbitrary names for the classes X, y = make_classification(n_samples=1000, n_classes=5, n_informative=3, n_clusters_per_class=1) # Perform 80/20 training/test split X_train, X_test, y_train, y_test = tts(X, y, test_size=0.20, random_state=42) # Pass in model and classes to ClassPredictionError visualizer = ClassPredictionError(RandomForestClassifier(), classes=['apple', 'kiwi', 'pear', 'banana', 'orange']) # Fit the model visualizer.fit(X_train, y_train) # Use test data to create visualization visualizer.score(X_test, y_test) # Display visualization visualizer.show() plt.show() # In[ ]: