我有一个用Python编写的代码,如下所示:
def adamic_adar_prediction(graph):
adjacencyMatrix = graph.get_adjacency()
AAMatrix = adamic_adar_score(graph)
AAMatrix = np.array(AAMatrix)
i = (-AAMatrix ).argsort(axis=None, kind='mergesort')
j = np.unravel_index(i, AAMatrix .shape)
sortedList = np.vstack(j).T
print(sortedList.size)
print(sortedList[1658943])
print(sortedList[1658945])
当第一次打印的结果是3316888时,我收到最后一次打印的以下错误:
IndexError: index 1658944 is out of bounds for axis 0 with size 1658944
知道为什么我的数组会出现这个错误吗?
您的
array
中没有足够的元素,例如:谢谢你的评论。我发现我的问题是sortedList.size返回数组中元素的总数,而我期望的是数组中的元组数(因为sortedList是元组列表[[],[],…])。所以我用
sortedList.shape
解决了我的问题考虑到您的问题有多神秘,我将继续使用try/except循环测试这个问题,以确保代码经过那个点,并且只在索引1658944处有问题。。。
类似于:
报告你的结果。
相关问题 更多 >
编程相关推荐