Discrete Event Simulation in Python
Diogo Costa (PhD, MSc)
Adjunct Professor, University of Saskatchewan, Canada & CEO of ImpactBLUE-Scientific
Histogram
Matplotlib package
import matplotlib.pyplot as plt
Use: Create an histogram of dataset data
with 50 bins
plt.hist(data, bins=50)
Machine learning
In discrete-event models
Our focus
Observations and cluster centroids
SciPy method
scipy.cluster.vq.kmeans()
Implementation
import scipy
scipy.cluster.vq.kmeans(
obs, k_or_guess, iter=20, thresh=1e-05,
check_finite=True, *, seed=None)
obs
is a numpy arrayBefore running k-mean: Data whitening
obs
dataobs
by its standard deviationIn SciPy
scipy.cluster.vq.whiten(
obs, check_finite=True)
obs
is a numpy arrayProcess 1
Import package
import scipy.cluster.vq as scvq
Whiten model results
white_data = scvq.whiten(model_results)
Find 2 clusters (blue dots)
cluster_centroids, distortion =
scvq.kmeans(white_data, 2)
Techniques
Simple method
nobs
= number of observationsnum_clusters =
int((model_results.shape[0]/2)**0.5)
22
from sklearn.metrics import silhouette_score
Calculate silhouette scores for k
numbers of clusters
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 output
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
Interpret results
score = 1
score = -1
score
near 0Discrete Event Simulation in Python