Abstract
Modern text classification is dominated by massive deep neural networks (DNNs) like BERT, which require significant computational resources, billions of parameters, and extensive fine-tuning. This paper explores a radically simpler alternative: combining the gzip compression algorithm with a k-nearest-neighbor (kNN) classifier. This parameter-free approach requires zero training and demonstrates remarkable robustness, particularly in few-shot and out-of-distribution (OOD) scenarios where traditional models often falter. Our results show that this "less is more" method can compete with, and sometimes outperform, state-of-the-art deep learning models across diverse languages and domains.
Introduction
Deep Neural Networks (DNNs) have become the default for text classification tasks. However, their success comes with significant "baggage": they are computationally expensive, require massive amounts of labeled data, and often lack robustness when faced with out-of-distribution (OOD) data—information that differs from their training set. This paper addresses a fundamental question: Is all this complexity necessary for simple text classification?
The authors propose a lightweight, universal alternative that treats text as a raw byte stream. By leveraging the principles of information theory, they demonstrate that a non-parametric method can provide a powerful baseline that works across different languages without the overhead of modern NLP pipelines.
Methodology
The core of the proposed method is the Normalized Compression Distance (NCD), which uses lossless compressors to approximate similarity.
- The Intuition: Lossless compressors like gzip work by identifying patterns and regularities. If two pieces of text belong to the same category, they likely share more patterns than texts from different categories.
-
The Metric: To quantify this, the authors use NCD, which approximates Kolmogorov complexity (the length of the shortest possible program that can generate a string). The formula is: $$NCD(x, y) = \frac{C(xy) - \min\{C(x), C(y)\}}{\max\{C(x), C(y)\}}$$ Where $C(x)$ is the compressed length of string $x$, and $C(xy)$ is the compressed length of the concatenation of $x$ and $y$. If $x$ and $y$ are similar, the compressor can "reuse" patterns from the first string to encode the second, keeping $C(xy)$ small.
-
The Classifier: To classify a new document, the algorithm calculates the NCD between the test document and every document in the training set, then assigns a label based on the $k$ most similar neighbors.
This entire approach can be implemented in just 15 lines of Python code and requires no tokenization, stop-word removal, or embedding generation.
Key Results and Insights
Few-Shot and OOD Performance
One of the most striking findings is the method's performance in "few-shot" settings. While massive models like BERT struggle to adapt their weight space to a tiny sample (e.g., 5 labeled examples), gzip’s simple similarity search remains highly effective.
In OOD scenarios, gzip outperformed BERT on all five tested datasets, including low-resource languages like Swahili and Kinyarwanda. Because gzip is non-parametric and doesn't "learn" a specific distribution, it is inherently more flexible when encountering new languages or niche domains that were likely unseen during the pre-training of large models.
Compressors as Feature Extractors
Gzip acts as a universal feature extractor. It does not need to "understand" semantics; it simply finds statistical regularity. The effectiveness of this approach is tied to how well the data can be compressed.
Limitations and Future Work
Despite its strengths, the method has notable trade-offs:
- Inference Latency: While there is no training time, inference is $O(N \times M)$, requiring a distance calculation against every training sample. For massive datasets, this can take days on standard CPUs.
- Sliding Window Limits: Gzip uses a 32KB sliding window. If the similarity between two documents is separated by more than 32KB, the compressor may fail to capture the relationship.
- Vocabulary Sensitivity: Performance is slightly lower on datasets with extremely large and diverse vocabularies, such as YahooAnswers.
Future research may explore "neural compressors" to combine the pattern recognition of deep learning with the robust NCD framework for improved efficiency and accuracy.
Summary updated