Fast TF-IDF
A blazingly fast TF-IDF implementation for Python that uses multiprocessing to parallelise document processing.
Key Features:
- orders of magnitude faster than TensorFlow's TextVectorization (see benchmarks below)
- tensorflow-compatible vocabulary and idf weights can be used with TensorFlow's TextVectorization, it just computes them much quicker
- it's much more memory efficient too, consuming less than half the memory required by TextVectorization for the same dataset in my tests
Installation
# Using pip
pip install fast-tfidf
# Or install from source
git clone https://github.com/npil/fast-tfidf.git
cd fast-tfidf
pip install -e .
Usage
This package provides vocabulary extraction and IDF weight calculation that can be used standalone or integrated with other libraries.
Basic Example
from fast_tfidf import get_vocabulary_and_idf_weights
# Your text documents
documents = [
"machine learning is amazing",
"deep learning and machine learning",
"artificial intelligence and deep learning"
]
# Extract vocabulary and IDF weights
# Get top 1000 terms (unigrams only)
vocabulary, idf_weights = get_vocabulary_and_idf_weights(
documents,
n_features=1000,
remove_stopwords=False,
use_bigrams=False
)
print(f"Vocabulary size: {len(vocabulary)}")
print(f"Top terms: {vocabulary[:5]}")
print(f"IDF weights: {idf_weights[:5]}")
With Bigrams
# Include bigrams for richer representation
vocabulary, idf_weights = get_vocabulary_and_idf_weights(
documents,
n_features=2000,
use_bigrams=True # Enable bigrams
)
With Stopword Removal
# Remove common English stopwords
vocabulary, idf_weights = get_vocabulary_and_idf_weights(
documents,
n_features=1000,
remove_stopwords=True # Filter out stopwords
)
Integration with TensorFlow
from tensorflow.keras.layers import TextVectorization
from fast_tfidf import get_vocabulary_and_idf_weights
vocabulary, idf_weights = get_vocabulary_and_idf_weights(documents, n_features=2000)
vectoriser = TextVectorization(
vocabulary=vocabulary,
idf_weights=idf_weights,
output_mode='tf_idf'
)
# No adapt() needed - use pre-computed weights! 🚀
# Your model can now use 'vectoriser' for TF-IDF transformation
Note: Although fast-tf-idf uses a different IDF formula than TensorFlow's default log((1 + n_docs) / (1 + df)) + 1 vs TensorFlow's internal formula), this only affects absolute values while preserving relative term importance.
Performance
Benchmark results comparing against TensorFlow's TextVectorization:
| Documents | TensorFlow | fast-tfidf | Speedup |
|---|---|---|---|
| 100 | 204 ms | 29 ms | 7.1x ⚡ |
| 1,000 | 1.06 s | 28 ms | 38.3x ⚡⚡ |
| 5,000 | 3.00 s | 43 ms | 70.4x ⚡⚡⚡ |
| 10,000 | 7.51 s | 77 ms | 97.0x ⚡⚡⚡⚡ |
See benchmarks/ directory for detailed results and comparison scripts.
Development
Dependencies are managed using poetry.
There is a vscode dev container that can be used for development, or otherwise you can run make install in the environment of your choice to install the package and all dependencies (requires poetry and make).
We use the following tools to ensure code quality:
You can run all formatting / linting / type checks using make lint (you can use make fix to automatically fix some formatting errors).
You can run unittests using make test.
You can run linting and unittests using make lint-test.
Metadata
Release files for fast-tfidf 0.2.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| fast_tfidf-0.2.0.tar.gz | 4.0 kB | With provenance | Details for fast_tfidf-0.2.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| fast_tfidf-0.2.0-py3-none-any.whl | Python 3 | none | any | Details for fast_tfidf-0.2.0-py3-none-any.whl |
Total release size: 8.1 kB
Release files / fast_tfidf-0.2.0.tar.gz
| Download URL | fast_tfidf-0.2.0.tar.gz |
|---|---|
| Size | 4.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
895ffea9ffa730db08edcfbc8c0ea4161b879d5586938a011f367e4820b4dd44
|
|
BLAKE2b-256 checksum How to use checksums |
7f2cd53d7ab6b220584959e1733035ac6a6123ba2aea0a45fe54bd0170b50c96
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.12
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on May 15, 2026.
Transparency logRelease files / fast_tfidf-0.2.0-py3-none-any.whl
| Download URL | fast_tfidf-0.2.0-py3-none-any.whl |
|---|---|
| Size | 4.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
44f4255a28ef6dc319b85059e4cf2d15b73dbc1e6c529aedd69492a7db273e1b
|
|
BLAKE2b-256 checksum How to use checksums |
03bb557333b7bd963578f810f34adb419d6f5292025c13615c8b2216389f1bed
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.12
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on May 15, 2026.
Transparency logSecurity for fast-tfidf 0.2.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
Full provenance
Both files in this release have been uploaded with provenance metadata.
Attestation for fast_tfidf-0.2.0.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on May 15, 2026.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.