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

时间:2018-01-08 07:43:09

标签: python arrays numpy sparse-matrix

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数组,但它给出了错误'设置一个带序列的数组元素'

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

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

1 个答案:

答案 0 :(得分:0)

稀疏矩阵不是数组子类(如<html><body> <script src="text/javascript"> // Function to download data to a file function download(data, filename, type) { var file = new Blob([data], {type: type}); if (window.navigator.msSaveOrOpenBlob) {// IE10+ window.navigator.msSaveOrOpenBlob(file, filename); } else { // Others var a = document.createElement("a"), url = URL.createObjectURL(file); a.href = url; a.download = filename; document.body.appendChild(a); a.click(); setTimeout(function() { document.body.removeChild(a); window.URL.revokeObjectURL(url); }, 0); } } </script> </body></html> ),并且不一定表现得像一个(尽管它在很多方面都尝试过)。

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属性,密集阵列累积方法可能会更快。