您如何在 scipy 中访问 ward/centroid/median 集群?
How do you access ward/centroid/median clustering in scipy?
当使用 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()
的文档至少可以改进。
scipy.cluster.hierarchy.linkage(y, method)
returns 当 y 是距离矩阵或数据矩阵时,单个、完整、平均、加权的正确结果。但是对于centroid、median和ward方法,y必须是数据矩阵,如果y是距离矩阵就会出错。我同意文档不清楚。
from scipy.cluster.hierarchy import linkage
from scipy.spatial.distance import pdist
inp = np.loadtxt('iris.txt',delimiter=",", usecols=(0,1,2,3))
x = np.asarray(inp)
Y = pdist(x,'euclidean')
res_linkage = linkage(x,"weighted")`
您可以通过在 linkage() 函数中输入数据矩阵 x 或欧氏距离矩阵 Y 来测试上面的代码。
我还发现,与 R 中的等效实现相比,hclust
包,scipy.cluster.hierarchy.linkage
returns 质心、中值和病房方法的结果不同。似乎 scipy.cluster.hierarchy.linkage
在更新新合并的集群与现有集群的距离时包含一些错误。
当使用 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()
的文档至少可以改进。
scipy.cluster.hierarchy.linkage(y, method)
returns 当 y 是距离矩阵或数据矩阵时,单个、完整、平均、加权的正确结果。但是对于centroid、median和ward方法,y必须是数据矩阵,如果y是距离矩阵就会出错。我同意文档不清楚。
from scipy.cluster.hierarchy import linkage
from scipy.spatial.distance import pdist
inp = np.loadtxt('iris.txt',delimiter=",", usecols=(0,1,2,3))
x = np.asarray(inp)
Y = pdist(x,'euclidean')
res_linkage = linkage(x,"weighted")`
您可以通过在 linkage() 函数中输入数据矩阵 x 或欧氏距离矩阵 Y 来测试上面的代码。
我还发现,与 R 中的等效实现相比,hclust
包,scipy.cluster.hierarchy.linkage
returns 质心、中值和病房方法的结果不同。似乎 scipy.cluster.hierarchy.linkage
在更新新合并的集群与现有集群的距离时包含一些错误。