简体   繁体   English

Python - numpy 矩阵每列中前 n 个元素的索引

[英]Python - Indices of top n elements in each column of numpy matrix

What is the best way to select top n elements from each column of a numpy matrix of 2 dimension?(apart from using for loop)从二维 numpy 矩阵的每一列中 select 前 n 个元素的最佳方法是什么?(除了使用 for 循环)

Maybe slicing?也许切片?

if you have:如果你有:

matr = np.random.randn(4, 5)

you can simply select for example first 2 elements of first 2 cols doing:您可以简单地 select 例如前 2 个 cols 的前 2 个元素做:

matr[:2,0:2]

Try those snippets of code and check if it's what you was looking for.尝试这些代码片段并检查它是否是您要查找的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM