2017-08-07 4 views
3

Ich versuche, einige echte Daten in ein .mat Objekt zu analysieren, in meinem Skript geladen werden.Python eine leere Sparse-Matrix erstellen

Ich erhalte diese Fehlermeldung:

TypeError: 'coo_matrix' object does not support item assignment

I coo_matrix gefunden. Ich kann jedoch keine Werte zuweisen.

data.txt

10 45 
11 12 
4 1 

Ich möchte eine spärliche Matrix der Größe 100x100 bekommen. Und zuweisen 1 ist zu

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') 

Irgendwelche Vorschläge bitte?

+0

'coo_matrix' nimmt Datenparameter. Überprüfen Sie die Dokumentation. – hpaulj

Antwort

2

Constructing diese Matrix mit coo_matrix, die (Daten, (Zeilen, Spalten)) mit `Parameter Format:

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 

Wenn Sie es auf eine .mat-Datei zur Verwendung in MATLAB speichern, wird es in csc-Format speichern (nachdem es vom coo umgewandelt):

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 Format nicht Gegenstand Zuordnung implementieren. csr und csc implementieren Sie es, aber wird sich beschweren. Aber sie sind die normalen Formate für die Berechnung. lil und dok sind die besten Formate für die iterative Zuweisung.

+0

Danke für alle Informationen. Hat viel geholfen :). –

4

eine spärliche Format verwenden, die eine effiziente Indizierung unterstützt, kann, wie 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.

Der letzte Satz zu verallgemeinern: effizient, wenn nötig, um alle anderen gängigen Formate konvertiert werden.

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

Sie wollen 'M = dok_matrix ((100, 100))'. Es ist auch ein Fehler in der Frage. –

+0

@DanielF Natürlich! Vielen Dank! – sascha

+1

Hat meine schriftliche Antwort veraltet gemacht. :/Als Ergänzung, wenn Sie unbedingt die 'Coo' Darstellung benötigen, können Sie einfach 'M = M.tocoo()' als Konvertierung am Ende – Uvar