【问题标题】:Why do I get nested clusters in kmeans when I use normalized data while I get non-overlapping clusters when I use non normalized data?为什么当我使用规范化数据时会在 kmeans 中得到嵌套集群,而当我使用非规范化数据时会得到非重叠集群?
【发布时间】:2020-06-13 02:25:15
【问题描述】:

我目前正在学习 IBM 提供的机器学习基础课程。老师建好模型后,我注意到他没有使用归一化数据来拟合模型,而是使用常规数据,最终得到了一个很好的聚类和不重叠的聚类。但是当我尝试使用归一化数据来训练模型时,我遇到了灾难,我得到了嵌套集群,如代码和图像所示。为什么规范化过程会导致这种情况?尽管“据我所知”在数学基础算法中使用归一化总是好的。

代码不使用标准化数据

import numpy as np
import matplotlib.pyplot as plt
%matplotlib  inline
from sklearn.cluster import KMeans
cust_df = pd.read_csv('D:\machine learning\Cust_Segmentation.csv')
cust_df.head()
df = cust_df.drop('Address', axis = 1)
X = df.values[:, 1:]
X = np.nan_to_num(X)
from sklearn.preprocessing import StandardScaler
norm_featur = StandardScaler().fit_transform(X)
clusterNum = 3
kmeans = KMeans(init = 'k-means++', n_clusters = clusterNum, n_init = 12)
kmeans.fit(X)
k_means_labels = kmeans.labels_
df['cluster'] = kmeans.labels_
k_means_cluster_centers = kmeans.cluster_centers_
area = np.pi * ( X[:, 1])**2  
plt.scatter(X[:, 0], X[:, 3], s=area, c=kmeans.labels_.astype(np.float), alpha=0.5)
plt.xlabel('Age', fontsize=18)
plt.ylabel('Income', fontsize=16)
plt.show()

CLUSTERS WITH OUT USING NORMALIZATION

使用标准化数据的代码

import numpy as np
import matplotlib.pyplot as plt
%matplotlib  inline
from sklearn.cluster import KMeans
cust_df = pd.read_csv('D:\machine learning\Cust_Segmentation.csv')
cust_df.head()
df = cust_df.drop('Address', axis = 1)
X = df.values[:, 1:]
X = np.nan_to_num(X)
from sklearn.preprocessing import StandardScaler
norm_feature = StandardScaler().fit_transform(X)
clusterNum = 3
kmeans = KMeans(init = 'k-means++', n_clusters = clusterNum, n_init = 12)
kmeans.fit(norm_feature)
k_means_labels = kmeans.labels_
df['cluster'] = kmeans.labels_
k_means_cluster_centers = kmeans.cluster_centers_
area = np.pi * ( norm_feature[:, 1])**2  
plt.scatter(norm_feature[:, 0], norm_feature[:, 3], s=area, c=kmeans.labels_.astype(np.float), 
alpha=0.5)
plt.xlabel('Age', fontsize=18)
plt.ylabel('Income', fontsize=16)
plt.show()

CLUSTER AFTER NORMALIZATION

【问题讨论】:

  • 归一化消除了数据的可变性。
  • 我猜,不,我敢肯定你没有读过这个问题。还是谢谢你。

标签: python machine-learning scikit-learn normalization k-means


【解决方案1】:

这里的收入和年龄完全不同。在您的第一个情节中,收入差异约 100 与年龄差异约 10 大致相同。但在 k-means 中,这种收入差异被认为是 10 倍大。纵轴很容易主导聚类。

这可能是“错误的”,除非您碰巧相信收入变化 1 与 10 年龄变化“相同”,以便找出相似之处。这就是你标准化的原因,它做出了不同的假设:它们同样重要。

你的第二个情节不太合理; k-means 不能产生“重叠”的簇。问题是您只绘制了您聚集的 4 个(?)维度中的 2 个。您无法绘制 4D 数据,但我怀疑如果您将 PCA 应用于结果以首先减少到二维并绘制它,您会看到分离的集群。

【讨论】:

  • 非常感谢您抽出宝贵时间。我想我现在明白了这个问题。
猜你喜欢
  • 2010-10-06
  • 2014-04-28
  • 2010-09-17
  • 1970-01-01
  • 2017-01-14
  • 2018-12-12
  • 2015-09-09
  • 2013-12-11
  • 2016-11-15
相关资源
最近更新 更多