群集中每个标签的Sklearn聚类提取ID

发布于 2025-02-11 09:37:56 字数 1029 浏览 1 评论 0原文

您好,我正在学习如何使用Scikit-Learn聚类模块。我有一个工作脚本,可以在大熊猫数据框架中读取。

df=pd.read_csv("test.csv",index_col="identifier")

我将数据框转换为一个numpy阵列

array=df.to_numpy()

,然后实现了群集并绘制为:

km=KMeans(n_clusters=25,init="random",n_init=100,max_iter=1000,tol=1e-04, random_state=0)
##get cluster labels
y_km=km.fit_predict(array)
###To plot use PCA function
pca=PCA(n_components=3)
pca_t=pca.fit_transform(array)

####
u_labels=np.unique(y_km)
fig = plt.figure(figsize=(14,10))
ax = plt.axes(projection='3d')

for i in u_labels:
    ax.scatter3D(pca_t[y_km == i , 0] , pca_t[y_km == i , 1],pca_t[y_km == i , 2],  label = i)
ax.legend()

这一切都输出了一个看起来像这样的图:

”

我想尝试获取最终输出,以启用字典或文本某种形式的文件告诉我每个标识符基于原始数组的行ID所需的群集。我很难弄清楚如何维护该信息。我尝试查看是否可以使用pandas dataframe.to_records()函数,该函数维护了DTYPES,但无法弄清楚如何将其转换为我想要的内容。

Hello I am learning how to use the Scikit-learn clustering modules right now. I have a working script that reads in a pandas dataframe.

df=pd.read_csv("test.csv",index_col="identifier")

I converted the dataframe to a numpy array

array=df.to_numpy()

Then implemented the clustering and plotted as so:

km=KMeans(n_clusters=25,init="random",n_init=100,max_iter=1000,tol=1e-04, random_state=0)
##get cluster labels
y_km=km.fit_predict(array)
###To plot use PCA function
pca=PCA(n_components=3)
pca_t=pca.fit_transform(array)

####
u_labels=np.unique(y_km)
fig = plt.figure(figsize=(14,10))
ax = plt.axes(projection='3d')

for i in u_labels:
    ax.scatter3D(pca_t[y_km == i , 0] , pca_t[y_km == i , 1],pca_t[y_km == i , 2],  label = i)
ax.legend()

This all outputs a plot that looks like this:

enter image description here

I want to try and get a final output that ouputs a dictionary or text file of some sort that tells me what cluster each identifier is in based on the row ids of the original array. I was having trouble figuring out how to maintain that information though. I tried seeing if I could use the pandas Dataframe.to_records() function which maintained the dtypes but couldn't figure out how to translate that to what I wanted.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

囚你心 2025-02-18 09:37:56

y_km以与熊猫数据框架中的行相同的顺序包含您的标签。例子:

df = pd.DataFrame({
'foo': ['one', 'one', 'one', 'two', 'two','two'],
'bar': ['A', 'B', 'C', 'A', 'B', 'C'],
},
index =  ['x', 'y', 'z', 'q', 'w', 't']
)

y_km = [1, 2, 3, 4, 5, 6]
print(pd.DataFrame(y_km, df.index))

   0
x  1
y  2
z  3
q  4
w  5
t  6

y_km contains your labels in the same order as the rows in your pandas dataframe. example:

df = pd.DataFrame({
'foo': ['one', 'one', 'one', 'two', 'two','two'],
'bar': ['A', 'B', 'C', 'A', 'B', 'C'],
},
index =  ['x', 'y', 'z', 'q', 'w', 't']
)

y_km = [1, 2, 3, 4, 5, 6]
print(pd.DataFrame(y_km, df.index))

   0
x  1
y  2
z  3
q  4
w  5
t  6
深海里的那抹蓝 2025-02-18 09:37:56

您应该尝试:

print(y_km.labels_) 

这应该为您提供每个点的标签列表。

参见对于Kmeans。

You should try :

print(y_km.labels_) 

This should give you a list of label for each point.

See the documentation for KMeans.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文