如何在python中设置bestLabel向量,bestLabel的大小是多少,这个样本有两个位置。
compactness,label,center=cv2.kmeans(samples,K,bestLabel,criteria,10,cv2.KMEANS_USE_INITIAL_LABELS)
我正在努力将我的初始手段给予kmeans。我的样本是70x2阵列,包括70个样本,有2个特征。 K = 2。 PLZ给我一个bestLabel generate的python示例。
您必须提供所需的初始质心和每个样品的初始标签。
import cv2
import numpy as np
def main():
seed = 1
nclusters = 2
np.random.seed(seed) # Get always same random numpys
data = np.random.random(size=(100, 100)).astype(np.float32)
centers = np.array([[75], [125]])
labels = np.random.randint(nclusters,
size=(data.shape[0], data.shape[1]),
dtype=np.int32)
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 100, 1.0)
reshaped_data = np.reshape(data, data.shape[0] * data.shape[1])
reshaped_labels = np.reshape(labels, (labels.shape[0] * labels.shape[1], 1))
_, new_labels, center = cv2.kmeans(data=reshaped_data,
K=nclusters,
bestLabels=reshaped_labels,
criteria=criteria,
attempts=10,
flags=cv2.KMEANS_USE_INITIAL_LABELS,
centers=centers)
if __name__ == "__main__":
main()
我读了documentation。我没有找到用向量初始化centorids的选项。设置初始质心有两种可能的方法:
这是一个运行的例子,k表示使用opencv在python中。
import numpy as np
import cv2
from matplotlib import pyplot as plt
X = np.random.randint(25,50,(25,2))
Y = np.random.randint(60,85,(25,2))
Z = np.vstack((X,Y))
# convert to np.float32
Z = np.float32(Z)
# define criteria and apply kmeans()
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 10, 1.0)
ret,label,center=cv2.kmeans(Z,2,None,criteria,10,cv2.KMEANS_RANDOM_CENTERS)
# Now separate the data, Note the flatten()
A = Z[label.ravel()==0]
B = Z[label.ravel()==1]
# Plot the data
plt.scatter(A[:,0],A[:,1])
plt.scatter(B[:,0],B[:,1],c = 'r')
plt.scatter(center[:,0],center[:,1],s = 80,c = 'y', marker = 's')
plt.xlabel('Height'),plt.ylabel('Weight')
plt.show()
如果必须设置初始中心,可以使用sklearn库。文档here。