- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在尝试学习如何使用 SciPy
在 Python
中使用 dendrograms
。我想获得集群并能够将它们可视化;我听说 hierarchical clustering
和 dendrograms
是最好的方法。
我怎样才能在特定距离“砍”树?
我在 https://joernhees.de/blog/2015/08/26/scipy-hierarchical-clustering-and-dendrogram-tutorial/#Inconsistency-Method 上查找了教程但是这个家伙使用 **kwargs
做了一些非常困惑的包装函数(他称他的阈值为 max_d
)
下面是我的代码和情节;为了重现性,我尝试尽可能地注释它:
from __future__ import print_function
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from scipy.cluster.hierarchy import dendrogram,linkage,fcluster
from scipy.spatial import distance
np.random.seed(424173239) #43984
#Dims
n,m = 20,7
#DataFrame: rows = Samples, cols = Attributes
attributes = ["a" + str(j) for j in range(m)]
DF_data = pd.DataFrame(np.random.random((n, m)), columns = attributes)
A_dist = distance.cdist(DF_data.as_matrix().T, DF_data.as_matrix().T)
#(i) . Do the labels stay in place from DF_data for me to do this?
DF_dist = pd.DataFrame(A_dist, index = attributes, columns = attributes)
#Create dendrogram
fig, ax = plt.subplots()
Z = linkage(distance.squareform(DF_dist.as_matrix()), method="average")
D_dendro = dendrogram(Z, labels = attributes, ax=ax) #create dendrogram dictionary
threshold = 1.6 #for hline
ax.axhline(y=threshold, c='k')
plt.show()
#(ii) How can I "cut" the tree by giving it a distance threshold?
#i.e. If I cut at 1.6 it would make (a5 : cluster_1 or not in a cluster), (a2,a3 : cluster_2), (a0,a1 : cluster_3), and (a4,a6 : cluster_4)
#link_1 says use fcluster
#This -> fcluster(Z, t=1.5, criterion='inconsistent', depth=2, R=None, monocrit=None)
#gives me -> array([1, 1, 1, 1, 1, 1, 1], dtype=int32)
print(
len(set(D_dendro["color_list"])), "^ # of colors from dendrogram",
len(D_dendro["ivl"]), "^ # of labels",sep="\n")
#3
#^ # of colors from dendrogram it should be 4 since clearly (a6, a4) and a5 are in different clusers
#7
#^ # of labels
link_1 : How to compute cluster assignments from linkage/distance matrices in scipy in Python?
最佳答案
color_threshold
是我一直在寻找的方法。当 color_palette
对于生成的簇数量来说太小时,它并没有真正的帮助。将下一步迁移到 Bigger color-palette in matplotlib for SciPy's dendrogram (Python)如果有人可以提供帮助。
关于python - 在距离高度处从 SciPy 切割树状图/聚类树,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36523789/
Python 中是否有任何预制的优化工具/库来切割/切片值“小于”某物的列表? 问题来了:假设我有一个列表: a=[1,3,5,7,9] 我想删除所有 2 的项目,将会有很多迭代)。 我也可以使用二进
我创建了一个 fiddle 来更好地展示我想问的问题。 angle crop of pseudo element 这是一张显示所需结果的图片: li.active::after { conte
我有一个字符串如下: 2012/02/01,13:27:20,872226816,-1174749184,2136678400,2138578944,-17809408,2147352576 我想提取
RESTEasy 看起来会忽略尾部斜杠,因此我无法编写带有和不带有尾部斜杠的两个不同的 Web 服务,此示例显示: @Path("foo") public class TestClass {
mov rax,QWORD PTR [rbp-0x10] mov eax,DWORD PTR [rax] add eax,0x1 mov DWORD PTR [rbp-0x14], eax 下
我有以下 xml: External Vendor File External vendor file for so
我在 Python 中工作,并在 Pandas 中使用剪切功能。我想让我的 pd.cut 中的 bins 基于用户定义的逗号分隔整数,具有预定义的上限和下限。换句话说,我想将 bins 预定义为 [0
我正在尝试嵌入 ShareThis将代码写入使用 Bootstrap 创建的网站。 问题是由于某种原因,两个按钮的底部都被切断了,而且我似乎无法使用 Bootstrap 列偏移将按钮居中,因为使用这种
我正在尝试模拟无线传感器节点网络,以研究网络的稳健性。我面临以下问题: 我有一个具有一些边缘容量的节点网络。这相当于算法中的网络流问题。有一个源节点(检测某些事件)和一个接收节点(我的基站)。现在,我
我有一个 UIImageView,带有特定的图像。我还有一个形状奇怪的 UIBezierPath。我想将图像剪切成该形状并返回该形状的新图像。 形式为: func getCut(bezier:UIBe
我有一个大型的全局 .nc 文件数据集,我正试图将它们裁剪到一个较小的区域。我将此区域存储为 .shp 文件。 我曾尝试使用 Qgis 中的 gdal,但需要通过转换每个变量来做到这一点,我必须为所有
我正在使用以下命令来剪切 FLAC 文件:ffmpeg -i input.flac -ss 10s -t 10s -c copy output.flacoutput.flac包含正确的音频持续时间。但
我希望为以下组件制作足迹: AG EMCO HV power supply 现在,这个组件可以通过电路板安装,虽然它不是一个很高的组件,但我希望探索这个选项。我想一个附带问题是:PCB 制造商有多喜欢
我目前正在用 C++ 编写一个小程序(在 Mac 上,其中包含一些 C 代码)并且需要从套接字中检索二进制数据(效果很好),但我还需要切断 HTTP标题。 基本上,我连接到服务器,发送 HTTP GE
我想制作一个Python程序,其中在文本文件中给出DNA序列。它有超过9000个字符。我必须将序列剪成 3 个字符所以我们的框架从1到3读取,然后4到6,然后7到9,这被称为密码子。 例如,序列是 A
我正在开发一个迷你游戏,如果我将 DIN-Ax 作为输入,我需要找出可以在 DIN-A0 纸上画多少行。例如: ;car horizontal and cdr vertical lines (defi
我在下面有这个名为 monitor.log 的报告文件 switch#sh mac address-table int g1/0/1 Mac Address Table -----
我的设备上有 MPEG-TS 文件。我想从设备上的文件开始处截断一个相当准确的时间。 使用 FFmpegWrapper作为基地,我希望能够实现这一目标。 不过,我对 ffmpeg 的 C API 有点
我导出了两个字段:name和header从数据库中使用: SELECT name, header INTO OUTFILE '/var/lib/mysql-files/myfile.txt' FIEL
这个问题在这里已经有了答案: How do you parse and process HTML/XML in PHP? (31 个答案) 关闭 9 年前。 我尝试转换以下字符串 st
我是一名优秀的程序员,十分优秀!