Skip to content

Commit 6bccee3

Browse files
authored
Update main.py
1 parent e524ef8 commit 6bccee3

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

better-python/main.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,20 @@ def read_large_csv_with_generator(file_path: str) -> Generator[Dict[str, str], N
2929

3030
for row in read_large_csv_with_generator(file_path):
3131
print(row)
32+
33+
# Caching
34+
35+
from functools import cache
36+
from typing import Tuple
37+
import numpy as np
38+
39+
@cache
40+
def euclidean_distance(pt1: Tuple[float, float], pt2: Tuple[float, float]) -> float:
41+
return np.sqrt((pt1[0] - pt2[0]) ** 2 + (pt1[1] - pt2[1]) ** 2)
42+
43+
def assign_clusters(data: np.ndarray, centroids: np.ndarray) -> np.ndarray:
44+
clusters = np.zeros(data.shape[0])
45+
for i, point in enumerate(data):
46+
distances = [euclidean_distance(tuple(point), tuple(centroid)) for centroid in centroids]
47+
clusters[i] = np.argmin(distances)
48+
return clusters

0 commit comments

Comments
 (0)