- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我使用 scipy.cluster.hierarchy 使用“余弦”相似性度量对一组点进行层次聚类。例如,我有:
import scipy.cluster.hierarchy as hac
import matplotlib.pyplot as plt
Points =
np.array([[ 0. , 0.23508573],
[ 0.00754775 , 0.26717266],
[ 0.00595464 , 0.27775905],
[ 0.01220563 , 0.23622067],
[ 0.00542628 , 0.14185873],
[ 0.03078922 , 0.11273108],
[ 0.06707743 ,-0.1061131 ],
[ 0.04411757 ,-0.10775407],
[ 0.01349434 , 0.00112159],
[ 0.04066034 , 0.11639591],
[ 0. , 0.29046682],
[ 0.07338036 , 0.00609912],
[ 0.01864988 , 0.0316196 ],
[ 0. , 0.07270636],
[ 0. , 0. ]])
z = hac.linkage(Points, metric='cosine', method='complete')
labels = hac.fcluster(z, 0.1, criterion="distance")
plt.scatter(Points[:, 0], Points[:, 1], c=labels.astype(np.float))
plt.show()
由于我使用余弦度量,在某些情况下,两个向量的点积可能为负,或者某些向量的范数可能为零。这意味着 z 输出将包含一些对 fcluster 无效的负数或无限元素(如下所示):
z =
[[ 0.00000000e+00 1.00000000e+01 0.00000000e+00 2.00000000e+00]
[ 1.30000000e+01 1.50000000e+01 0.00000000e+00 3.00000000e+00]
[ 8.00000000e+00 1.10000000e+01 4.26658708e-13 2.00000000e+00]
[ 1.00000000e+00 2.00000000e+00 2.31748880e-05 2.00000000e+00]
[ 3.00000000e+00 4.00000000e+00 8.96700489e-05 2.00000000e+00]
[ 1.60000000e+01 1.80000000e+01 3.98805492e-04 5.00000000e+00]
[ 1.90000000e+01 2.00000000e+01 1.33225099e-03 7.00000000e+00]
[ 5.00000000e+00 9.00000000e+00 2.41120340e-03 2.00000000e+00]
[ 6.00000000e+00 7.00000000e+00 1.52914684e-02 2.00000000e+00]
[ 1.20000000e+01 2.20000000e+01 3.52441432e-02 3.00000000e+00]
[ 2.10000000e+01 2.40000000e+01 1.38662986e-01 1.00000000e+01]
[ 1.70000000e+01 2.30000000e+01 6.99056531e-01 4.00000000e+00]
[ 2.50000000e+01 2.60000000e+01 1.92543748e+00 1.40000000e+01]
[ -1.00000000e+00 2.70000000e+01 inf 1.50000000e+01]]
为了解决这个问题,我检查了 linkage() 函数,我需要在其中检查 _hierarchy.linkage() 方法。我使用 pycharm 文本编辑器,当我要求“链接”源代码时,它在目录中打开了一个名为“_hierarchy.py”的 python 文件,如下所示:
.PyCharm40/system/python_stubs/-1247972723/scipy/cluster/_hierarchy.py
此 python 文件没有对所有包含的函数进行任何定义。
我想知道修改此函数的正确来源是什么,或者是否有其他方法可以解决此问题。我将不胜感激你的帮助和提示。
最佳答案
您的数据集中有一个零向量 0 0
。对于此类数据,余弦距离未定义,因此您使用的距离函数不合适!这是一个无法轻易弥补的定义差距。 inf
与 0
一样不正确。到 0 0
的距离 不能 在没有矛盾的情况下定义。您不得对此类数据使用余弦。
回到您的实际问题:_hierarchy
是一个 Cython 模块。 它不是纯 python,而是编译为 native 代码。你可以很容易地在 Github 上看到源代码:
https://github.com/scipy/scipy/blob/master/scipy/cluster/_hierarchy.pyx
关于linux - fcluster 包中具有余弦相似性度量的层次聚类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30488156/
我有一个例程,它将接受 union 参数 d、theta、a 和 alpha 作为输入,并将生成相应的 4x4 齐次矩阵作为输出。我已经测试了我的矩阵乘法,它确实工作正常。我将从输入中得到 5 个矩阵
我有以下代码使用预先计算的内存表执行 Sin/Cos 函数。在下面的例子中,表格有 1024*128 个项目,涵盖了从 0 到 2pi 的所有 Sin/Cos 值。我知道我可以使用 Sin/Cos 对
问题是不言自明的。我看过几个 pi 的例子,但没有看到 trigo 函数。也许可以使用泰勒级数 as done here但我不完全确定如何在 python 中实现它。特别是如何存储这么多数字。我应该提
我开始学习 Python 中的数学模块,并试图围绕 Python 中的正弦、余弦和正切的三角函数。 我花了一些时间学习更多关于三角学的知识,并了解了直角三角形的基本公式是如何工作的: 正弦函数: si
我一个月前才开始学习这门 C++ 类(class)。现在我被分配去写一个程序来计算这个。我不知道我做错了什么。 #include #include float gatherl1(); float
这个问题在这里已经有了答案: Python cos(90) and cos(270) not 0 (3 个答案) 关闭 9 年前。 有没有办法获得角度(以弧度为单位)的精确正切/余弦/正弦? mat
这个问题在这里已经有了答案: Java Math.cos(Math.toRadians()) returns weird values (4 个答案) 关闭 10 年前。 我正在编写一个程序,我必须
我做了一个简单的计算器作为我的第一个android程序,现在我想给它添加trigonometry,log等函数。在 C 中,我们必须包含 math library 才能这样做,我似乎无法弄清楚你是如何
我正面临 objective-c 中 cos 函数的奇怪问题。我安装了带有 iOS 4.3 SDK 的 xcode 4.1.1。 我正在计算一个数的余弦值: y= cos(x*M_PI/180) 这将
尝试将以下 php 方法转换为在 .less 样式表中使用: 在 Less 中,如何在不使用特定语言的 cos()/sin() 函数的情况下实现正弦/余弦方法? .rotate(@deg) {
可以使用 iPhone 3GS 或 Pandora 的人请测试我刚刚编写的以下组装程序吗? 它应该在 NEON 矢量 FPU 上非常快速地计算正弦和余弦。我知道它编译得很好,但没有足够的硬件我无法测试
我为泰勒级数编写了以下函数来计算余弦。 double cosine(int x) { x %= 360; // make it less than 360 double rad = x
我是一名优秀的程序员,十分优秀!