Skip to content

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

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
31 changes: 27 additions & 4 deletions dynamic_programming/k_means_clustering_tensorflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,33 @@

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.

Check failure on line 13 in dynamic_programming/k_means_clustering_tensorflow.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

dynamic_programming/k_means_clustering_tensorflow.py:13:89: E501 Line too long (91 > 88)
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)
Expand Down
Loading