Skip to main content

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 of k to 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 to True, the system will plot cluster densities (number of documents in each cluster).
  • plot_k: bool = False: If set to True, the algorithm will plot the inertia trendline for every k that has been explored.
  • show_examples: bool = False: If set to True, the algorithm will display 3 examples of each output cluster after the elbow has been found.
  • verbose: bool = False: If set to True, 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.

Metadata

Release files for clustexts 0.0.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for clustexts 0.0.2
File Size Uploaded Provenance Actions
clustexts-0.0.2.tar.gz 5.8 kB Without provenance Details for clustexts-0.0.2.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for clustexts 0.0.2
File Interpreter ABI Platform Actions
clustexts-0.0.2-py3-none-any.whl Python 3 none any Details for clustexts-0.0.2-py3-none-any.whl

Total release size: 12.1 kB

Release files / clustexts-0.0.2.tar.gz

Download URL clustexts-0.0.2.tar.gz
Size 5.8 kB
Tags Source
SHA-256 checksum
How to use checksums
8257f42d7738bbbd774f35444bafe0f76b1a4542079796895b6b4d2a30a2c4f3
BLAKE2b-256 checksum
How to use checksums
2fe3bb45fc42fa3f6aa83c7b2aeef906a782b4216bf9059a92ac742e0a02db16
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.11.9

Release files / clustexts-0.0.2-py3-none-any.whl

Download URL clustexts-0.0.2-py3-none-any.whl
Size 6.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
0d0e66cce81fd5a811d29b46d4bf5d827a9428126a6685eb14868a585b767c47
BLAKE2b-256 checksum
How to use checksums
1816c5a0f826643214c213b57bbb472fe3a5aa66085fb20619eae62f4c8fe3aa
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.11.9

Security for clustexts 0.0.2

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.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A 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.

Release history Release notifications | RSS feed

This release

0.0.2 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page