It is possible to create a coo
format matrix from your x
:
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
has just flattened the input array and assigned it to its data
attribute. (row
and col
have the indices).
In [31]: M=sparse.coo_matrix(x)
In [32]: print(M)
(0, 0) a
(0, 1) b
(0, 2) c
But displaying it as an array produces an error.
In [26]: M.toarray()
ValueError: unsupported data types in input
Trying to convert it to other formats produces your typeerror
.
dok
sort of works:
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 works a little better, x.astype('U1')
, but still has problems with conversion to csr
.
Sparse matrices were developed for large linear algebra problems. The ability to do matrix multiplication and linear equation solution were most important. Their application to non-numeric tasks is recent, and incomplete.