我正致力于在python中实现一个kmeans算法。我正在测试初始化我的质心的新方法,并希望实现它,看看它会对集群产生什么影响。
我的想法是从我的数据集中选择数据点,其方式是将质心初始化为数据的边缘点。
简单示例2属性示例:
让我们说这是我的输入数组
input = array([[3,3], [1,1], [-1,-1], [3,-3], [-1,1], [-3,3], [1,-1], [-3,-3]])

我想从这个数组中选择边缘点,它是3-3 -3,3。所以如果我的k是4,这些点将被选中。
在我正在处理的数据中,我的数据集中有4个和9个属性,以及大约300个数据点。
注意:我还没有找到k个<>边点的解,但是如果k是>边点,我想我会选择这4个点,然后试着把其余的点放在图的中心点上。
我还考虑过为每一列寻找max和min,然后尝试找到我的数据集的边缘,但我不知道从这些值中识别边缘的有效方法。
如果你相信这个想法行不通,我很想听听你要说什么。
问题
发布于 2017-11-25 18:26:09
使用scipy和成对距离来找出每个距离离另一个有多远:
from scipy.spatial.distance import pdist, squareform
p=pdist(input)然后,使用sqaureform将p向量转换为矩阵形状:
s=squareform(pdist(input))然后,使用numpy argwhere查找值为最大值或极值的索引,然后在输入数组中查找这些索引:
input[np.argwhere(s==np.max(p))]
array([[[ 3, 3],
[-3, -3]],
[[ 3, -3],
[-3, 3]],
[[-3, 3],
[ 3, -3]],
[[-3, -3],
[ 3, 3]]])完整的代码是:
from scipy.spatial.distance import pdist, squareform
p=pdist(input)
s=squareform(p)
input[np.argwhere(s==np.max(p))]https://stackoverflow.com/questions/47487331
复制相似问题