删除零行二维 numpy 数组 [英] remove zero lines 2-D numpy array

查看:35
本文介绍了删除零行二维 numpy 数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 numpy 中运行了一个 qr 分解,它返回一个 ndarrays 列表,即 Q>R:

<预><代码>>>>[q,r] = np.linalg.qr(np.array([1,0,0,0,1,1,1,1,1]).reshape(3,3))

R 是一个二维数组,在底部旋转零线(甚至证明了我测试集中的所有示例):

<预><代码>>>>打印[[ 1.41421356 0.70710678 0.70710678][ 0. 1.22474487 1.22474487][ 0. 0. 0. ]]

.现在,我想将 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.allaxis 参数:

<预><代码>>>>r[np.all(r == 0, 轴=1)]数组([[ 0., 0., 0.]])>>>r[~np.all(r == 0, 轴=1)]数组([[-1.41421356, -0.70710678, -0.70710678],[ 0. , -1.22474487, -1.22474487]])

I run a qr factorization in numpy which returns a list of ndarrays, namely Qand 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屋!

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