gpt4 book ai didi

python - 将列表和列表列表转换为 scipy 稀疏数组

转载 作者:太空狗 更新时间:2023-10-29 22:22:27 24 4
gpt4 key购买 nike

假设我有一个列表或列表列表(每个列表的大小相同)。如何分别转换为稀疏向量或稀疏矩阵?

最佳答案

In [5]: scipy.sparse.csr_matrix([[1, 2], [3, 0]])
Out[5]:
<2x2 sparse matrix of type '<type 'numpy.int64'>'
with 3 stored elements in Compressed Sparse Row format>

In [6]: scipy.sparse.csr_matrix([1, 2])
Out[6]:
<1x2 sparse matrix of type '<type 'numpy.int64'>'
with 2 stored elements in Compressed Sparse Row format>

scipy.sparse.whatever_matrix_type(your_data_structure)。这与您为获得常规数组所做的操作非常相似。请注意,没有稀疏向量或稀疏 ndarray 类,只有稀疏矩阵。

关于python - 将列表和列表列表转换为 scipy 稀疏数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24924940/

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