Package: counterplots
CounterPlots: Plotting tool for counterfactualsCounterplots is a Python package that allows you to plot counterfactuals with easy integration with any counterfactual generation algorithm.Plot examplesGreedy PlotThe greedy plot shows the greediest (feature change with the highest impact towards the opposite class) path from the factual instance until it reaches the counterfactual.CounterShapley PlotThis chart shows each counterfactual feature change contribution to the counterfactual prediction. It uses Shapley values to calculate the contribution of each feature change.Constellation PlotThis chart shows the prediction score change for all possible feature change combinations.RequirementsCounterPlots requires Python 3.8 or higher.InstallationWith pip:pipinstallcounterplotsUsageTo use CounterPlots, you just need the machine learning model predictor, and the factual and counterfactual points.
The example below uses a simple mock model:fromcounterplotsimportCreatePlotimportnumpyasnp# Simple mock model for the predict_proba function which returns a probability for each input instancedefmock_predict_proba(data):out=[]forxindata:iflist(x)==[0.0,0.0,0.0]:out.append(0.0)eliflist(x)==[1.0,0.0,0.0]:out.append(0.44)eliflist(x)==[0.0,1.0,0.0]:out.append(0.4)eliflist(x)==[0.0,0.0,1.0]:out.append(0.2)eliflist(x)==[1.0,1.0,0.0]:out.append(0.3)eliflist(x)==[0.0,1.0,1.0]:out.append(0.25)eliflist(x)==[1.0,0.0,1.0]:out.append(0.4)eliflist(x)==[1.0,1.0,1.0]:out.append(1.0)returnnp.array(out)# Factual Instancefactual=np.array([0,0,0])# Counterfactual Instancecf=np.array([1,1,1])# Create the plot objectcf_plots=CreatePlot(factual,cf,mock_predict_proba)# Create the greedy plotcf_plots.greedy('greedy_plot.png')# Create the countershapley plotcf_plots.countershapley('countershapley_plot.png')# Create the constellation plotcf_plots.constellation('constellation_plot.png')# Print the countershapley valuesprint(cf_plots.countershapley_values())In case you want to add custom names to the features, use the optional argumentfeature_names:cf_plots=CreatePlot(factual,cf,mock_predict_proba,feature_names=['feature1','feature2','feature3'])In case you want to add custom labels to the factual and counterfactual points, use the optional argumentclass_names:cf_plots=CreatePlot(factual,cf,mock_predict_proba,class_names=['Factual','Counterfactual'])Using with Scikit-LearnCounterPlots can be used with any machine learning model that has apredict_probafunction. For example, with Scikit-Learn:importnumpyasnpfromsklearn.ensembleimportRandomForestClassifierfromsklearn.datasetsimportload_irisfromcounterplotsimportCreatePlotiris=load_iris()X=iris.datay=[0ifl==0else1forliniris.target]# Makes it a binary classification problemclf=RandomForestClassifier(max_depth=2,random_state=0)clf.fit(X,y)preds=clf.predict(X)# For the factual point, takes an instance with 0 classificationfactual=X[np.argwhere(preds==0)[0]][0]# For the counterfactual point, takes an instance with 1 classificationcf=X[np.argwhere(preds==1)[0]][0]cf_plots=CreatePlot(factual,cf,clf.predict_proba,feature_names=iris.feature_names,class_names={0:'Setosa',1:'Non-Setosa'})# Create the greedy plotcf_plots.greedy('iris_greedy_plot.png')# Create the countershapley plotcf_plots.countershapley('iris_countershapley_plot.png')# Create the constellation plotcf_plots.constellation('iris_constellation_plot.png')# Print the countershapley valuesprint(cf_plots.countershapley_values())CitationIf you use CounterPlots in your research, please cite the following paper: