我在哪里可以找到kmeans的距离度量?

2024-04-20 02:02:50 发布

您现在位置:Python中文网/ 问答频道 /正文

我在towardsdatascience上看到一篇关于在timeseries上使用kmeans的文章。链接:https://towardsdatascience.com/clustering-electricity-profiles-with-k-means-42d6d0644d00 现在我试着在我自己的数据集上使用它,效果非常好。 我有每日档案,看起来是这样的: enter image description here 我的问题是:每日档案之间的距离是多少? 那么它是如何计算每日档案之间的差异的呢? 我对k-means函数使用以下代码:

kmeans = KMeans(n_clusters=2)
cluster_found = kmeans.fit_predict(X)
cluster_found_sr = pd.Series(cluster_found, name='cluster')
df_pivot = df_pivot.set_index(cluster_found_sr, append=True )

fig, ax = plt.subplots(1,1, figsize=(18,10))
color_list = ['blue', 'red', 'green', 'brown', 'yellow', 'black', 'white']
cluster_values = sorted(df_pivot.index.get_level_values('cluster').unique())
""
for cluster, color in zip(cluster_values, color_list):
    df_pivot.xs(cluster, level=1).T.plot(
        ax=ax, legend=False, alpha=0.01, color=color, label = f'Cluster {cluster}'
        )
    df_pivot.xs(cluster, level=1).median().plot(
        ax=ax, color=color, alpha=0.9, ls='--'
    )

ax.set_xticks(np.arange(0 , 24))
ax.set_ylabel('watt')
ax.set_xlabel('hour')

在我使用一种方法来帮助我决定应该选择多少簇之前

sillhoute_scores = []
n_cluster_list = np.arange(2,31).astype(int)

X = df_pivot.values.copy()

sc = MinMaxScaler()
X = sc.fit_transform(X)

for n_cluster in n_cluster_list:
    kmeans = KMeans(n_clusters=n_cluster)
    cluster_found = kmeans.fit_predict(X)
    sillhoute_scores.append(silhouette_score(X, kmeans.labels_))

Tags: df档案axlevellistmeansfitcolor