Skip to content

fix: added doctest for k-means-clustering #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 30, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 28 additions & 5 deletions dynamic_programming/k_means_clustering_tensorflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,38 @@

def tf_k_means_cluster(vectors, noofclusters):
"""
K-Means Clustering using TensorFlow.
'vectors' should be a n*k 2-D NumPy array, where n is the number
of vectors of dimensionality k.
'noofclusters' should be an integer.
K-Means Clustering using TensorFlow 1.x.

Parameters:
vectors (numpy.ndarray): A n*k 2-D NumPy array of dtype float32,
where n is the number of vectors and k is their dimensionality.
noofclusters (int): An integer representing the number of clusters (k).

(For reproducibility, set both Python's random seed and TensorFlow's random seed)
>>> import random
>>> random.seed(42)
>>> tf.random.set_seed(42)

Example 1
>>> data1 = numpy.array([[0.0, 0.0], [0.1, 0.1], [10.0, 10.0]], dtype=numpy.float32)
>>> centroids1, assignments1 = tf_k_means_cluster(data1, 2)
>>> print(centroids1)
[[ 0.05 0.05]
[10. 10. ]]
>>> print(assignments1)
[0 0 1]

Example 2:
>>> data3 = numpy.array([[0.0, 0.0], [0.9, 0.9], [13.0, 15.0]], dtype=numpy.float32)
>>> tf_k_means_cluster(data3, 5)
Traceback (most recent call last):
...
AssertionError
"""

noofclusters = int(noofclusters)
assert noofclusters < len(vectors)

# Find out the dimensionality
dim = len(vectors[0])

Expand Down