|
6 | 6 |
|
7 | 7 | def tf_k_means_cluster(vectors, noofclusters):
|
8 | 8 | """
|
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 |
13 | 36 | """
|
14 | 37 |
|
15 | 38 | noofclusters = int(noofclusters)
|
16 | 39 | assert noofclusters < len(vectors)
|
17 |
| - |
| 40 | + |
18 | 41 | # Find out the dimensionality
|
19 | 42 | dim = len(vectors[0])
|
20 | 43 |
|
|
0 commit comments