在numpy或pytorch中自动获取对角矩阵条纹 [英] Getting diagonal matrix stripe automatically in numpy or pytorch

查看:95
本文介绍了在numpy或pytorch中自动获取对角矩阵条纹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要获得矩阵的对角线条纹(不确定此处的术语,对角线矩阵条纹似乎最能描述它).

I need to get a diagonal stripe of the matrix (not sure about the terminology here, diagonal matrix stripe seems to describe it best).

说,我有一个大小为KxN的矩阵,其中K和N是任意大小,K> N.说,我有一个矩阵:

Say, I have a matrix of size KxN, where K and N are arbitrary sizes and K>N. Say, I have a matrix:

[[ 0  1  2]
 [ 3  4  5]
 [ 6  7  8]
 [ 9 10 11]]

我需要从中提取对角线条纹,在这种情况下,是通过截断原始像素而创建的矩阵MxV大小:

From it I would need to extract a diagonal stripe, in this case, a matrix MxV size that is created by truncating the original one:

[[ 0  x  x]
 [ 3  4  x]
 [ x  7  8]
 [ x  x  11]]

所以结果矩阵为:

[[ 0  4  8]
 [ 3  7  11]]

这是一个使用矩阵蒙版的小示例代码,以去除蒙版位置:

Here is a small example code using masking for the matrices, to strip out the masked positions:

import numpy as np
X=np.arange(12).reshape(4,3)
mask=np.asarray([
  [ True,  False,  False],
  [ True,  True,  False], 
  [ False, True,  True], 
  [ False, False,  True]
])

>>> mask
array([[ True, False, False],
       [ True,  True, False],
       [False,  True,  True],
       [False, False,  True]], dtype=bool)

>>> X
array([[ 0,  1,  2],
       [ 3,  4,  5],
       [ 6,  7,  8],
       [ 9, 10, 11]])

>>> X.T[mask.T].reshape(3,2).T
array([[ 0,  4,  8],
       [ 3,  7, 11]])

但是我看不到如何自动将这种蒙版生成为任何K和N尺寸. 39x9或360x96

But I don't see how such a mask could be automatically generated to any K and N sizes, e.i. 39x9 or 360x96

感谢您的帮助.也许有一些功能可以自动在numpy,scipy或pytorch中执行此操作?

Any help is appreciated. Maybe there is some function that does this automatically either in numpy, scipy or pytorch?

我还有一个问题,是否可能代替:

I’ve got another question, is it possible instead of getting:

[[ 0  x  x]
 [ 3  4  x]
 [ x  7  8]
 [ x  x  11]]

要获得像这样的反向条纹:

To get a reverse stripe like this:

[[ x   x   2]
 [ x   4   5]
 [ 6   7   x]
 [ 9   x   x]]

推荐答案

stride_tricks做到了:

>>> import numpy as np
>>> 
>>> def stripe(a):
...    a = np.asanyarray(a)
...    *sh, i, j = a.shape
...    assert i >= j
...    *st, k, m = a.strides
...    return np.lib.stride_tricks.as_strided(a, (*sh, i-j+1, j), (*st, k, k+m))
... 
>>> a = np.arange(24).reshape(6, 4)
>>> a
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11],
       [12, 13, 14, 15],
       [16, 17, 18, 19],
       [20, 21, 22, 23]])
>>> stripe(a)
array([[ 0,  5, 10, 15],
       [ 4,  9, 14, 19],
       [ 8, 13, 18, 23]])

如果a是一个数组,则会创建一个可写的视图,这意味着如果您觉得自己很愿意,可以做类似的事情

If a is an array this creates a writable view, meaning that if you feel so inclined you can do things like

>>> stripe(a)[...] *= 10
>>> a
array([[  0,   1,   2,   3],
       [ 40,  50,   6,   7],
       [ 80,  90, 100,  11],
       [ 12, 130, 140, 150],
       [ 16,  17, 180, 190],
       [ 20,  21,  22, 230]])

更新:可以相同的方式获得从左下到右上的条纹.只是轻微的复杂性:它与原始数组的地址不同.

UPDATE: bottom-left to top-right stripes can be obtained in the same spirit. Only minor complication: It is not based at the same address as the original array.

>>> def reverse_stripe(a):
...     a = np.asanyarray(a)
...     *sh, i, j = a.shape
...     assert i >= j
...     *st, k, m = a.strides
...     return np.lib.stride_tricks.as_strided(a[..., j-1:, :], (*sh, i-j+1, j), (*st, k, m-k))
... 
>>> a = np.arange(24).reshape(6, 4)
>>> reverse_stripe(a)
array([[12,  9,  6,  3],
       [16, 13, 10,  7],
       [20, 17, 14, 11]])

这篇关于在numpy或pytorch中自动获取对角矩阵条纹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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