在执行 K-Means 算法时检索索引
Retrieve Indices while performing K-Means algorithm
我有以下形式的数据框;
dict_new={'var1':[1,0,1,0,2],'var2':[1,1,0,2,0],'var3':[1,1,1,2,1]}
pd.DataFrame(dict_new,index=['word1','word2','word3','word4','word5'])
请注意,实际数据集很大,上面的例子是为了简单起见。然后我在 sickit-learn 中执行了 K-means 算法,为了简单起见取了 2 个簇质心。
from sklearn.cluster import KMeans
num_clusters = 2
km = KMeans(n_clusters=num_clusters,verbose=1)
km.fit(dfnew.to_numpy())
假设新的簇质心由
给出
centers=km.cluster_centers_
centers
array([[0. , 1.5 , 1.5 ],
[1.33333333, 0.33333333, 1. ]])
目标是为每个聚类质心找到两个最接近的词,即为每个聚类中心识别两个最接近的词。我使用了 scipy
包中的 distance_matrix
,并将输出作为 2 x 5
矩阵,对应于 2 个中心和 5 个单词。请看下面的代码。
from scipy.spatial import distance_matrix
distance_matrix(centers,np.asmatrix(dfnew.to_numpy()))
array([[1.22474487, 0.70710678, 1.87082869, 0.70710678, 2.54950976],
[0.74535599, 1.49071198, 0.47140452, 2.3570226 , 0.74535599]])
但是我们在这里看不到索引这个词。所以我无法为每个质心识别两个最接近的词。我能否就如何检索索引(在原始数据框中定义)寻求帮助?感谢帮助。
鉴于我理解您想正确地做的事情,这里是一个关于如何找到单词索引的最小工作示例。
首先,让我们生成一个类似的可重现环境
# import packages
import pandas as pd
import numpy as np
from sklearn.cluster import KMeans
from scipy.spatial.distance import cdist
from scipy.spatial import distance_matrix
# set up the DataFrame
dict_new={'var1':[1,0,1,0,2],'var2':[1,1,0,2,0],'var3':[1,1,1,2,1]}
df = pd.DataFrame(dict_new,index= ['word1','word2','word3','word4','word5'])
# get the cluster centers
kmeans = KMeans(n_clusters=2, random_state=0).fit(np.array(df))
centers = kmeans.cluster_centers_
如果你只需要知道一个最接近的词
现在,如果您想使用距离矩阵,您可以(改为):
def closest(df, centers):
# define the distance matrix
mat = distance_matrix(centers, np.asmatrix(df.to_numpy()))
# get an ordered list of the closest word for each cluster centroid
closest_words = [df.index[i] for i in np.argmin(mat, axis=1)]
return closest_words
# example of it working for all centroids
print(closest(df, centers))
# > ['word3', 'word2']
如果你需要知道最接近的2个词
现在,如果我们想要两个最接近的词:
def two_closest(df, centers):
# define the distance matrix
mat = distance_matrix(centers, np.asmatrix(df.to_numpy()))
# get an ordered list of lists of the closest two words for each cluster centroid
closest_two_words = [[df.index[i] for i in l] for l in np.argsort(mat, axis=1)[:,0:2]]
return closest_two_words
# example of it working for all centroids
print(two_closest(df, centers))
# > [['word3', 'word5'], ['word2', 'word4']]
如果这不是您想要做的或者我的回答不符合您的需求,请告诉我!如果我解决了您的问题,请不要忘记将问题标记为已回答。
我有以下形式的数据框;
dict_new={'var1':[1,0,1,0,2],'var2':[1,1,0,2,0],'var3':[1,1,1,2,1]}
pd.DataFrame(dict_new,index=['word1','word2','word3','word4','word5'])
请注意,实际数据集很大,上面的例子是为了简单起见。然后我在 sickit-learn 中执行了 K-means 算法,为了简单起见取了 2 个簇质心。
from sklearn.cluster import KMeans
num_clusters = 2
km = KMeans(n_clusters=num_clusters,verbose=1)
km.fit(dfnew.to_numpy())
假设新的簇质心由
给出centers=km.cluster_centers_
centers
array([[0. , 1.5 , 1.5 ],
[1.33333333, 0.33333333, 1. ]])
目标是为每个聚类质心找到两个最接近的词,即为每个聚类中心识别两个最接近的词。我使用了 scipy
包中的 distance_matrix
,并将输出作为 2 x 5
矩阵,对应于 2 个中心和 5 个单词。请看下面的代码。
from scipy.spatial import distance_matrix
distance_matrix(centers,np.asmatrix(dfnew.to_numpy()))
array([[1.22474487, 0.70710678, 1.87082869, 0.70710678, 2.54950976],
[0.74535599, 1.49071198, 0.47140452, 2.3570226 , 0.74535599]])
但是我们在这里看不到索引这个词。所以我无法为每个质心识别两个最接近的词。我能否就如何检索索引(在原始数据框中定义)寻求帮助?感谢帮助。
鉴于我理解您想正确地做的事情,这里是一个关于如何找到单词索引的最小工作示例。
首先,让我们生成一个类似的可重现环境
# import packages
import pandas as pd
import numpy as np
from sklearn.cluster import KMeans
from scipy.spatial.distance import cdist
from scipy.spatial import distance_matrix
# set up the DataFrame
dict_new={'var1':[1,0,1,0,2],'var2':[1,1,0,2,0],'var3':[1,1,1,2,1]}
df = pd.DataFrame(dict_new,index= ['word1','word2','word3','word4','word5'])
# get the cluster centers
kmeans = KMeans(n_clusters=2, random_state=0).fit(np.array(df))
centers = kmeans.cluster_centers_
如果你只需要知道一个最接近的词
现在,如果您想使用距离矩阵,您可以(改为):
def closest(df, centers):
# define the distance matrix
mat = distance_matrix(centers, np.asmatrix(df.to_numpy()))
# get an ordered list of the closest word for each cluster centroid
closest_words = [df.index[i] for i in np.argmin(mat, axis=1)]
return closest_words
# example of it working for all centroids
print(closest(df, centers))
# > ['word3', 'word2']
如果你需要知道最接近的2个词
现在,如果我们想要两个最接近的词:
def two_closest(df, centers):
# define the distance matrix
mat = distance_matrix(centers, np.asmatrix(df.to_numpy()))
# get an ordered list of lists of the closest two words for each cluster centroid
closest_two_words = [[df.index[i] for i in l] for l in np.argsort(mat, axis=1)[:,0:2]]
return closest_two_words
# example of it working for all centroids
print(two_closest(df, centers))
# > [['word3', 'word5'], ['word2', 'word4']]
如果这不是您想要做的或者我的回答不符合您的需求,请告诉我!如果我解决了您的问题,请不要忘记将问题标记为已回答。