【问题标题】:K-Means algorithm Centroids are not placed in the clustersK-Means 算法 质心未放置在集群中
【发布时间】:2022-01-01 02:19:08
【问题描述】:

我有问题。我想对我的数据集进行聚类。不幸的是,我的质心不在集群中,而是在外面。我已经读过了 Python k-mean, centroids are placed outside of the clusters 关于这个。

但是,我不知道可能是什么原因。如何正确聚类?

您可以在https://gist.githubusercontent.com/Coderanker3/24c948d2ff0b7f71e51b3774c2cc7b22/raw/253ba0660720de3a9cf7dee2a2d25a37f61095ca/Dataset找到数据集

import pandas as pd
from sklearn.cluster import KMeans
from scipy.cluster import hierarchy
import seaborn as sns
from sklearn import metrics
from sklearn.metrics import silhouette_samples
import matplotlib as mpl
import matplotlib.pyplot as plt

df = pd.read_csv(r'https://gist.githubusercontent.com/Coderanker3/24c948d2ff0b7f71e51b3774c2cc7b22/raw/253ba0660720de3a9cf7dee2a2d25a37f61095ca/Dataset')
df.shape

features_clustering = ['review_scores_accuracy',
 'distance_to_center',
 'bedrooms',
 'review_scores_location',
 'review_scores_value',
 'number_of_reviews',
 'beds',
 'review_scores_communication',
 'accommodates',
 'review_scores_checkin',
 'amenities_count',
 'review_scores_rating',
 'reviews_per_month',
 'corrected_price']

df_cluster = df[features_clustering].copy()
X = df_cluster.copy()

model = KMeans(n_clusters=4, random_state=53, n_init=10, max_iter=1000, tol=0.0001)
clusters = model.fit_predict(X)
df_cluster["cluster"] = clusters

fig = plt.figure(figsize=(8, 8))
sns.scatterplot(data=df_cluster, x="amenities_count", y="corrected_price", hue="cluster", palette='Set2_r')
sns.scatterplot(x=model.cluster_centers_[:,0], y=model.cluster_centers_[:,1], color='blue',marker='*',
                            label='centroid', s=250)
plt.legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.)
#plt.ylim(ymin=0)
plt.xlim(xmin=-0.1)
plt.show()

model.cluster_centers_

inertia = model.inertia_
sil = metrics.silhouette_score(X,model.labels_)

print(f'inertia {inertia:.3f}')
print(f'silhouette {sil:.3f}')

[OUT]

inertia 4490.076
silhouette 0.156

【问题讨论】:

    标签: python pandas cluster-analysis k-means centroid


    【解决方案1】:

    您正在制作多维集群,并且希望它们适合二维地图,但它本身是行不通的。让我解释一下,变量是一个维度:x1,x2,x3,...,xn,如果您找到集群,它会给您结果 y1,y2,y3,...,yn。如果您在 2D 中映射结果,(我以您为例) x1 是“amenities_count”,x5 是“corrected_price”。

    它将创建仅包含这两个变量的 2D 地图,并且绘图仪肯定会看到您使用 2D 地图,只会从集群中获取前两个变量 y1 和 y2 进行绘图。请注意,xi 与 y1 没有直接关系。

    您必须:1) 进行转换以找到其对应的 x,y 或 2) 减少您用于生成包含所有变量信息的 2D 地图的数据的维度。

    对于第一种情况,我不太确定,因为我从未做过(重新映射数据)。 但是在降维方面,我推荐你使用https://en.wikipedia.org/wiki/T-distributed_stochastic_neighbor_embedding或者经典的PCA。

    道德:如果您想查看 2D 集群,请确保您只有 2 个变量。

    【讨论】:

      【解决方案2】:

      您的主要问题的答案:集群中心在您的集群之外。

      1 :您正在对 features_clustering 列表中显示的 14 个特征进行聚类。

      2:您正在二维空间中查看集群,任意选择amenities_countcorrected_price 作为数据以及集群中心x=model.cluster_centers_[:,0], y=model.cluster_centers_[:,1] 的两个坐标,它们不对应相同的特征。

      由于这些原因,您会得到奇怪的结果;他们真的没有任何意义。

      底线是您无法在二维上查看 14 维聚类。

      为了更清楚地显示第 2 点,将聚类线的绘图更改为

      sns.scatterplot(x=model.cluster_centers_[:,10], y=model.cluster_centers_[:,13], color='blue',marker='*', label='centroid', s=250)
      

      根据与数据相同的特征绘制集群中心。


      关于集群中心在集群数据之外的 SO 答案的链接是关于在集群之前将数据缩放到 0 到 1 之间,然后在使用真实数据进行绘图时不将集群中心重新缩放。这与您在此处的问题不同。

      【讨论】:

        猜你喜欢
        • 2018-10-11
        • 2019-05-14
        • 2020-02-21
        • 1970-01-01
        • 2021-11-25
        • 2019-11-26
        • 2013-09-18
        • 1970-01-01
        • 2018-05-09
        相关资源
        最近更新 更多