2013-01-23 11 views
7

Kein Problem:Scipy: Ermöglichen dünn besetzte Matrizen erweiterte Indizierung?

>>> t = np.array([[1,1,1,1,1],[2,2,2,2,2],[3,3,3,3,3],[4,4,4,4,4],[5,5,5,5,5]]) 
>>> x = np.arange(5).reshape((-1,1)); y = np.arange(5) 
>>> print (t[[x]],t[[y]]) 

Großes Problem:

>>> s = scipy.sparse.csr_matrix(t) 
>>> print (s[[x]].toarray(),s[[y]].toarray()) 
Traceback (most recent call last): 
    File "<pyshell#22>", line 1, in <module> 
:    : 
:    : 
ValueError: data, indices, and indptr should be rank 1 

s.toarray()[[x]] funktioniert gut, aber verfehlt den Sinn und Zweck von mir mit Sparse-Matrizen als meine Arrays zu groß ist. Ich habe die Attribute und Methoden, die mit einigen der dünn besetzten Matrizen verknüpft sind, auf etwas überprüft, das auf Erweiterte Indizierung verweist, aber keine Würfel. Irgendwelche Ideen?

+0

Warum setzen Sie ein zusätzliches Paar eckige Klammern? Sie sind wackelige Logik, die numpy gerade durch seine Argumentation ignoriert. Anders als das, probiere nur 1-d fancy Indizes, das sind Matrizen, höherdimensionale Fancy Indizierung würde deine 2-d Matrix sowieso wahrscheinlich umbringen. – seberg

+0

@seberg: Das obige Beispiel soll nur die [erweiterte Indizierung] (http://docs.scipy.org/doc/numpy/reference/arrays.indexing.html) Syntax veranschaulichen. In meinem realen Code benötige ich eine erweiterte Indexierung, um bei Bedarf _ _ spezifische_ Zeilen aufzurufen (d. H. –

+0

Ja, aber das Hinzufügen dieses zusätzlichen Paars könnte auch als 't [np.array ([x])]' 'anstelle von' t [x,] 'interpretiert werden, was dem einzelnen Index eine zusätzliche Dimension hinzufügt. Und ich würde der spärlichen Indexierung nicht vertrauen, um diesen Fall notwendigerweise so zu behandeln, wie Sie es wollen. – seberg

Antwort

11

spärliche Matrizen haben eine sehr begrenzte Indexierungsunterstützung, und was verfügbar ist, hängt vom Format der Matrix ab.

Zum Beispiel:

>>> a = scipy.sparse.rand(100,100,format='coo') 
>>> a[2:5, 6:8] 
Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
TypeError: 'coo_matrix' object has no attribute '__getitem__' 

aber

>>> a = scipy.sparse.rand(100,100,format='csc') 
>>> a[2:5, 6:8] 
<3x2 sparse matrix of type '<type 'numpy.float64'>' 
    with 0 stored elements in Compressed Sparse Column format> 

obwohl

>>> a[2:5:2, 6:8:3] 
Traceback (most recent call last): 
... 
ValueError: slicing with step != 1 not supported 

Es gibt auch

>>> a = scipy.sparse.rand(100,100,format='dok') 
>>> a[2:5:2, 6:8:3] 
Traceback (most recent call last): 
... 
NotImplementedError: fancy indexing supported over one axis only 
>>> a[2:5:2,1] 
<3x1 sparse matrix of type '<type 'numpy.float64'>' 
    with 0 stored elements in Dictionary Of Keys format> 

Und sogar

>>> a = scipy.sparse.rand(100,100,format='lil') 
>>> a[2:5:2,1] 
<2x1 sparse matrix of type '<type 'numpy.int32'>' 
    with 0 stored elements in LInked List format> 
C:\Python27\lib\site-packages\scipy\sparse\lil.py:230: SparseEfficiencyWarning: Indexing into a lil_matrix with multiple indices is slow. Pre-converting to CSC or CSR beforehand is more efficient. 
    SparseEfficiencyWarning) 
>>> a[2:5:2, 6:8:3] 
<2x1 sparse matrix of type '<type 'numpy.int32'>' 
    with 0 stored elements in LInked List format> 
+0

Also, was Sie sagen, ist ... nur die oben beschriebene Indizierung wird funktionieren, und das wird mein Weg nicht? –

+1

@NoobSaibot Ja, ich denke, die oben genannten fasst die Indexierungsmöglichkeiten für dünn besetzte Matrizen zusammen. Ich bin mir fast sicher, dass die Indizierung mit einem 2D-Array für keines der Formate funktioniert, obwohl ich glaube, dass LIL zwei 1D-Arrays für Indizes und einen Zeilenvektor verwenden wird. – Jaime

+0

Nun, das ist ein Schlag im Schritt ... Danke! –

Verwandte Themen