将稀疏矩阵的值分配给 numpy 数组

Assign a the value of a sparse matrix to numpy array

import numpy as np
import scipy.sparse as scsp
from scipy.sparse import csr_matrix,lil_matrix

# create an empty numpy matrix
wi=np.empty((num_clusters*num_cluster_neurons, input))   
for i in range(num_clusters*num_cluster_neurons):         
    temp_neuron_prob=dic_cluster_prob[dic_neuron_cluster[i]]

    #create 1*input shape sparse matrix according to probability
    lil=lil_matrix(scsp.rand(1, input, temp_neuron_prob))

    #want to assign the 1*input sparse matrix to one slice of the numpy matrix
    wi[i,:]=lil[:]

我试图将 lil_matrix 的值分配给 numpy 数组的一个片段,但它给出了错误 'setting an array element with a sequence'

我想知道为什么会出现这个错误,因为它们具有相同的大小,我该如何提高效率,因为 numpy 数组比稀疏矩阵快 (lil_matrix)。

我想使用 numpy 数组来获取由稀疏矩阵创建的值

稀疏矩阵不是数组子类(如 np.matrix),也不一定表现得像(尽管在很多方面它确实尝试这样做)。

In [129]: arr = np.zeros((3,4),int)
In [130]: M = sparse.lil_matrix([0,1,2,0])
In [131]: M.shape
Out[131]: (1, 4)
In [132]: arr[0,:] = M
...
ValueError: setting an array element with a sequence.

但如果我先将稀疏矩阵转换为数组或矩阵,则赋值有效:

In [133]: arr[0,:] = M.A
In [134]: arr[0,:] = M.todense()
In [135]: arr
Out[135]: 
array([[0, 1, 2, 0],
       [0, 0, 0, 0],
       [0, 0, 0, 0]])

作为一般规则,稀疏矩阵不能插入到 numpy 代码中。例外情况是 numpy 将任务委托给对象自己的方法的代码。

看起来您正在尝试生成如下内容:

In [148]: arr = np.zeros((3,5),float)
In [149]: for i in range(arr.shape[0]):
     ...:     arr[i,:] = sparse.rand(1,5, .2*(i+1)).A
     ...:     
In [150]: arr
Out[150]: 
array([[ 0.        ,  0.        ,  0.82470353,  0.        ,  0.        ],
       [ 0.        ,  0.43339367,  0.99427277,  0.        ,  0.        ],
       [ 0.        ,  0.99843277,  0.05182824,  0.1705916 ,  0.        ]])

纯稀疏等价物可能是:

In [151]: alist = []
In [152]: for i in range(3):
     ...:     alist.append(sparse.rand(1,5, .2*(i+1)))
     ...:     
     ...:     
In [153]: alist
Out[153]: 
[<1x5 sparse matrix of type '<class 'numpy.float64'>'
    with 1 stored elements in COOrdinate format>,
 <1x5 sparse matrix of type '<class 'numpy.float64'>'
    with 2 stored elements in COOrdinate format>,
 <1x5 sparse matrix of type '<class 'numpy.float64'>'
    with 3 stored elements in COOrdinate format>]
In [154]: sparse.vstack(alist)
Out[154]: 
<3x5 sparse matrix of type '<class 'numpy.float64'>'
    with 6 stored elements in COOrdinate format>
In [155]: _.A
Out[155]: 
array([[ 0.        ,  0.        ,  0.        ,  0.19028467,  0.        ],
       [ 0.        ,  0.        ,  0.        ,  0.92668274,  0.67424419],
       [ 0.96208905,  0.63604635,  0.        ,  0.69463657,  0.        ]])

但考虑到sparse.vstack使用稀疏bmat将矩阵拼接成一个新矩阵,而bmat结合了分量的coo属性,稠密数组积累方法可能会更快。