utelearn 0.1.2026__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
utelearn/__init__.py ADDED
@@ -0,0 +1 @@
1
+ from .kmeans import KMeans
utelearn/kmeans.py ADDED
@@ -0,0 +1,47 @@
1
+ # Kmeans_Lib.py
2
+
3
+ import numpy as np
4
+ import matplotlib.pyplot as plt
5
+
6
+ class KMeans:
7
+ def __init__(self, k, max_iters=10, tol=1e-4):
8
+ self.k = k
9
+ self.max_iters = max_iters
10
+ self.tol = tol
11
+ self.centroids = None
12
+ self.labels = None
13
+
14
+ def fit(self, X):
15
+ np.random.seed(42)
16
+ random_indices = np.random.permutation(X.shape[0])[:self.k]
17
+ self.centroids = X[random_indices]
18
+
19
+ for _ in range(self.max_iters):
20
+ self.labels = self._assign_clusters(X)
21
+ new_centroids = self._calculate_centroids(X)
22
+
23
+ if np.all(np.abs(new_centroids - self.centroids) < self.tol):
24
+ break
25
+ self.centroids = new_centroids
26
+
27
+ def predict(self, X):
28
+ return self._assign_clusters(X)
29
+
30
+ def _assign_clusters(self, X):
31
+ distances = np.linalg.norm(X[:, np.newaxis] - self.centroids, axis=2)
32
+ return np.argmin(distances, axis=1)
33
+
34
+ def _calculate_centroids(self, X):
35
+ new_centroids = []
36
+ for i in range(self.k):
37
+ cluster_points = X[self.labels == i]
38
+ if len(cluster_points) == 0:
39
+ # Handle empty cluster: keep the old centroid or reassign randomly
40
+ new_centroids.append(self.centroids[i])
41
+ else:
42
+ new_centroids.append(cluster_points.mean(axis=0))
43
+ return np.array(new_centroids)
44
+
45
+ def inertia(self, X):
46
+ # Calculate inertia, the sum of squared distances of samples to their closest cluster center.
47
+ return np.sum((X - self.centroids[self.labels]) ** 2)
@@ -0,0 +1,9 @@
1
+ Metadata-Version: 2.4
2
+ Name: utelearn
3
+ Version: 0.1.2026
4
+ Summary: A custom machine learning library built from scratch from UTE Vietnam.
5
+ Author-email: Dung Cai <dung.cai@hcmute.edu.vn>
6
+ Requires-Python: >=3.7
7
+ Description-Content-Type: text/markdown
8
+ Requires-Dist: numpy
9
+ Requires-Dist: matplotlib
@@ -0,0 +1,6 @@
1
+ utelearn/__init__.py,sha256=ZMqXHawHjsU_2iaijw7-tKhk_MHaC2cXOY7J7zQLfJ8,26
2
+ utelearn/kmeans.py,sha256=MyNC7WAE9_mv9k2OEXKvLT1DL0MRKne-tZSUnMb1xTw,1663
3
+ utelearn-0.1.2026.dist-info/METADATA,sha256=xf6TJ3Wl1bbkyesClBKvdOORigcToo9NOVQu4zGI1Ns,302
4
+ utelearn-0.1.2026.dist-info/WHEEL,sha256=YVMoNqKzERt-wjUZwJ33xBGAwnFl-4cqbYkTtWa4itE,91
5
+ utelearn-0.1.2026.dist-info/top_level.txt,sha256=D0StDsby178AgJyPRJwq2Pc3iqZ-hR64wXlP8eyK0V4,9
6
+ utelearn-0.1.2026.dist-info/RECORD,,
@@ -0,0 +1,5 @@
1
+ Wheel-Version: 1.0
2
+ Generator: setuptools (84.0.0)
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
5
+
@@ -0,0 +1 @@
1
+ utelearn