|
6 | 6 | def tf_k_means_clustering(vectors, noofclusters,max_iterations = 100,tolerance = 1e-4): |
7 | 7 | """ |
8 | 8 | Performs K-means clustering using a fixed and efficient vectorized approach, using Tensorflow 2.x |
9 | | -
|
10 | | - Parameters: |
11 | | - vectors (list): A list of vectors. |
12 | | - noofclusters (int): The number of clusters (k). |
13 | | - max_iterations(int): maximum number of iterations or how many times the algorithm will refine its cluster assignments and centroid positions, until convergence. |
14 | | - tolerance(int): defines a convergence criterion. The K-means algorithm stops when the centroids move less than this tolerance value between consecutive iterations. |
15 | | -
|
16 | | - (set same random seed in all examples for reproducibility) |
17 | | - >>>tf.random.set_seed(42) |
18 | | -
|
19 | | - Example 1: |
20 | | - >>>data2 = np.array([[0.0, 0.0], [0.1, 0.1], [10.0, 10.0]], dtype=np.float32) |
21 | | - >>>centroids2, assignments2 = tf_k_means_clustering(data2, 2) |
22 | | - >>>print(centroids2,assignments2) |
23 | | - [[ 0.05 0.05] |
24 | | - [10. 10. ]] [0 0 1] |
25 | | -
|
26 | | - Example 2 (Idential data points): |
27 | | - >>>data_identical = np.array([[1.0, 1.0], [1.0, 1.0], [1.0, 1.0], [1.0, 1.0]], dtype=np.float32) |
28 | | - >>>centroids, assignments = tf_k_means_clustering(data_identical, 1) |
29 | | - >>>print(centroids,assignments) |
30 | | -
|
31 | | - Example 3 (k>N): |
32 | | - >>>data = np.array([[0.0, 0.0], [0.9, 0.9], [13.0, 15.0]], dtype=np.float32) |
33 | | - >>>centroids, assignments = tf_k_means_clustering(data, 5) |
34 | | - >>>print(centroids,assignments) |
35 | 9 | """ |
36 | 10 |
|
37 | 11 |
|
|
0 commit comments