我用 SciPy 制作了以下树形图:
\n\n# create the dendrogram\nfrom scipy.cluster import hierarchy as hc\nfrom scipy.stats import spearmanr as sp\nimport matplotlib.pyplot as plt\n%matplotlib inline\n\ncorr = np.round(sp(data_for_cluster).correlation, 4)\ncorr_condensed = hc.distance.squareform(1-corr)\nz = hc.linkage(corr_condensed, method=\'average\')\nfig = plt.figure(figsize=(20,35))\ndendrogram = hc.dendrogram(z, labels=vals_to_keep, orientation=\'left\', \nleaf_font_size=14)\nplt.show()\nRun Code Online (Sandbox Code Playgroud)\n\n给出了下图:
\n\n\n\n我不太确定如何解释树状图中的颜色。从文档中,它给出了以下参数描述color_threshold:
\n\n\n为了简洁起见,让 t 为 color_threshold。如果 k 是低于剪切阈值 t 的第一个节点,则将集群节点 k 下面的所有后代链接着色为相同颜色。连接距离大于或等于阈值的节点的所有链接都显示为蓝色。如果 t 小于或等于 0,则所有节点都呈蓝色。如果 color_threshold 为 None 或 \xe2\x80\x98default\xe2\x80\x99(与 MATLAB(TM) 行为相对应),则阈值设置为 0.7*max(Z[:,2])。
\n
然而,令我困惑的是,为什么一些看起来非常接近的簇没有给出不同的颜色,而其他相距较远的簇......却给出了不同的颜色。
\n\n我认为,在其他条件相同的情况下,簇越靠近,它们就越有可能是不同的颜色,以代表簇成员资格,但情况似乎并非如此。
\n我正在阅读有关Delaunay (scipy)的内容的文章并发现了代码:
\nimport numpy as np\npoints = np.array([[0, 0], [0, 1.1], [1, 0], [1, 1]])\n\nfrom scipy.spatial import Delaunay\ntri = Delaunay(points)\n\nimport matplotlib.pyplot as plt\nplt.triplot(points[:,0], points[:,1], tri.simplices.copy())\nplt.plot(points[:,0], points[:,1], \'o\')\nplt.show()\nRun Code Online (Sandbox Code Playgroud)\n据我了解,单纯形是三角形到更高维度的推广。
\n我不明白下面代码的含义,希望帮助理解它:
\n# Point indices and coordinates for the two triangles forming the triangulation:\n\ntri.simplices\narray([[3, 2, 0],\n [3, 1, 0]], dtype=int32)\n\npoints[tri.simplices]\narray([[[ 1. , 1. ],\n [ 1. , 0. ],\n [ 0. , 0. ]],\n [[ 1. , 1. ],\n [ 0. , 1.1],\n [ 0. , 0. …Run Code Online (Sandbox Code Playgroud) 我有自己的三角剖分算法,该算法基于Delaunay的条件和渐变创建三角剖分,以使三角形与渐变对齐。
上面的描述与问题无关,但是对于上下文是必需的。
现在我想使用三角剖分与scipy.interpolate.LinearNDInterpolator进行插值。
使用scipy的Delaunay,我将执行以下操作
import numpy as np
import scipy.interpolate
import scipy.spatial
points = np.random.rand(100, 2)
values = np.random.rand(100)
delaunay = scipy.spatial.Delaunay(points)
ip = scipy.interpolate.LinearNDInterpolator(delaunay, values)
Run Code Online (Sandbox Code Playgroud)
该delaunay对象具有delaunay.points和delaunay.simplices,构成了三角剖分。我自己的三角剖分得到的信息完全相同,但是scipy.interpolate.LinearNDInterpolator需要一个scipy.spatial.Delaunay对象。
我想我需要继承scipy.spatial.Delaunay并实现相关方法。但是,我不知道要到达那里需要哪些。
我在许多参考资料中都被告知 KDTree 是一种为大数据寻找最近邻的快速方法。我当前的问题是为给定的数据 A 找到 X 中最近的点。详细说明,目前 X 有 1,000,000 个数值数据,A 由 10,000 个组成。我想为 A 中的每个点找到 X 中最近的点。因此,结果应该是 10,000 个索引,指示 X 中的数据点。
当我使用带有 for 循环的 cdist(来自 scipy.spatial)来查找 A 中每个数据的最近点时,大约需要半小时(1972 秒),而使用 n_jobs 时 cKDTree.query 需要大约 50 分钟(2839 秒) = 4。
cdist 的代码如下:
t = time.time()
nn = np.array([])
jump = 1000
nloop = np.ceil(A.shape[0]/jump).astype(int)
for ii in range(nloop):
temp = cdist(X, A[ii*jump:(ii+1)*jump])
nn = np.append(nn, np.argmin(temp, axis = 0))
print('Elapsed time: ', time.time() - t) # this was 1926 seconds (a …Run Code Online (Sandbox Code Playgroud) 我想为10060 个记录/点创建一个距离邻近矩阵,其中每个记录/点有23 个使用欧氏距离作为度量的属性。我使用嵌套 for 循环编写代码来计算每个点之间的距离(导致(n(n-1))/2)计算)。花了很长时间(大约8分钟)。当我使用 cdist 时,它花费的时间要少得多(仅 3 秒!!!)。当我查看源代码时,cdist 还使用嵌套的 for 循环,而且它进行了n^2 次计算(这大于我的逻辑所做的比较次数)。是什么让 cdist 执行得更快并给出正确的输出?请帮我理解。提前致谢。
我试图scipy.special.rel_entr在GitHub Repo中查找函数的代码源,但找不到rel_entr函数定义。
我还尝试查看此代码:https://github.com/scipy/scipy/pull/6522/files#diff-0