IndexError: index 8 is out of bounds for axis 1 with size 8

IndexError: index 8 is out of bounds for axis 1 with size 8

我正在做一个关于层次聚类的项目,我正在编写一些代码,其中我使用 'affinity' 和 'linkage' 的每个可能组合执行 AgglomerativeClustering,这是您可以设置的两个参数.当我尝试将数据与算法相匹配时,问题就出现了。数据集具有以下形状 (1300, 8) 并使用 'index_col=0' 进行索引,以便删除无用的第一列(删除无用的列后列数最多为 8)

如果 运行 分开,链接的 for 循环实际上工作正常,问题与亲和力有关。

dataset = #csv file
aff = ["l1", "l2", "manhattan", "cosine", "precomputed", "euclidean"]
link = ["complete", "average", "single"]

for a in aff:
    for l in link:
        ds=dataset
        ac_tune=AgglomerativeClustering(n_clusters=5, affinity=a, linkage=l)
        ac_tune.fit(ds)

错误如下:

IndexError: index 8 is out of bounds for axis 1 with size 8

当您尝试执行 "precomputed" 关联时失败。对于此选项,数据集需要是距离矩阵而不是原始数据。

https://scikit-learn.org/stable/modules/generated/sklearn.cluster.AgglomerativeClustering.html