删除零行二维 numpy 数组 [英] remove zero lines 2-D numpy array
问题描述
我在 numpy
中运行了一个 qr 分解
,它返回一个 ndarrays
列表,即 Q
和 >R
:
R
是一个二维数组,在底部旋转零线(甚至证明了我测试集中的所有示例):
.现在,我想将 R
分成两个矩阵 R_~
:
[[ 1.41421356 0.70710678 0.70710678][0.1.22474487 1.22474487]]
和R_0
:
[[ 0. 0. 0. ]]
(提取所有零线).似乎接近这个解决方案:删除numpy数组中的行.
更有趣的是:np.linalg.qr()
返回一个 n x n
-矩阵.不是,这是我所期望的:
A := n x mQ := n x mR := n x m
使用 np.all
和 axis
参数:
I run a qr factorization
in numpy
which returns a list of ndarrays
, namely Q
and R
:
>>> [q,r] = np.linalg.qr(np.array([1,0,0,0,1,1,1,1,1]).reshape(3,3))
R
is a two-dimensional array, having pivoted zero-lines at the bottom (even proved for all examples in my test set):
>>> print r
[[ 1.41421356 0.70710678 0.70710678]
[ 0. 1.22474487 1.22474487]
[ 0. 0. 0. ]]
. Now, I want to divide R
in two matrices R_~
:
[[ 1.41421356 0.70710678 0.70710678]
[ 0. 1.22474487 1.22474487]]
and R_0
:
[[ 0. 0. 0. ]]
(extracting all zero-lines). It seems to be close to this solution: deleting rows in numpy array.
EDIT:
Even more interesting: np.linalg.qr()
returns a n x n
-matrix. Not, what I would have expected:
A := n x m
Q := n x m
R := n x m
Use np.all
with an axis
argument:
>>> r[np.all(r == 0, axis=1)]
array([[ 0., 0., 0.]])
>>> r[~np.all(r == 0, axis=1)]
array([[-1.41421356, -0.70710678, -0.70710678],
[ 0. , -1.22474487, -1.22474487]])
这篇关于删除零行二维 numpy 数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!