关于我
一世
;设置集群指标c i
词
成为k
ķ
根据等式c i = argmin k || x i −μ k || 2
ci = argmink || xi−μk || 2
,其中k
ķ
是集群之一。
例如我的重点 一世 位于群集中,中心距该点最近。
编写一个名为assign_clusters_k_means的函数,请接受两个参数: •points:每个点的二维二维数组 •簇:每个簇的质心的二维二维数组。
确定哪个聚类质心最接近每个点。
返回一个二维numpy数组,其中每行指示一个点靠近壁簇,因此也将其分配给:
例如[0,1,0,...,0]指示该点已分配给第二个群集,并且 [0,0,...,1]表示该点已分配给最后一个聚类
以下是我到目前为止所做的。您能否就我可能需要做的事情提供一些建议。 将numpy导入为np 将熊猫作为pd导入 points = np.array([[0,1],[2,2],[5,4],[3,6],[4,2]]) 簇= np.array([[0,1],[5,4]]) def Assign_clusters_k_means(点,簇): distances = np.sqrt((((points-clusters [:, np.newaxis])** 2).sum(axis = 2)) cluster_weights = np.array(np.argmin(距离,轴= 0)) 返回cluster_weights cluster_weights = Assign_clusters_k_means(点,簇) 打印(cluster_weights)
此输出为 [0 0 1 1 1]
def Assign_clusters_k_means(点,簇): “” 确定每个点最近的聚类,并返回一个指示最近聚类的数组
Positional Arguments:
points: a 2-d numpy array where each row is a different point, and each
column indicates the location of that point in that dimension
clusters: a 2-d numpy array where each row is a different centroid cluster;
each column indicates the location of that centroid in that dimension
Example:
points = np.array([[0,1], [2,2], [5,4], [3,6], [4,2]])
clusters = np.array([[0,1],[5,4]])
cluster_weights = assign_clusters_k_means(points, clusters)
print(cluster_weights) #--> np.array([[1, 0],
[1, 0],
[0, 1],
[0, 1],
[0, 1]])
"""
# NB: "cluster_weights" is used as a common term between functions
# the name makes more sense in soft-clustering contexts