如何在使用 K-means 缩放后使用原始变量绘制散点图

How to plot scatter plot with original variables after scalling with K-means

我已将原始数据缩放 X1:

scaler = StandardScaler()
X1_scaled = pd.DataFrame(scaler.fit_transform(X1),columns = X1.columns)

然后进行k均值聚类:

kmeans = KMeans(
        init="random",
        n_clusters=3,
        n_init=10,
        max_iter=300,
        random_state=123)   
X1['label'] = kmeans.fit_predict(X1_scaled[['Wn', 'LL']])

# get centroids
centroids = kmeans.cluster_centers_
cen_x = [i[0] for i in centroids] 
cen_y = [i[1] for i in centroids]  

                                        

现在,我想绘制原始数据 (X1) 和质心,但质心是按比例缩放的,所以当我绘制结果时:

g = sns.scatterplot(x=X1.Wn, y= X1.LL, hue=X1.label,
              data=X1, palette='colorblind', 
                   legend='full')
g = sns.scatterplot(cen_x,cen_y,s=80,color='black')

质心在簇外。 如何绘制原始数据、组和质心?

这是我得到的图像:

这就是我想要的,但使用原始数据而不是缩放后的数据:

您可以在质心上调用 scaler.inverse_transform()。 (请注意,sns.scatterplotaxes-level function,returns 是 ax,而不是 FacetGrid。)

from sklearn.preprocessing import StandardScaler
from sklearn.cluster import KMeans
from matplotlib import pyplot as plt
import seaborn as sns
import pandas as pd
import numpy as np

X1 = pd.DataFrame({'Wn': np.random.rand(30) * 12, 'LL': np.random.rand(30) * 6})

scaler = StandardScaler()
X1_scaled = pd.DataFrame(scaler.fit_transform(X1), columns=X1.columns)

kmeans = KMeans(init="random",
                n_clusters=3,
                n_init=10,
                max_iter=300,
                random_state=123)
X1['label'] = kmeans.fit_predict(X1_scaled[['Wn', 'LL']])

# get centroids
centroids = scaler.inverse_transform(kmeans.cluster_centers_)
cen_x = [i[0] for i in centroids]
cen_y = [i[1] for i in centroids]

ax = sns.scatterplot(x='Wn', y='LL', hue='label',
                     data=X1, palette='colorblind',
                     legend='full')
sns.scatterplot(x=cen_x, y=cen_y, s=80, color='black', ax=ax)

plt.tight_layout()
plt.show()