我怎样才能找到从质心到每个簇中所有数据点的平均距离。我能够从每个群集的质心找到每个点(在我的数据集中)的欧氏距离。现在我想查找从质心到每个群集中所有数据点的平均距离。 从每个质心计算平均距离的好方法是什么? 到目前为止,我已经做到了这一点..Sklearn:每个簇的质心的平均距离
def k_means(self):
data = pd.read_csv('hdl_gps_APPLE_20111220_130416.csv', delimiter=',')
combined_data = data.iloc[0:, 0:4].dropna()
#print combined_data
array_convt = combined_data.values
#print array_convt
combined_data.head()
t_data=PCA(n_components=2).fit_transform(array_convt)
#print t_data
k_means=KMeans()
k_means.fit(t_data)
#------------k means fit predict method for testing purpose-----------------
clusters=k_means.fit_predict(t_data)
#print clusters.shape
cluster_0=np.where(clusters==0)
print cluster_0
X_cluster_0 = t_data[cluster_0]
#print X_cluster_0
distance = euclidean(X_cluster_0[0], k_means.cluster_centers_[0])
print distance
classified_data = k_means.labels_
#print ('all rows forst column........')
x_min = t_data[:, 0].min() - 5
x_max = t_data[:, 0].max() - 1
#print ('min is ')
#print x_min
#print ('max is ')
#print x_max
df_processed = data.copy()
df_processed['Cluster Class'] = pd.Series(classified_data, index=df_processed.index)
#print df_processed
y_min, y_max = t_data[:, 1].min(), t_data[:, 1].max() + 5
xx, yy = np.meshgrid(np.arange(x_min, x_max, 1), np.arange(y_min, y_max, 1))
#print ('the mesh grid is: ')
#print xx
Z = k_means.predict(np.c_[xx.ravel(), yy.ravel()])
Z = Z.reshape(xx.shape)
plt.figure(1)
plt.clf()
plt.imshow(Z, interpolation='nearest',
extent=(xx.min(), xx.max(), yy.min(), yy.max()),
cmap=plt.cm.Paired,
aspect='auto', origin='lower')
#print Z
plt.plot(t_data[:, 0], t_data[:, 1], 'k.', markersize=20)
centroids = k_means.cluster_centers_
inert = k_means.inertia_
plt.scatter(centroids[:, 0], centroids[:, 1],
marker='x', s=169, linewidths=3,
color='w', zorder=8)
plt.xlim(x_min, x_max)
plt.ylim(y_min, y_max)
plt.xticks(())
plt.yticks(())
plt.show()
总之我想从集群的重心计算特别是集群中的所有数据点的平均距离,因为我需要清理的基础上,我的数据这意味着距离
这会给我,我已经得到了每个集群的质心坐标,现在我需要计算在集群中的质心的所有数据点的平均距离。 – Rezwan