numpy 使用kmapper时出现Python错误:元组索引必须是整数或切片,而不是字符串

js5cn81o  于 2023-10-19  发布在  Python
关注(0)|答案(1)|浏览(94)

我试图可视化一个用scikit得到的给定数据集的单纯复杂图。代码如下:

import kmapper as km
from sklearn import cluster

# Define the data points
import numpy as np
data = np.asarray(
         [ [145.0,  17.0], [166.0,  10], [145,  38], [161,  49],
           [165,  24], [188,  41], [193,  18], [179,  52],
           [208,  21], [194,  51], [198,  31], [207,  41],
           [218,  55], [222,  26], [238,  27], [226,  41] ])
                   
mapper = km.KeplerMapper(verbose=1)

projected_data = mapper.fit_transform(data, projection=[0],
                                      distance_matrix="euclidean")

cover = km.Cover(n_cubes=10, perc_overlap=0.4)

graph = mapper.map(projected_data, data, cover=cover,
               clusterer=cluster.AgglomerativeClustering(n_clusters=2,
               linkage="single")
               ),

mapper.visualize(graph, path_html="graph1.html")

代码正确运行,直到最后一行,当我得到以下错误:

Created 11 edges and 16 nodes in 0:00:00.005993.
Traceback (most recent call last):

  File "C:\Desktop\ex.py", line 24, in <module>
    mapper.visualize(graph, path_html="graph1.html")

  File "C:\Anaconda3\lib\site-packages\kmapper\utils.py", line 11, in wrapper
    return f(*args, **kwargs)

  File "C:\Anaconda3\lib\site-packages\kmapper\kmapper.py", line 829, in visualize
    if not len(graph["nodes"]) > 0:

TypeError: tuple indices must be integers or slices, not str
trnvg8h3

trnvg8h31#

由于某种原因,变量graph作为“tuple”而不是“dict”返回。我把最后一行换成了

mapper.visualize(graph[0], path_html="graph1.html")

这就解决了问题

相关问题