Classify news articles into different categories using Machine Learning. The dataset consists of 6000 documents and 47 categories.
My goal is to show you how to create a predictive model(s) that will classification labels for news articles.
Full documentation: ml-classifier.readthedocs.io.
- To classify news articles
- Learn the basics of natural language processing
- Build models using sklearn and choose the best one
- Use sklearn's make_pipeline class
- Learn how to turn it into a service
- Learn how to make it composable and portable
- ...
- Profit?
- Python >= v3.13
- uv
- Jupyter Notebook
- Some knowledge of Machine Learning
- NumPy
- Pandas
- SciPy
- Matplotlib
- Jupyter
- Scikit-learn (the library that we will use later in this post when creating the classifier model(s))
- Apply some preprocessing steps to prepare the data.
- We will perform a descriptive analysis of the data to better understand the main characteristics that they have
- We will continue by practicing how to train different machine learning models using scikit-learn. It is one of the most popular python libraries for machine learning
- We will also use a subset of the dataset for training purposes
- We will iterate and evaluate the learned models by using unseen data. Later, we will compare them until we find a good models that meets our expectations, and use a
VotingClassifiersoft voting for unfitted estimators. - Once we have chosen the candidate model(s), we will use it to perform predictions and to create a simple web application that consumes this predictive model
See HowTo Jupyter Notebook
As a container:
docker run -d -p 7070:7070 docker.io/saidsef/ml-classifier:latestAs a Python application, once the model is in data/, see running the service:
uv sync
uv run classifier-ml.pyOn Kubernetes, see deploying on Kubernetes:
kubectl apply -k ./deploymentThe request must be a POST with a JSON payload:
{ "body": "text-goes-here" }curl -XPOST http://localhost:7070/api/v1/news -H 'Content-Type: application/json' -d @test/test.jsonThe response names the category and its probability:
{
"category": "World",
"score": 0.126
}Published at ml-classifier.readthedocs.io, with the source in docs/.
- Architecture - how the notebook, the image and the cluster fit together
- Training the model - the dataset, the pipeline, and how to run the notebook
- Running the service - the container, a source checkout, and configuration
- API reference - every endpoint, with payloads and responses
- Deploying on Kubernetes - the manifests, the overlays, and what to change in each
- Operations - health checks, metrics, and what the common failures mean
- CI and releases - what CI builds and tests, the image tags, and how releases are cut