การจำลองเหตุการณ์แบบไม่ต่อเนื่องด้วย Python
Diogo Costa (PhD, MSc)
Adjunct Professor, University of Saskatchewan, Canada & CEO of ImpactBLUE-Scientific
Histogram
แพ็กเกจ Matplotlib
import matplotlib.pyplot as plt
การใช้งาน: สร้าง histogram จากชุดข้อมูล data โดยใช้ 50 bins
plt.hist(data, bins=50)


Machine learning

ในโมเดล discrete-event
จุดสนใจหลัก
การสังเกตและ cluster centroids

เมธอดใน SciPy
scipy.cluster.vq.kmeans()
การใช้งาน
import scipy
scipy.cluster.vq.kmeans(
obs, k_or_guess, iter=20, thresh=1e-05,
check_finite=True, *, seed=None)
obs คือ numpy arrayก่อนรัน k-means: Data whitening
obsobs ด้วยส่วนเบี่ยงเบนมาตรฐาน
ใน SciPy
scipy.cluster.vq.whiten(
obs, check_finite=True)
obs คือ numpy arrayProcess 1
นำเข้าแพ็กเกจ
import scipy.cluster.vq as scvq
Whiten ผลลัพธ์ของโมเดล
white_data = scvq.whiten(model_results)
หา 2 คลัสเตอร์ (จุดสีน้ำเงิน)
cluster_centroids, distortion =
scvq.kmeans(white_data, 2)
เทคนิค
วิธีอย่างง่าย
nobs = จำนวนการสังเกตnum_clusters =
int((model_results.shape[0]/2)**0.5)
22
from sklearn.metrics import silhouette_score
คำนวณ silhouette scores สำหรับจำนวนคลัสเตอร์ 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)
ผลลัพธ์จาก 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
การแปลผล
score = 1score = -1score ใกล้ 0การจำลองเหตุการณ์แบบไม่ต่อเนื่องด้วย Python