開始使用免費開始

物流電商模型:k-means 分析

你已經對模型輸出有了第一步的瞭解,現在可以透過叢集分析進一步看出結果之間的模式與關係。

你將使用 k-means 演算法來協助理解影響模型行為的主要控制因子,並將資料點依相似屬性分成多個群組。這有助於找出你的模型所代表的真實電商/物流作業中的瓶頸。

kmeans 與 whiten 已從 scipy.cluster.vq 匯入,並已匯入 matplotlib.pyplot as plt。原始與白化(whitened)後的資料集都具有下列欄位。虛擬變數 p 定義這些流程在資料集中的索引:

  • 第 1 欄(p=0):time_requests
  • 第 2 欄(p=1):time_packaging
  • 第 3 欄(p=2):time_shipping
  • 第 4 欄(p=3):sum/total time

本練習屬於課程

Python 的離散事件模擬

檢視課程

練習說明

  • 對 record_processes_np 陣列進行 whiten,以便為 k-means 分群做好準備。
  • 使用 SciPy 套件在 whitened 陣列上執行 k-means 方法,並將 k 設為尋找 3 個叢集。

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

# Whiten the record_processes_np array
whitened = ____(record_processes_np)

# Run the k-means method on whitened, using three clusters
codebook, distortion = ____(whitened, ____)

fig, axs = plt.subplots(3)
for p in range(3):
    axs[p].scatter(whitened[:, 3], whitened[:, p], marker=".", label=f"{process_names[p]}")
    axs[p].scatter(codebook[:, 3], codebook[:, p], label='Cluster Centroids')
    axs[p].legend(loc='center left', bbox_to_anchor=(1, 0.5))
    axs[p].set_ylabel(f'Process duration (days)')
    axs[p].set_xlabel('Total duration (days)')
plt.show()
編輯並執行程式碼