为什么当我使用规范化数据时,我在kmean中得到嵌套的集群,而当我使用非标准化数据时得到非重叠的集群



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

代码不使用标准化数据

import numpy as np
import matplotlib.pyplot as plt
%matplotlib  inline
from sklearn.cluster import KMeans
cust_df = pd.read_csv('D:machine learningCust_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()

使用归一化的OUT聚类

使用标准化数据的代码

import numpy as np
import matplotlib.pyplot as plt
%matplotlib  inline
from sklearn.cluster import KMeans
cust_df = pd.read_csv('D:machine learningCust_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()

标准化后的集群

这里的收入和年龄是完全不同的。在你的第一幅图中,收入相差约100与年龄相差约10大致相同。但在k均值中,收入差异被认为是10倍大。垂直轴很容易控制聚类。

这可能是"错误的",除非你碰巧认为收入变化1与10岁的变化"相同",以找出相似之处。这就是为什么要进行标准化,这就产生了一个不同的假设:它们同样重要。

你的第二个情节不太合理;k-均值不能产生"重叠"聚类。问题是,你只绘制了你聚类的4个维度中的2个。你不能绘制4D数据,但我怀疑,如果你将PCA应用于结果,先减少到2个维度并绘制它,你会看到分离的聚类。

最新更新