Unlocking the power of active learning with modAL: A beginner’s guide

Chang In Moon Chang In Moon #python#machine-learning

Before getting started, you will need to install the modAL library by running the following command:

Active learning is a powerful approach for improving the performance of machine learning models by selectively labeling the most informative samples. In this tutorial, we will show you how to use the modAL library in Python to implement active learning.

modAL is an open-source active learning library for Python, designed to make active learning easy and accessible to everyone. It is built on top of scikit-learn, so it can be used with any classifier that is compatible with scikit-learn.

Before getting started, you will need to install the modAL library by running the following command:

pip install modAL

Once the library is installed, you can start using it to implement active learning in your projects. Here is an example of how to use modAL to perform active learning with a simple logistic regression classifier:

from sklearn.datasets import make_classification
from sklearn.linear_model import LogisticRegression
from modAL.models import ActiveLearner
# generate some data
X, y = make_classification(n_classes=2, n_samples=1000, n_features=20, n_informative=10, n_redundant=5, n_repeated=5, random_state=42)
# initialize the classifier
clf = LogisticRegression()
# initialize the active learner
learner = ActiveLearner(estimator=clf, X_training=X[:200], y_training=y[:200])
# query for labels
query_idx, query_inst = learner.query(X[200:])
# provide the labels
learner.teach(X[query_idx], y[query_idx])
# evaluate the performance
accuracy = learner.score(X[200:], y[200:])
print('Accuracy: {:.3f}'.format(accuracy))

In this example, we first generate a dataset using the make_classification function from scikit-learn. We then initialize a logistic regression classifier, which we will use as the base estimator for our active learner. Next, we initialize an ActiveLearner object and provide it with the classifier and the initial training data (X[], y[]).

We then use the query method to select the next sample to be labeled. The method returns the index of the sample and the instance itself, which we use to provide the labels using the teach method. Finally, we evaluate the performance of the active learner by calculating the accuracy of its predictions on the remaining samples.

In this example, we used random sampling to select the next sample to be labeled. However, modAL provides several uncertainty sampling methods such as entropy-based, least confidence, and margin-based sampling, which can be used to select the most informative samples for labeling. To use any of these methods, you can pass the sampling method of your choice to the query method of the active learner.

query_idx, query_inst = learner.query(X[200:], n_instances=1, method='margin_sampling')

The modAL library also provides a variety of other useful features, such as the ability to query multiple instances at a time, to query instances in batches, and to use different acquisition functions. You can find more information and examples in the modAL documentation. Additionally, modAL also allows you to query for labels from human annotators or other external sources through the modAL.uncertainty.uncertainty_sampling.UncertaintySampler class.

In conclusion, modAL is a powerful and easy-to-use library for implementing active learning in Python. It provides a variety of uncertainty sampling methods, as well as other useful features, making it a great choice for any project that requires active learning. With its seamless integration with scikit-learn, it is easy to use, allowing you to focus on the problem at hand, instead of the implementation details. I hope this tutorial has provided you with a good starting point for using modAL in your own projects.

Resource: https://modal-python.readthedocs.io/en/latest/

Comments