使用Python将产品分为产品系列

2024-05-15 21:28:26 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个数据框,其中包含来自不同生产站和生产线的产品ID和传感器以及值(1:产品通过传感器/或0:产品和传感器之间没有关系)。 以下是数据帧的一部分:

enter image description here

我想使用一种聚类方法,可以根据流程(传感器)对产品系列中的产品进行聚类

谢谢你的帮助


Tags: 数据方法id关系产品聚类传感器流程
1条回答
网友
1楼 · 发布于 2024-05-15 21:28:26

由于没有标签,我们需要一种无监督的聚类方法

例如Kmeans。下面我提供一个例子

import numpy as np
np.random.seed(0)
from sklearn.cluster import KMeans

# build fake data with only 0/1 values in the features
X = np.ones((100,10))
random_indices_rows = np.random.randint(1,100,50) 
X[random_indices_rows]=0

print(X.shape)
#(100, 10) # 100 samples and 10 variables/sensors

# the clustering model
kmeans = KMeans(n_clusters=2, random_state=0).fit(X)
kmeans.labels_

print(kmeans.labels_)

#array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 1,
#       1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 1, 0, 1, 0, 0, 0,
#       0, 1, 0, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1,
#       1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 0, 0,
#       1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], dtype=int32)

相关问题 更多 >