Clustexts
Last updated: 13th March, 2025
Performs k-means clustering on a collection of texts. It automates the selection of k by running the elbow method implicitly. The algorithm only expects the range of minimum and maximum values for k (default to 2 and 20, respectively).
Texts are encoded using a TFIDF Bag-of-Words representation. Optionally, Truncated Singular Value Decomposition can be used to reduce the dimensionality of the resulting matrix and project the topology onto an embedded space, for improved data compression and schema generalization.
The call to an initialized and fitted instance of this object returns an iterator containing the cluster identifiers associated with each input document.
Dependencies
Ensure you have the following packages installed:
matplotlib==3.10.1
numpy==2.2.3
pandas==2.2.3
scikit-learn==1.6.1
scipy==1.15.2
seaborn==0.13.2
tqdm==4.67.1
Usage
Given the following sample toy dataset:
rows = [
'one text',
'another text',
'this sentence',
'fourth sentence',
'fifth sentence',
]
df = pd.DataFrame(rows, columns=['text'])
an instance of the Clustext class can be instantiated as shown below:
cls = Clustexts(
reducer={},
range = (2, 10),
min_gain=0.001,
vectorizer={'min_df': 0.0}
)
and clustering can then be performed as shown in the line below:
df['cluster'] = cls(df['text'])
Parameters
Clustering
range: Tuple[int, int] = (2, 20): Specifies the minimum and maximum values ofkto explore when applying the elbow method.min_size: int = 0: The minimum cluster size to be accepted. If reached, the clustering stops.min_gain: float = 0.03: The minimum relative improvement for the clustering to continue running (as a percentage of the inertia).
Vectorization (required) & Dimensionality reduction (optional)
Refer to the scikit-learn's documentation for the TfidfVectorizer and the TruncatedSVD classes.
Reporting (optional)
plot_density: bool = False: If set toTrue, the system will plot cluster densities (number of documents in each cluster).plot_k: bool = False: If set toTrue, the algorithm will plot the inertia trendline for everykthat has been explored.show_examples: bool = False: If set toTrue, the algorithm will display 3 examples of each output cluster after the elbow has been found.verbose: bool = False: If set toTrue, prints a message on the terminal specifying the clustering termination condition.
Methods
encode(X: Iterable[str]) -> np.ndarray: Transforms input text X to a numerical vector using TF-IDF Vectorizer, and optionally applies SVD dimensionality reduction.__call__(self, X: Iterable[str]) -> Iterable[int]: fits model on input data X.
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file clustexts-0.0.2.tar.gz.
File metadata
- Download URL: clustexts-0.0.2.tar.gz
- Upload date:
- Size: 5.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via:
twine/6.1.0 CPython/3.11.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8257f42d7738bbbd774f35444bafe0f76b1a4542079796895b6b4d2a30a2c4f3
|
|
| MD5 |
be10764450f4b91c1f08bf0b4294dead
|
|
| BLAKE2b-256 |
2fe3bb45fc42fa3f6aa83c7b2aeef906a782b4216bf9059a92ac742e0a02db16
|
File details
Details for the file clustexts-0.0.2-py3-none-any.whl.
File metadata
- Download URL: clustexts-0.0.2-py3-none-any.whl
- Upload date:
- Size: 6.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via:
twine/6.1.0 CPython/3.11.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0d0e66cce81fd5a811d29b46d4bf5d827a9428126a6685eb14868a585b767c47
|
|
| MD5 |
5353a31fc534047c6bceb4b346f3e547
|
|
| BLAKE2b-256 |
1816c5a0f826643214c213b57bbb472fe3a5aa66085fb20619eae62f4c8fe3aa
|