2017-08-07 156 views
3

我想解析一些真实的数据到一个.mat对象来加载我的脚本。Python创建一个空的稀疏矩阵

我收到此错误:

TypeError: 'coo_matrix' object does not support item assignment

我发现coo_matrix。但是,我无法为其分配值。

的data.txt

10 45 
11 12 
4 1 

我想获得尺寸100×100 稀疏矩阵。并指定1对

Mat(10, 45) = 1 
Mat(11, 12) = 1 
Mat(4, 1) = 1 

CODE

import numpy as np 
from scipy.sparse import coo_matrix 

def pdata(pathToFile): 
    M = coo_matrix(100, 100) 
    with open(pathToFile) as f: 
     for line in f: 
      s = line.split() 
      x, y = [int(v) for v in s] 
      M[x, y] = 1  
    return M 

if __name__ == "__main__": 
    M = pdata('small.txt') 

任何建议吗?

+0

'coo_matrix'获取数据参数。检查它的文档。 – hpaulj

回答

2

构建这个矩阵coo_matrix,使用(数据,(行的cols))`参数格式:

In [2]: from scipy import sparse 
In [3]: from scipy import io 
In [4]: data=np.array([[10,45],[11,12],[4,1]]) 
In [5]: data 
Out[5]: 
array([[10, 45], 
     [11, 12], 
     [ 4, 1]]) 
In [6]: rows = data[:,0] 
In [7]: cols = data[:,1] 
In [8]: data = np.ones(rows.shape, dtype=int) 
In [9]: M = sparse.coo_matrix((data, (rows, cols)), shape=(100,100)) 
In [10]: M 
Out[10]: 
<100x100 sparse matrix of type '<class 'numpy.int32'>' 
    with 3 stored elements in COOrdinate format> 
In [11]: print(M) 
    (10, 45) 1 
    (11, 12) 1 
    (4, 1) 1 

如果你将它保存到MATLAB中使用的.MAT文件,它将它保存在csc格式(已经从coo将其转换):

In [13]: io.savemat('test.mat',{'M':M}) 
In [14]: d = io.loadmat('test.mat') 
In [15]: d 
Out[15]: 
{'M': <100x100 sparse matrix of type '<class 'numpy.int32'>' 
    with 3 stored elements in Compressed Sparse Column format>, 
'__globals__': [], 
'__header__': b'MATLAB 5.0 MAT-file Platform: posix, Created on: Mon Aug 7 08:45:12 2017', 
'__version__': '1.0'} 

coo格式不落实的项目分配。 csrcsc执行它,但会抱怨。但它们是计算的正常格式。 lildok是迭代分配的最佳格式。

+0

感谢所有的信息。帮了很多:)。 –

4

使用稀疏格式,它支持高效的索引,如dok_matrix

This is an efficient structure for constructing sparse matrices incrementally.

...

Allows for efficient O(1) access of individual elements. Duplicates are not allowed. Can be efficiently converted to a coo_matrix once constructed.

最后一句可以概括为:如果需要,可以有效地转换到所有其他常见的格式。

from scipy.sparse import dok_matrix 

M = dok_matrix((100, 100)) # extra brackets needed as mentioned in comments 
          # thanks Daniel! 
M[0,3] = 5 
+1

你想'M = dok_matrix((100,100))'。这也是一个错误。 –

+0

@DanielF当然!谢谢! – sascha

+1

呈现我的答案已过时。 :/另外,如果你绝对需要'coo'表示,你可以在末尾 – Uvar