6
6
7
7
8
8
def generate_data (n_samples = 100 , n_features = 2 , n_clusters = 2 ):
9
- data , labels = make_blobs (n_samples = n_samples , centers = n_clusters , n_features = n_features , random_state = 42 )
9
+ data , labels = make_blobs (
10
+ n_samples = n_samples , centers = n_clusters , n_features = n_features , random_state = 42
11
+ )
10
12
return MinMaxScaler ().fit_transform (data ), labels
11
13
14
+
12
15
def quantum_distance (point1 , point2 ):
13
16
"""
14
17
Computes the quantum distance between two points.
@@ -46,6 +49,7 @@ def initialize_centroids(data: np.ndarray, k: int) -> np.ndarray:
46
49
"""
47
50
return data [np .random .choice (len (data ), k , replace = False )]
48
51
52
+
49
53
def assign_clusters (data , centroids ):
50
54
clusters = [[] for _ in range (len (centroids ))]
51
55
for point in data :
@@ -55,9 +59,11 @@ def assign_clusters(data, centroids):
55
59
clusters [closest ].append (point )
56
60
return clusters
57
61
62
+
58
63
def recompute_centroids (clusters ):
59
64
return np .array ([np .mean (cluster , axis = 0 ) for cluster in clusters if cluster ])
60
65
66
+
61
67
def quantum_kmeans (data , k , max_iters = 10 ):
62
68
centroids = initialize_centroids (data , k )
63
69
@@ -102,4 +108,4 @@ def quantum_kmeans(data, k, max_iters=10):
102
108
plt .tight_layout ()
103
109
plt .show ()
104
110
105
- print (f"Final Centroids:\n { final_centroids } " )
111
+ print (f"Final Centroids:\n { final_centroids } " )
0 commit comments