Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit c46141d

Browse files
committedOct 2, 2024·
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
1 parent 89f5f80 commit c46141d

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed
 

‎quantum/quantum_kmeans_clustering.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,12 @@
66

77

88
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+
)
1012
return MinMaxScaler().fit_transform(data), labels
1113

14+
1215
def quantum_distance(point1, point2):
1316
"""
1417
Computes the quantum distance between two points.
@@ -46,6 +49,7 @@ def initialize_centroids(data: np.ndarray, k: int) -> np.ndarray:
4649
"""
4750
return data[np.random.choice(len(data), k, replace=False)]
4851

52+
4953
def assign_clusters(data, centroids):
5054
clusters = [[] for _ in range(len(centroids))]
5155
for point in data:
@@ -55,9 +59,11 @@ def assign_clusters(data, centroids):
5559
clusters[closest].append(point)
5660
return clusters
5761

62+
5863
def recompute_centroids(clusters):
5964
return np.array([np.mean(cluster, axis=0) for cluster in clusters if cluster])
6065

66+
6167
def quantum_kmeans(data, k, max_iters=10):
6268
centroids = initialize_centroids(data, k)
6369

@@ -102,4 +108,4 @@ def quantum_kmeans(data, k, max_iters=10):
102108
plt.tight_layout()
103109
plt.show()
104110

105-
print(f"Final Centroids:\n{final_centroids}")
111+
print(f"Final Centroids:\n{final_centroids}")

0 commit comments

Comments
 (0)
Please sign in to comment.