Python 将numpy对象数组转换为稀疏矩阵

Python 将numpy对象数组转换为稀疏矩阵,python,arrays,numpy,matrix,scipy,Python,Arrays,Numpy,Matrix,Scipy,我想将具有dtype=object的numpy数组转换为稀疏数组,例如csr\u矩阵。然而,这是失败的 x = np.array(['a', 'b', 'c'], dtype=object) csr_matrix(x) # This fails csc_matrix(x) # This fails 对稀疏矩阵的两个调用都会产生以下错误: TypeError:不支持类型的转换:(dtype('O'),) 事实上,即使打电话 csr_matrix(['a', 'b', 'c']) 产生相同的错

我想将具有
dtype=object
的numpy数组转换为稀疏数组,例如
csr\u矩阵
。然而,这是失败的

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

csr_matrix(x) # This fails
csc_matrix(x) # This fails
对稀疏矩阵的两个调用都会产生以下错误:

TypeError:不支持类型的转换:(dtype('O'),)

事实上,即使打电话

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

产生相同的错误。稀疏矩阵不支持
对象
数据类型吗?

我认为这不受支持,虽然文档在这方面有点稀疏,但应该显示:

# 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'),
]

要求基于对象的类型听起来像很多。甚至缺少一些更基本的类型,例如。

我认为这是不受支持的,虽然文档在这方面有点稀疏,但应该显示:

# 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'),
]

要求基于对象的类型听起来像很多。甚至缺少一些更基本的类型,例如。

可以从
x
创建
coo
格式矩阵:

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)
但将其显示为数组会产生错误

In [26]: M.toarray()
ValueError: unsupported data types in input
尝试将其转换为其他格式会产生
typeerror

dok
作品种类:

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>
[28]中的
M=sparse.dok_矩阵(x)
/usr/local/lib/python3.5/dist packages/scipy/sparse/sputils.py:114:UserWarning:sparse矩阵不支持对象数据类型
warnings.warn(“稀疏矩阵不支持对象数据类型”)
In[29]:M
出[29]:
stringdtype工作得稍微好一点,
x.astype('U1')
,但在转换到
csr
时仍然存在问题


稀疏矩阵是为大型线性代数问题开发的。矩阵乘法和线性方程解的能力是最重要的。它们对非数值任务的应用是最新的,而且不完整。

可以从
x
创建
coo
格式矩阵:

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)
但将其显示为数组会产生错误

In [26]: M.toarray()
ValueError: unsupported data types in input
尝试将其转换为其他格式会产生
typeerror

dok
作品种类:

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>
[28]中的
M=sparse.dok_矩阵(x)
/usr/local/lib/python3.5/dist packages/scipy/sparse/sputils.py:114:UserWarning:sparse矩阵不支持对象数据类型
warnings.warn(“稀疏矩阵不支持对象数据类型”)
In[29]:M
出[29]:
stringdtype工作得稍微好一点,
x.astype('U1')
,但在转换到
csr
时仍然存在问题


稀疏矩阵是为大型线性代数问题开发的。矩阵乘法和线性方程解的能力是最重要的。它们对非数值任务的应用是最近才出现的,而且是不完整的。

稀疏矩阵可以包含非数值元素吗?对象数据类型中的
zero
元素是什么?
csr
数学无法处理对象。它是用一组有限的数字类型编译的。你希望用这样一个矩阵做什么?即使字符串也不起作用。我希望
None
zero
元素。但是,只处理数值类型是有意义的。稀疏矩阵是否可以包含非数值元素?对象数据类型中的
zero
元素是什么?
csr
数学无法处理对象。它是用一组有限的数字类型编译的。你希望用这样一个矩阵做什么?即使字符串也不起作用。我希望
None
zero
元素。不过,只使用数字类型是有意义的。啊,谢谢!我对这种行为感到很困惑,我猜一定是这样的。谢谢你的确认!啊,谢谢你!我对这种行为感到很困惑,我猜一定是这样的。谢谢你的确认!感谢您的深入跟进!感谢您的深入跟进!