Python Library
Create counterfactual records, draw PCA correlation graphs and decision boundaries, perform bias-variance decomposition, bootstrapping, and much more

MLxtend library (Machine Learning extensions) has many interesting functions for everyday Data Analysis and machine learning tasks. Although there are many machine learning libraries available for Python such as scikit-learn, TensorFlow, Keras, PyTorch, etc, however, MLxtend offers additional functionalities and can be a valuable addition to your data science toolbox.
In this post, I will go over several tools of the library, in particular, I will cover:
- Create counterfactual (for model interpretability)
- PCA correlation circle
- Bias-variance decomposition
- Decision regions of classification models
- Matrix of scatter plots
- Bootstrapping
A link to a free one-page summary of this post is available at the end of the article.
For a list of all functionalities this library offers, you can visit MLxtend’s documentation [1].
MLxtend Library
MLxtend library is developed by Sebastian Raschka (a professor of statistics at the University of Wisconsin-Madison). The library has nice API documentation as well as many examples.
You can install the MLxtend package through the Python Package Index (PyPi) by running pip install mlxtend
.
Dataset
In this post, I’m using the wine data set obtained from the Kaggle. The data contains 13 attributes of alcohol for three types of wine. This is a multiclass classification dataset, and you can find the description of the dataset here.
First, let’s import the data and prepare the input variables X (feature set) and the output variable y (target).
MLxtend Functionalities
Create Counterfactual (for model interpretability)
For creating counterfactual records (in the context of machine learning), we need to modify the features of some records from the training set in order to change the model prediction [2]. This may be helpful in explaining the behavior of a trained model. The algorithm used in the library to create counterfactual records is developed by Wachter et al [3].
You can create counterfactual records using _createcounterfactual() from the library. Note that this implementation works with any scikit-learn estimator that supports the predict()
function. Below is an example of creating a counterfactual record for an ML model. The counterfactual record is highlighted in a red dot within the classifier’s decision regions (we will go over how to draw decision regions of classifiers later in the post).

PCA Correlation Circle
An interesting and different way to look at PCA results is through a correlation circle that can be plotted using _plot_pca_correlation_graph()_. We basically compute the correlation between the original dataset columns and the PCs (principal components). Then, these correlations are plotted as vectors on a unit-circle. The axes of the circle are the selected dimensions (a.k.a. PCs). You can specify the PCs you’re interested in by passing them as a tuple to dimensions
function argument. The correlation circle axes labels show the percentage of the explained variance for the corresponding PC [1].
Remember that the normalization is important in PCA because the PCA projects the original data on to the directions that maximize the variance.


Bias-Variance Decomposition
You often hear about the bias-variance tradeoff to show the model performance. In supervised learning, the goal often is to minimize both the bias error (to prevent underfitting) and variance (to prevent overfitting) so that our model can generalize beyond the training set [4]. This process is known as a bias-variance tradeoff.
Note that we cannot calculate the actual bias and variance for a predictive model, and the bias-variance tradeoff is a concept that an ML engineer should always consider and tries to find a sweet spot between the two. Having said that, we can still study the model’s expected generalization error for certain problems. In particular, we can use the bias-variance decomposition to decompose the generalization error into a sum of 1) bias, 2) variance, and 3) irreducible error [4, 5].
The bias-variance decomposition can be implemented through _bias_variance_decomp()_ in the library. An example of such implementation for a decision tree classifier is given below.
>>> Average expected loss: 0.108
>>> Average bias: 0.032
>>> Average variance: 0.076
Plotting Decision Regions of Classifiers
MLxtend library has an out-of-the-box function _plot_decision_regions()_ to draw a classifier’s decision regions in 1 or 2 dimensions.
Here, I will draw decision regions for several scikit-learn as well as MLxtend models. Let’s first import the models and initialize them.
Now that we have initialized all the classifiers, let’s train the models and draw decision boundaries using _plot_decision_regions()_ from the MLxtend library.

Matrix of Scatter Plots
Another useful tool from MLxtend is the ability to draw a matrix of scatter plots for features (using _scatterplotmatrix()_). In order to add another dimension to the scatter plots, we can also assign different colors for different target classes.

By the way, for plotting similar scatter plots, you can also use Pandas’ _scatter_matrix() or seaborn’s pairplot()_ function.
Bootstrapping
The bootstrap is an easy way to estimate a sample statistic and generate the corresponding confidence interval by drawing random samples with replacement. For this, you can use the function _bootstrap()_ from the library. Note that you can pass a custom statistic to the bootstrap function through argument func
. The custom function must return a scalar value.
>>> Mean: 5.03
>>> Standard Error: +/- 0.11
>>> CI95: [4.8, 5.26]
Conclusion
In this post, we went over several MLxtend library functionalities, in particular, we talked about creating counterfactual instances for better model interpretability and plotting decision regions for classifiers, drawing PCA correlation circle, analyzing bias-variance tradeoff through decomposition, drawing a matrix of scatter plots of features with colored targets, and implementing the bootstrapping. The library is a nice addition to your Data Science toolbox, and I recommend giving this library a try.
You can find the Jupyter notebook for this blog post on GitHub.
You can download the one-page summary of this post at https://ealizadeh.com.
Thanks for reading! 📚
I’m a senior data scientist 📊 and engineer, writing about statistics, Machine Learning, Python, and more.
🌱 I also curate a weekly newsletter called AI Sprout where I provide hands-on reviews and analysis of the latest AI tools and innovations. Subscribe to explore emerging AI with me!
- Follow me on Medium 👋 to get my latest post
- Subscribe to my mailing list ✉️ for updates right to your inbox
- _Let’s connect on LinkedIn and Twitter 🤝_
References
[1] Sebastian Raschka, MLxtend
[2] Sebastian Raschka, Create Counterfactual, MLxtend API documentation
[3] S. Wachter et al (2018), Counterfactual Explanations without Opening the Black Box: Automated Decisions and the GDPR, 31(2), Harvard Journal of Law & Technology
[4] Wikipedia, Bias-variance tradeoff
[5] Sebastian Raschka, Bias-Variance Decomposition, MLxtend API documentation
Originally published at https://www.ealizadeh.com.