【问题标题】:Create an sparse matrix from a list of tuples having the indexes of the column where is a 1从具有列索引的元组列表中创建一个稀疏矩阵,其中 1
【发布时间】:2021-11-30 11:56:56
【问题描述】:

问题

我有一个元组列表,每个元组代表二维数组的一列,元组的每个元素代表数组中该列的索引,即 1;不在该元组中的其他条目为 0。

我想用这种元组列表以一种有效的方式创建一个稀疏矩阵(尽量不使用 for 循环)。

示例

# init values
list_tuples = [
 (0, 2, 4),
 (0, 2, 3),
 (1, 3, 4)
]

n = length(list_tuples) + 1
m = 5 # arbritrary, however n >= max([ei for ei in list_tuples]) + 1

# what I need is a function which accepts this tuples and give the shape of the array
# (at least the row size, because the column size can be infered from the list of tuples)
A = some_function(list_tuples, array_shape = (m, n))

那么我期望得到的是一个形式的数组:

[   
 [1, 1, 0]
 [0, 0, 1]  
 [1, 1, 0]
 [0, 1, 1]
 [1, 0, 1]
]

【问题讨论】:

  • 创建稀疏矩阵的最有效方法是创建 sparse.coo_matrix 期望的 3 个数组 - 行、列和数据。阅读文档和示例。

标签: python numpy scipy sparse-matrix one-hot-encoding


【解决方案1】:

您的值是indices 所需的compressed sparse column format。您还需要 indptr 数组,对于您的数据,它是元组长度的累积总和(以 0 开头)。 data 数组将是一个长度与元组长度之和相同的数组,您可以从累积和的最后一个元素中获得。您的示例如下所示:

In [45]: from scipy.sparse import csc_matrix

In [46]: list_tuples = [
    ...:  (0, 2, 4),
    ...:  (0, 2, 3),
    ...:  (1, 3, 4)
    ...: ]

In [47]: indices = sum(list_tuples, ())  # Flatten the tuples into one sequence.

In [48]: indptr = np.cumsum([0] + [len(t) for t in list_tuples])

In [49]: a = csc_matrix((np.ones(indptr[-1], dtype=int), indices, indptr))

In [50]: a
Out[50]: 
<5x3 sparse matrix of type '<class 'numpy.int64'>'
    with 9 stored elements in Compressed Sparse Column format>

In [51]: a.A
Out[51]: 
array([[1, 1, 0],
       [0, 0, 1],
       [1, 1, 0],
       [0, 1, 1],
       [1, 0, 1]])

请注意,csc_matrix 根据在​​索引中找到的最大值推断行数。您可以使用shape 参数来覆盖它,例如

In [52]: b = csc_matrix((np.ones(indptr[-1], dtype=int), indices, indptr), shape=(7, len(list_tuples)))

In [53]: b
Out[53]: 
<7x3 sparse matrix of type '<class 'numpy.int64'>'
    with 9 stored elements in Compressed Sparse Column format>

In [54]: b.A
Out[54]: 
array([[1, 1, 0],
       [0, 0, 1],
       [1, 1, 0],
       [0, 1, 1],
       [1, 0, 1],
       [0, 0, 0],
       [0, 0, 0]])

您还可以很容易地生成coo_matrix。扁平化的list_tuples 提供行索引,np.repeat 可用于创建列索引:

In [63]: from scipy.sparse import coo_matrix

In [64]: i = sum(list_tuples, ())  # row indices

In [65]: j = np.repeat(range(len(list_tuples)), [len(t) for t in list_tuples])

In [66]: c = coo_matrix((np.ones(len(i), dtype=int), (i, j)))

In [67]: c
Out[67]: 
<5x3 sparse matrix of type '<class 'numpy.int64'>'
    with 9 stored elements in COOrdinate format>

In [68]: c.A
Out[68]: 
array([[1, 1, 0],
       [0, 0, 1],
       [1, 1, 0],
       [0, 1, 1],
       [1, 0, 1]])

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-29
    • 2017-09-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多