删除零线2-D numpy数组 [英] remove zero lines 2-D numpy array
本文介绍了删除零线2-D numpy数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在numpy
中运行qr factorization
,它返回ndarrays
的列表,即Q
和R
:
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
是一个二维数组,在底部具有旋转的零线(即使在我的测试集中为所有示例都证明了这一点):
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. ]]
.现在,我想将R
分为两个矩阵R_~
:
. Now, I want to divide R
in two matrices R_~
:
[[ 1.41421356 0.70710678 0.70710678]
[ 0. 1.22474487 1.22474487]]
和R_0
:
[[ 0. 0. 0. ]]
(提取所有零线).似乎接近此解决方案:删除numpy数组中的行.
(extracting all zero-lines). It seems to be close to this solution: deleting rows in numpy array.
更有趣的是:np.linalg.qr()
返回一个n x n
-矩阵.不,我期望的是:
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
推荐答案
将np.all
与axis
参数一起使用:
>>> 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]])
这篇关于删除零线2-D numpy数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文