utelearn
utelearn is a custom machine learning library built from scratch for educational purposes at UTE.
Installation
You can install the package directly from PyPI:
pip install utelearn
Features
- KMeans Clustering: Custom implementation of the KMeans clustering algorithm using NumPy.
- Built-in support for data visualization with Matplotlib.
Quick Start
Here are quick examples of how to use the machine learning libs from utelearn:
K-Means
Kmeans Clustering Example
import numpy as np
import matplotlib.pyplot as plt
from utelearn.kmeans import KMeans
# 1. Tạo dữ liệu mẫu ngẫu nhiên (2 cụm rõ rệt)
np.random.seed(42)
X = np.vstack([
np.random.randn(50, 2) + np.array([2, 2]),
np.random.randn(50, 2) + np.array([-2, -2])
])
# 2. Khởi tạo và huấn luyện mô hình K-Means với k=2
kmeans = KMeans(k=2, max_iters=100)
kmeans.fit(X)
# 3. In kết quả trung tâm cụm (centroids)
print("Tọa độ các tâm cụm (Centroids):")
print(kmeans.centroids)
# 4. Dự đoán nhãn cho các điểm dữ liệu (nếu lớp KMeans có hàm predict)
# Hoặc gán nhãn dựa vào khoảng cách gần nhất tới các centroids
labels = np.argmin([np.linalg.norm(X - c, axis=1) for c in kmeans.centroids], axis=0)
# 5. Vẽ biểu đồ trực quan
plt.figure(figsize=(8, 6))
plt.scatter(X[:, 0], X[:, 1], c=labels, cmap='viridis', marker='o', alpha=0.7, edgecolors='k', label='Dữ liệu mẫu')
plt.scatter(kmeans.centroids[:, 0], kmeans.centroids[:, 1], c='red', marker='X', s=250, edgecolors='k', label='Tâm cụm (Centroids)')
plt.title("Trực quan hóa thuật toán K-Means - Utelearn", fontsize=14)
plt.xlabel("Trục X", fontsize=12)
plt.ylabel("Trục Y", fontsize=12)
plt.legend()
plt.grid(True, linestyle='--', alpha=0.5)
plt.show()
K-Nearest Neighbors (KNN)
1. KNN Regression & Classification Example
import numpy as np
from utelearn import knn
# Generate synthetic training data
X_train = np.random.rand(100, 1)
y_train = 4 * X_train**2 + 3 * X_train + 2 + np.random.randn(100, 1) * 0.1
# Generate test points
X_test = np.linspace(0, 1, 10).reshape(-1, 1)
# Perform KNN regression with k=5
predictions = knn.knn_regression(X_train, y_train, X_test, k=5)
print("Regression Predictions:", predictions)
# Generate synthetic training and test features/labels
X_train = np.random.rand(100, 2)
y_train = np.random.randint(0, 3, size=100) # 3 classes
X_test = np.random.rand(10, 2)
# Perform KNN classification with k=5
predictions = knn.knn_classify(X_train, y_train, X_test, k=5, n_classes=3)
print("Classification Predictions:", predictions)
K-Nearest Neighbors (KNN)
1. KNN Regression & Classification Example using Points Lib
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.colors as mcolors
from utelearn import knn
from utelearn import points as point
# 1. Configuration parameters
n_training_points = 100
n_test_points = 500
n_classes = 3
dimension = 2
k_neighbors = 5
# 2. Generate Training and Test Data using utelearn's points module (Spiral Distribution)
training_data = point.Spiral(n_training_points, n_classes, dimension)
test_data = point.Spiral(n_test_points, n_classes, dimension)
# 3. Perform KNN Classification using utelearn's knn module
print("Running KNN classification on test data using utelearn...")
predicted_labels = knn.knn_classify(
X_train=training_data.P,
y_train=training_data.L,
X_test=test_data.P,
k=k_neighbors,
n_classes=n_classes
)
# 4. Plotting the Results
plt.figure(figsize=(15, 5))
# Subplot 1: Reference Training Data
plt.subplot(1, 3, 1)
plt.scatter(
training_data.P[:, 0],
training_data.P[:, 1],
c=training_data.L,
s=20,
cmap=mcolors.ListedColormap(["red", "purple", "green"])
)
plt.title(f"Reference Data ({n_training_points * n_classes} points)")
plt.grid(True, alpha=0.3)
# Subplot 2: Unclassified Test Data
plt.subplot(1, 3, 2)
plt.scatter(
test_data.P[:, 0],
test_data.P[:, 1],
color="blue",
s=15
)
plt.title("Unclassified Test Data")
plt.grid(True, alpha=0.3)
# Subplot 3: Classified Test Data via utelearn KNN
plt.subplot(1, 3, 3)
plt.scatter(
test_data.P[:, 0],
test_data.P[:, 1],
c=predicted_labels,
s=15,
cmap=mcolors.ListedColormap(["red", "purple", "green"])
)
plt.title(f"Test Data After KNN (k={k_neighbors})")
plt.grid(True, alpha=0.3)
plt.tight_layout()
plt.show()
Author
- Dung Cai (dung.cai@hcmute.edu.vn)
Metadata
Release files for utelearn 10.2.2026
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| utelearn-10.2.2026.tar.gz | 6.3 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| utelearn-10.2.2026-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 12.3 kB
Release files / utelearn-10.2.2026.tar.gz
| Download URL | utelearn-10.2.2026.tar.gz |
|---|---|
| Size | 6.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
c0fa1c8f08460b6cdbb9085abd6a74ca821ff74525ce93e4ab5d06568a26be7a
|
|
BLAKE2b-256 checksum How to use checksums |
f2dfc1f50872623ce85d1cc413c6bbfe5cc26f03016b6d74a7aa1eb3f6c7d3f4
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.11.4
|
Release files / utelearn-10.2.2026-py3-none-any.whl
| Download URL | utelearn-10.2.2026-py3-none-any.whl |
|---|---|
| Size | 6.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
75ae9e46d9eea45205fbb793fec41829c1eb7432f4d423a38e70c0b84ebce356
|
|
BLAKE2b-256 checksum How to use checksums |
aec8267128aab5eaa45dda07846e2bbcfa349070330bc5382048704d00197a95
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.11.4
|