将numpy对象数组转换为稀疏矩阵 [英] Convert numpy object array to sparse matrix

查看:2292
本文介绍了将numpy对象数组转换为稀疏矩阵的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将具有dtype=object的numpy数组转换为稀疏数组,例如csr_matrix.但是,这失败了.

I would like to convert a numpy array with dtype=object to a sparse array e.g. csr_matrix. However, this fails.

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

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

对稀疏矩阵的调用均会产生以下错误:

Both of the calls to sparse matrices produce the following error:

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

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

事实上,甚至打电话给

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

产生相同的错误.稀疏矩阵不支持object dtypes吗?

produces the same error. Do sparse matrices not support object dtypes?

推荐答案

可以从您的x创建一个coo格式矩阵:

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刚刚将输入数组展平并将其分配给其data属性. (rowcol具有索引).

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

尝试将其转换为其他格式会生成typeerror.

Trying to convert it to other formats produces your 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>

字符串dtype效果更好,x.astype('U1'),但是在转换为csr时仍然存在问题.

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.

这篇关于将numpy对象数组转换为稀疏矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆