Skip to main content
 首页 » 编程设计

python之给定条件的矩阵上的 Numpy 高级索引

2025年05月04日33kevingrace

如何使用 np 索引选择具有特定条件的矩阵行?

我的矩阵是

n = np.array([[1,2],[4,5], [1,22]]) 

我想选择第一个元素大于 1 的行。类似于:

n[lambda x: x[0] > 1] 

请您参考如下方法:

编辑:np.where 是可选的,感谢@user2357112。

n[n[:, 0] > 1] 

Try

n[np.where(n[:, 0] > 1)] 

where np.where returns an array of row indices that satisfy the given condition.