在 SciPy 中,将 ix_() 与稀疏矩阵一起使用似乎不起作用,那么我还能使用什么?

发布于 2024-08-23 04:31:28 字数 1120 浏览 7 评论 0原文

在 Numpy 中,ix_() 用于获取矩阵的行和列,但它似乎不适用于稀疏矩阵。例如,这段代码之所以有效,是因为它使用了一个密集矩阵:

>>> import numpy as np
>>> x = np.mat([[1,0,3],[0,4,5],[7,8,0]])
>>> print x
[[1 0 3]
 [0 4 5]
 [7 8 0]]
>>> print x[np.ix_([0,2],[0,2])]
[[1 3]
 [7 0]]

我使用 ix_() 来索引与第 0 行和第 2 行和列对应的元素,这给出了矩阵的 4 个角。

问题是 ix_ 似乎不适用于稀疏矩阵。继续前面的代码,我尝试以下操作:

>>> import scipy.sparse as sparse
>>> xspar = sparse.csr_matrix(x)
>>> print xspar
  (0, 0) 1
  (0, 2) 3
  (1, 1) 4
  (1, 2) 5
  (2, 0) 7
  (2, 1) 8
>>> print xspar[np.ix_([0,2],[0,2])]

并收到一条巨大的错误消息,指出存在此异常:

  File "C:\Python26\lib\site-packages\scipy\sparse\compressed.py", line 138, in check_format
    raise ValueError('data, indices, and indptr should be rank 1')
ValueError: data, indices, and indptr should be rank 1

我已尝试使用 SciPy 提供的其他稀疏矩阵格式进行此操作,但它们似乎都无法与 ix_() 一起使用,尽管它们不要全部引发相同的异常。

我给出的示例使用了一个不是很大或不是很稀疏的矩阵,但我正在处理的矩阵非常稀疏并且可能非常大,因此仅一一列出元素似乎并不明智。

有谁知道在 SciPy 中使用稀疏矩阵进行此类索引的(希望简单的)方法,或者这个功能是否没有内置到这些稀疏矩阵中?

In Numpy, ix_() is used to grab rows and columns of a matrix, but it doesn't seem to work with sparse matrices. For instance, this code works because it uses a dense matrix:

>>> import numpy as np
>>> x = np.mat([[1,0,3],[0,4,5],[7,8,0]])
>>> print x
[[1 0 3]
 [0 4 5]
 [7 8 0]]
>>> print x[np.ix_([0,2],[0,2])]
[[1 3]
 [7 0]]

I used ix_() to index the elements corresponding with the 0th and 2nd rows and columns which gives the 4 corners of the matrix.

The problem is that ix_ doesn't seem to work with sparse matrices. Continuing from the previous code, I try the following:

>>> import scipy.sparse as sparse
>>> xspar = sparse.csr_matrix(x)
>>> print xspar
  (0, 0) 1
  (0, 2) 3
  (1, 1) 4
  (1, 2) 5
  (2, 0) 7
  (2, 1) 8
>>> print xspar[np.ix_([0,2],[0,2])]

and get a huge error message saying there is this exception:

  File "C:\Python26\lib\site-packages\scipy\sparse\compressed.py", line 138, in check_format
    raise ValueError('data, indices, and indptr should be rank 1')
ValueError: data, indices, and indptr should be rank 1

I have tried this with the other sparse matrix formats provided by SciPy, but none of them seem to work with ix_() though they don't all raise the same exception.

The example I gave used a matrix that wasn't very big or very sparse, but the ones I am dealing with are quite sparse and potentially very large so it doesn't seem prudent to just list off the elements one by one.

Does anyone know a (hopefully easy) way to do this sort of indexing with sparse matrices in SciPy or is this feature just not built into these sparse matrices?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

若相惜即相离 2024-08-30 04:31:28

试试这个:

>>> print xspar
  (0, 0) 1
  (0, 2) 3
  (1, 1) 4
  (1, 2) 5
  (2, 0) 7
  (2, 1) 8
>>> print xspar[[[0],[2]],[0,2]]
  (0, 0) 1
  (0, 2) 3
  (2, 0) 7

注意与此的区别:

>>> print xspar[[0,2],[0,2]]
  [[1 0]]

Try this instead:

>>> print xspar
  (0, 0) 1
  (0, 2) 3
  (1, 1) 4
  (1, 2) 5
  (2, 0) 7
  (2, 1) 8
>>> print xspar[[[0],[2]],[0,2]]
  (0, 0) 1
  (0, 2) 3
  (2, 0) 7

Note the difference with this:

>>> print xspar[[0,2],[0,2]]
  [[1 0]]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文