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

Bob*_*Bob 6 python numpy scipy

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

use*_*ica 9

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>
Run Code Online (Sandbox Code Playgroud)

scipy.sparse.whatever_matrix_type(your_data_structure).这与你为获得常规阵列所做的非常类似.请注意,没有稀疏矢量或稀疏ndarray类,只有稀疏矩阵.

  • @Bob:我确实展示了一个例子.当它"不起作用"时到底发生了什么?你得到了什么,你期望以不同的方式发生什么? (3认同)