gpt4 book ai didi

python - 一种在 pythons scipy 模块的 coo_matrix 中获取非零值计数的方法?

转载 作者:太空狗 更新时间:2023-10-30 02:05:57 25 4
gpt4 key购买 nike

我想到了使用 coo_matrix.nonzero(),它返回两个数组的元组,其中包含给定矩阵中非零条目的索引。文档中的示例指出:

>>> from scipy.sparse import coo_matrix
>>> A = coo_matrix([[1,2,0],[0,0,3],[4,0,5]])
>>> nonzero_entrys = A.nonzero()
(array([0, 0, 1, 2, 2]), array([0, 1, 2, 0, 2]))

然后我会做类似 len(nonzero_entrys[0]) 的事情,但这看起来像是一种转移。我在文档中忽略了更好的方法吗?

最佳答案

您可以改用 len(A.data)

关于python - 一种在 pythons scipy 模块的 coo_matrix 中获取非零值计数的方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7225159/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com