我有一个很长的索引元组列表(大量重复),以及一个n×n索引矩阵。每个元组表示一个共现
例如:
a = np.zeros(shape=(indexCount,indexCount))
我试过这个:
for i1,i2 in coocPairs: #for instance (2374, 22003)
a[i1][i2}+=1 #takes way too long
或:
np.put(a,coocPairs,1) #which obviously does not increment
或者:
np.add(a,coocPairs,1) #which takes even longer.
在一个理想的世界中,会有一个函数接受我的元组列表,并用它构建一个共现矩阵,但唉(doc.不是很有帮助)。我认为解决方案可以更多地放在代码的python方面,但我已经没有想法了。欢迎任何帮助。谢谢你抽出时间
您可以使用
np.add.at
如果速度不够快,则会有更快但更不直接的基于
np.bincount
的解决方案。这些依赖于使用np.ravel_multi_index
的平坦索引您可以使用
collections.Counter
获取实际出现在矩阵中的值。这是因为元组是可散列的。任务变得非常简单:通常,
np.unique
和return_counts=True
是Counter
的代名词。在这种情况下,有必要指定轴,并记住它将是较慢的解决方案之一:相反,您可以将线对转换为展开矩阵中的线性索引:
现在你可以做了
或者,您可以使用^{} 来加快计数,或者使用
np.add.at
来避免预先计数。带有raveled索引的bincount
解决方案省去了预分配a
的麻烦:相关问题 更多 >
编程相关推荐