Mô phỏng Sự kiện Rời rạc bằng Python
Diogo Costa (PhD, MSc)
Adjunct Professor, University of Saskatchewan, Canada & CEO of ImpactBLUE-Scientific
Histogram
Gói Matplotlib
import matplotlib.pyplot as plt
Dùng: Tạo histogram cho tập data với 50 bins
plt.hist(data, bins=50)


Máy học

Trong mô hình sự kiện rời rạc
Trọng tâm
Quan sát và tâm cụm

Phương thức SciPy
scipy.cluster.vq.kmeans()
Triển khai
import scipy
scipy.cluster.vq.kmeans(
obs, k_or_guess, iter=20, thresh=1e-05,
check_finite=True, *, seed=None)
obs là mảng numpyTrước khi chạy k-means: Làm trắng dữ liệu
obsobs theo độ lệch chuẩn
Trong SciPy
scipy.cluster.vq.whiten(
obs, check_finite=True)
obs là mảng numpyProcess 1
Nhập gói
import scipy.cluster.vq as scvq
Làm trắng kết quả mô hình
white_data = scvq.whiten(model_results)
Tìm 2 cụm (chấm xanh)
cluster_centroids, distortion =
scvq.kmeans(white_data, 2)
Kỹ thuật
Cách đơn giản
nobs = số quan sátnum_clusters =
int((model_results.shape[0]/2)**0.5)
22
from sklearn.metrics import silhouette_score
Tính điểm silhouette cho số cụm k
for k in range(2, 6):
model = KMeans(n_clusters=k)
model.fit(model_results)
pred = model.predict(model_results)
score = silhouette_score(model_results, pred)
Kết quả console
Silhouette Score for k = 2: 0.591
Silhouette Score for k = 3: 0.472
Silhouette Score for k = 4: 0.381
Silhouette Score for k = 5: 0.364
Silhouette Score for k = 6: 0.373
Diễn giải
score = 1score = -1score gần 0Mô phỏng Sự kiện Rời rạc bằng Python