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.
Kann eine dünn besetzte Matrix nicht numerische Elemente enthalten? –
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
Nun, ich würde erwarten, dass 'None' das' zero' Element ist. Es macht jedoch Sinn, nur mit numerischen Typen zu arbeiten. – Pavlin