2017-12-16 3 views
0

Ich möchte ein numpy Array mit dtype=object zu einem Sparse-Array z. csr_matrix. Dies schlägt jedoch fehl.Convert numpy Objekt-Array in Sparse-Matrix

x = np.array(['a', 'b', 'c'], dtype=object) 

csr_matrix(x) # This fails 
csc_matrix(x) # This fails 

Beide der Anrufe zu dünn besetzte Matrizen erzeugen den folgenden Fehler:

TypeError: no supported conversion for types: (dtype('O'),)

In der Tat, auch

csr_matrix(['a', 'b', 'c']) 

erzeugt den gleichen Fehler aufrufen. Unterstützt spärliche Matrizen keine object dtypes?

+0

Kann eine dünn besetzte Matrix nicht numerische Elemente enthalten? –

+0

Was ist ein 'zero' Element im Objekt dtype? Die 'csr'-Mathematik funktioniert nicht mit Objekten. Es ist mit einer begrenzten Anzahl von numerischen Typen kompiliert. Was erwarten Sie von einer solchen Matrix? Sogar Strings funktionieren nicht. – hpaulj

+0

Nun, ich würde erwarten, dass 'None' das' zero' Element ist. Es macht jedoch Sinn, nur mit numerischen Typen zu arbeiten. – Pavlin

Antwort

1

Es ist möglich, eine coo Format Matrix aus Ihrem x zu erstellen:

In [22]: x = np.array([['a', 'b', 'c']], dtype=object) 
In [23]: M=sparse.coo_matrix(x) 
In [24]: M 
Out[24]: 
<1x3 sparse matrix of type '<class 'numpy.object_'>' 
    with 3 stored elements in COOrdinate format> 
In [25]: M.data 
Out[25]: array(['a', 'b', 'c'], dtype=object) 

coo hat gerade abgeflacht den Eingangs-Array und zugeordnet es zu seinem data Attribute. (row und col haben die Indizes).

In [31]: M=sparse.coo_matrix(x) 
In [32]: print(M) 
    (0, 0) a 
    (0, 1) b 
    (0, 2) c 

Die Anzeige als Array erzeugt jedoch einen Fehler.

In [26]: M.toarray() 
ValueError: unsupported data types in input 

Der Versuch, es in andere Formate zu konvertieren produziert Ihr typeerror.

dok Art der Arbeiten:

In [28]: M=sparse.dok_matrix(x) 
/usr/local/lib/python3.5/dist-packages/scipy/sparse/sputils.py:114: UserWarning: object dtype is not supported by sparse matrices 
    warnings.warn("object dtype is not supported by sparse matrices") 
In [29]: M 
Out[29]: 
<1x3 sparse matrix of type '<class 'numpy.object_'>' 
    with 3 stored elements in Dictionary Of Keys format> 

String dtype arbeitet ein wenig besser, x.astype('U1'), hat aber immer noch Probleme mit Umstellung auf csr.

Sparse Matrizen wurden für große lineare Algebra Probleme entwickelt. Die Fähigkeit zur Matrixmultiplikation und linearen Gleichungslösung war am wichtigsten. Ihre Anwendung auf nicht numerische Aufgaben ist neu und unvollständig.

+0

Danke für das aufschlussreiche Follow-up! – Pavlin

2

Ich glaube nicht, das unterstützt wird, und während die Dokumente etwas spärlich an diesem Ende sind, sollten this part of the sources dass zeigen:

# List of the supported data typenums and the corresponding C++ types 
# 
T_TYPES = [ 
    ('NPY_BOOL', 'npy_bool_wrapper'), 
    ('NPY_BYTE', 'npy_byte'), 
    ('NPY_UBYTE', 'npy_ubyte'), 
    ('NPY_SHORT', 'npy_short'), 
    ('NPY_USHORT', 'npy_ushort'), 
    ('NPY_INT', 'npy_int'), 
    ('NPY_UINT', 'npy_uint'), 
    ('NPY_LONG', 'npy_long'), 
    ('NPY_ULONG', 'npy_ulong'), 
    ('NPY_LONGLONG', 'npy_longlong'), 
    ('NPY_ULONGLONG', 'npy_ulonglong'), 
    ('NPY_FLOAT', 'npy_float'), 
    ('NPY_DOUBLE', 'npy_double'), 
    ('NPY_LONGDOUBLE', 'npy_longdouble'), 
    ('NPY_CFLOAT', 'npy_cfloat_wrapper'), 
    ('NPY_CDOUBLE', 'npy_cdouble_wrapper'), 
    ('NPY_CLONGDOUBLE', 'npy_clongdouble_wrapper'), 
] 

für Fragen objektbasierte Typen wie viel klingt. Sogar einige grundlegendere Typen wie float16 fehlen.

+0

Ah, danke! Ich war ziemlich verwirrt über dieses Verhalten, und ich vermutete, dass es so etwas sein muss. Danke für die Bestätigung! – Pavlin