M×n维MatLab稀疏矩阵到PYTHON的转换

x3naxklr  于 2022-11-15  发布在  Matlab
关注(0)|答案(2)|浏览(247)

我正在尝试将以下MatLab代码转换为Python:

W2 = sparse(1:m*n-1,2:m*n,-w_alpha(1),m*n,3*m*n);

在MatLab中,输出应采用以下格式:

(1,2)      -10
   (2,3)      -10
   (3,4)      -10
   (4,5)      -10
    ..............
   ...............

以下是我的python代码:

i=np.int64(np.dot(m,n)-1) #------>m*n-1----->i=102
 test_j=np.int64(np.dot(m,n))#------->m*n 
 j=np.int64(np.dot(3,test_j))  #------>3*m*n----> j=309    
 a=((np.arange(0,i)).reshape(1,-1))
 b=(np.arange(1,test_j)).reshape(1,-1)
 W2=csr_matrix((-w_alpha,(a,b)),shape=(test_j,j))

哪里

m=103
 n=1
 weights = [10,10,10]
 w_alpha = np.float64(weights[0]);

然而,语法是不正确的。我使用了scipy库中的以下语法:csr_Matrix((data,(row_ind,colind)),[Shape=(M,N)]),其中data、row_ind和colind满足关系a[row_ind[k],colind[k]]=data[k]。
此外,我还收到以下错误消息:TypeError:Len()of Unsired Object
任何建议,以获得类似的输出与matlab。

jljoyd4f

jljoyd4f1#

好的,我想这就是你要找的东西。基本问题是,在传递-w_alpha的位置,需要传递一个包含N个-w_alpha副本的数组。

import numpy as np
from scipy.sparse import csr_matrix

m=103
n=1
weights = [10,10,10]
w_alpha = float(weights[0])

i = m*n-1
test_j = m*n
j = 3*test_j 

a = np.arange(0,i).flatten()
b = np.arange(1,test_j).flatten()
w_alpha = np.array([w_alpha] * len(a))
W2 = csr_matrix((-w_alpha,(a,b)),shape=(test_j,j)) 
print(W2)

产出:

(0, 1)    -10.0
  (1, 2)    -10.0
  (2, 3)    -10.0
  (3, 4)    -10.0
  (4, 5)    -10.0
  (5, 6)    -10.0
  (6, 7)    -10.0
  (7, 8)    -10.0
  (8, 9)    -10.0
  (9, 10)   -10.0
  (10, 11)  -10.0
  (11, 12)  -10.0
  (12, 13)  -10.0
  (13, 14)  -10.0
  (14, 15)  -10.0
  (15, 16)  -10.0
  (16, 17)  -10.0
  (17, 18)  -10.0
  (18, 19)  -10.0
  (19, 20)  -10.0
  (20, 21)  -10.0
  (21, 22)  -10.0
  (22, 23)  -10.0
  (23, 24)  -10.0
  (24, 25)  -10.0
  : :
  (77, 78)  -10.0
  (78, 79)  -10.0
  (79, 80)  -10.0
  (80, 81)  -10.0
  (81, 82)  -10.0
  (82, 83)  -10.0
  (83, 84)  -10.0
  (84, 85)  -10.0
  (85, 86)  -10.0
  (86, 87)  -10.0
  (87, 88)  -10.0
  (88, 89)  -10.0
  (89, 90)  -10.0
  (90, 91)  -10.0
  (91, 92)  -10.0
  (92, 93)  -10.0
  (93, 94)  -10.0
  (94, 95)  -10.0
  (95, 96)  -10.0
  (96, 97)  -10.0
  (97, 98)  -10.0
  (98, 99)  -10.0
  (99, 100) -10.0
  (100, 101)    -10.0
  (101, 102)    -10.0
wgxvkvu9

wgxvkvu92#

在Octave中,使用w_alpha=10时,显示实际上是:

W2 =

Compressed Column Sparse (rows = 25, cols = 75, nnz = 24 [1.3%])

  (1, 2) -> -10
  (2, 3) -> -10
  (3, 4) -> -10
  (4, 5) -> -10
  (5, 6) -> -10
  (6, 7) -> -10
  (7, 8) -> -10
  (8, 9) -> -10
  (9, 10) -> -10
   ...

因此,您可能希望改用sparse.csc_matrix。而且参数必须是具有匹配大小的一维数组。您的ab是(1102)。在matlab中,一切都是二维的,在numpy中,事物可以是一维的,而且在某些情况下必须是一维的。

w_alpha = a*0+w_alpha
In [170]: a,b = a.ravel(), b.ravel()
In [171]: w_alpha= w_alpha.ravel()
In [172]:  w_alpha = a*0+w_alpha
     ...:  W2=csr_matrix((-w_alpha,(a,b)),shape=(test_j,j))
In [173]: W2
Out[173]: 
<103x309 sparse matrix of type '<class 'numpy.float64'>'
    with 102 stored elements in Compressed Sparse Row format>
In [174]: print(W2)
  (0, 1)    -10.0
  (1, 2)    -10.0
  (2, 3)    -10.0

如果将matlab矩阵保存为文件(旧样式),scipy.io.loadmat可以将其加载为csc

相关问题