Sử dụng Matplotlib và NumPy để vẽ scatter plot hiển thị các cụm (clusters) và tâm cụm (centroids). Các bước: thiết lập kích thước figure, tạo dữ liệu ngẫu nhiên, vẽ điểm dữ liệu màu theo cluster, vẽ tâm cụm dạng dấu '+' màu đỏ.
Ví dụ
import numpy as np
import matplotlib.pyplot as plt
plt.rcParams["figure.figsize"] = [7.00, 3.50]
plt.rcParams["figure.autolayout"] = True
x = np.random.randn(10)
y = np.random.randn(10)
Cluster = np.array([0, 1, 1, 1, 3, 2, 2, 3, 0, 2])
centers = np.random.randn(4, 2)
fig = plt.figure()
ax = fig.add_subplot(111)
scatter = ax.scatter(x, y, c=Cluster, s=50)
for i, j in centers:
ax.scatter(i, j, s=50, c='red', marker='+')
plt.show()
Kết quả
Hiển thị cửa sổ đồ họa scatter plot với các điểm dữ liệu màu theo cluster và tâm cụm đỏ dấu '+'.