gpt4 book ai didi

python - 你如何访问 scipy 中的 ward/centroid/median 聚类?

转载 作者:行者123 更新时间:2023-11-28 16:33:49 24 4
gpt4 key购买 nike

当使用 scipy.spatial.distance.pdist 创建压缩距离矩阵并将其传递给 ward 时,出现此错误:

Valid methods when the raw observations are omitted are 'single', 'complete', 'weighted', and 'average' error. 

虽然文档说 linkage() 函数需要一个压缩距离矩阵。我该如何解决这个问题?

foo = np.random.randint(3, size=(10,10))
scipy.spatial.distance.pdist(foo)
scipy.cluster.hierarchy.linkage(foo)
bar = scipy.spatial.distance.pdist(foo)
scipy.cluster.hierarchy.linkage(bar, method='ward')

给出:

 Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/dist-packages/scipy /cluster/hierarchy.py", line 627, in linkage
raise ValueError("Valid methods when the raw observations are "
ValueError: Valid methods when the raw observations are omitted are 'single', 'complete', 'weighted', and 'average'.

我搜索了一下,找到了 this link ,表明其他一些人也有问题,但我无法找到一种解决方法来以 scipy 可以接受的形式提供数据。

最佳答案

来自文档字符串:

y : ndarray

A condensed or redundant distance matrix. A condensed distance matrix is a flat array containing the upper triangular of the distance matrix. This is the form that pdist returns. Alternatively, a collection of m observation vectors in n dimensions may be passed as an m by n array.

传入您的原始观察 x 维度数组 foo 似乎有效:

scipy.cluster.hierarchy.linkage(foo, method='ward')

给出:

array([[  1.        ,   2.        ,   2.23606798,   2.        ],
[ 5. , 8. , 2.23606798, 2. ],
[ 3. , 7. , 2.64575131, 2. ],
[ 9. , 11. , 2.64575131, 3. ],
[ 0. , 10. , 3.31662479, 3. ],
[ 12. , 13. , 3.71483512, 5. ],
[ 6. , 14. , 4.12310563, 4. ],
[ 4. , 16. , 4.17133072, 5. ],
[ 15. , 17. , 5.5136195 , 10. ]])

我同意 linkage() 的文档至少可以改进。

关于python - 你如何访问 scipy 中的 ward/centroid/median 聚类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29152518/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com