In machine learning procedures, visualization is an important step that helps data scientists to realize and understand how the procedure is going. While talking about EDA we find various libraries and tools that help in understanding the data but when it comes to visualizing the results and performance of the model we may need to use long-size codes and sometimes it becomes difficult. Scikit-plot is a library that is designed to plot the results and performances of the model. In this article, we are going to discuss the Scikit-plot library. The major points to be discussed in the article are listed below.
Table of contents
- What is Scikit-plot?
- Different modules of Scikit–Plot
- Metrics module
- Estimators module
- Cluster module
- Decomposition module
Let's start with understanding what Scikit-plot is.
What is Scikit-plot?
As the name suggests Scikit-plot is a package that helps us in the visualization process. Since visualization plays a crucial role in the life of data science processes we always need some libraries that can help us in making meaningful visualizations. visualization is not only meant for the process of data science but we can find its utilization in every data-related field. Using this library we can plot our model very quickly and beautifully.
The reason I find this library useful is that we can easily draw the performance of the models in an organized way. In this library we mainly find four types of modules:
- Metrics modules - these modules help in plotting the machine learning evaluation metrics for example confusion matrix, silhouette scores, etc.
- Estimators modules - these modules help in plotting the scikit-learn estimator instances for example random forest and support vector machines.
- Cluster module- using these modules we can plot cluster instances from scikit-learn for example means.
- Decomposition module- these modules are designed to plot the estimators from scikit learn that helps in performing dimensionality reduction.
We can install scikit-plot in our environment using the following lines of codes.
!pip install scikit-plot
Are you looking for for a complete repository of Python libraries used in data science, check out here.
Different modules of Scikit–Plot
After installation, we are ready to use this library. In this article, we see an example for every category of modules presented in the library.
Let's start with the metrics modules.
Metrics module
In this section, we will try to make a plot for the model's results using the cumulative gain. For this, we are required to define a model and fit it on some data. Using the below code we can perform this.
from sklearn.ensemble import RandomForestClassifier
from sklearn.datasets import load_breast_cancer
from sklearn.datasets import load_iris
X, y = load_breast_cancer(return_X_y=True)
rf = RandomForestClassifier()
rf.fit(X, y)
Output:

Let's predict using the model
probas = rf.predict_proba(X)
Now, we are ready to use the scikit-plot for plotting the cumulative gain using the metrics. Cumulative gain helps in measuring the ranking quality. Let's plot it for our model.
import matplotlib.pyplot as plt
import scikitplot as skplt
skplt.metrics.plot_cumulative_gain(y_true=y, y_probas=probas)
plt.show()
Output:
Here we can see that we are required to add just one or two lines of codes on standard codes to visualize the results from the model.
Estimators module
These modules are basically to estimate the results from the model. For example, we can look at the feature importance from the data set or we can draw a learning curve to know how the model improved with data.
For the above-given model, we can draw a learning curve using the following line of codes.
import scikitplot.estimators as esti
import matplotlib.pyplot as plt
esti.plot_learning_curve(rf, X, y)
plt.show()
Output:

We can find more functions of this module here.
Cluster module
These modules help in visualizing the results from the scikit learn cluster models. We can also use cluster models from outside, but these modules have some specific functions for models from scikit-learn. In this section, we will look at how we can plot the elbow curves for the clusters defined by scikit-learn’s K-means cluster on the iris data set. Let's define the model.
from sklearn.cluster import KMeans
from sklearn.datasets import load_iris as load_data
X, y = load_data(return_X_y=True)
kmeans = KMeans(random_state=1)
Let's plot the elbow curve
import scikitplot as skplt
import matplotlib.pyplot as plt
%pylab inline
pylab.rcParams['figure.figsize'] = (14, 14)
skplt.cluster.plot_elbow_curve(kmeans, X, cluster_ranges=range(1, 11))
plt.show()
Output:

Here we can see the elbow curve for our K-means clustering on iris data where it has generated 10 clusters in the data. More details about the modules and functions inside it can be found here.
Decomposition module
These modules include functions for plotting the models used for dimensionality reduction. Let’s see how we can draw a plot for a component variance for PCA on the iris data set.
Making the PCA model
from sklearn.decomposition import PCA
pca = PCA(random_state=1)
pca.fit(X)
Plotting the PCA component variance.
skplt.decomposition.plot_pca_component_variance(pca)
plt.show()
Output:

Here we can see how we can use these modules for plotting component variance of PCA. We can also plot the data points in 2D projection using this module. More details about the module can be found here.
Final words
In this article, we have discussed the scikit-plot library that is designed to plot the model results. Along with this, we have looked at some of the examples that can be utilized for explaining the results of the model.


Your reaction