Skip to content

Commit 273b47b

Browse files
authored
Merge pull request #1 from raisaaajose/k-means-doctest
fix: added doctest for k-means-clustering
2 parents 6e1a104 + d47ad9c commit 273b47b

File tree

1 file changed

+28
-5
lines changed

1 file changed

+28
-5
lines changed

dynamic_programming/k_means_clustering_tensorflow.py

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,38 @@
66

77
def tf_k_means_cluster(vectors, noofclusters):
88
"""
9-
K-Means Clustering using TensorFlow.
10-
'vectors' should be a n*k 2-D NumPy array, where n is the number
11-
of vectors of dimensionality k.
12-
'noofclusters' should be an integer.
9+
K-Means Clustering using TensorFlow 1.x.
10+
11+
Parameters:
12+
vectors (numpy.ndarray): A n*k 2-D NumPy array of dtype float32,
13+
where n is the number of vectors and k is their dimensionality.
14+
noofclusters (int): An integer representing the number of clusters (k).
15+
16+
(For reproducibility, set both Python's random seed and TensorFlow's random seed)
17+
>>> import random
18+
>>> random.seed(42)
19+
>>> tf.random.set_seed(42)
20+
21+
Example 1
22+
>>> data1 = numpy.array([[0.0, 0.0], [0.1, 0.1], [10.0, 10.0]], dtype=numpy.float32)
23+
>>> centroids1, assignments1 = tf_k_means_cluster(data1, 2)
24+
>>> print(centroids1)
25+
[[ 0.05 0.05]
26+
[10. 10. ]]
27+
>>> print(assignments1)
28+
[0 0 1]
29+
30+
Example 2:
31+
>>> data3 = numpy.array([[0.0, 0.0], [0.9, 0.9], [13.0, 15.0]], dtype=numpy.float32)
32+
>>> tf_k_means_cluster(data3, 5)
33+
Traceback (most recent call last):
34+
...
35+
AssertionError
1336
"""
1437

1538
noofclusters = int(noofclusters)
1639
assert noofclusters < len(vectors)
17-
40+
1841
# Find out the dimensionality
1942
dim = len(vectors[0])
2043

0 commit comments

Comments
 (0)