简体   繁体   English

基于另一个数组从 numpy 数组的行和列中取值

[英]Take values from row and column of numpy array based on another array

I have a np array of weights我有一个 np 权重数组

mat = np.array([
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
])

I have another numpy array containing the row and index to extract from the weight matrix.我有另一个 numpy 数组,其中包含要从权重矩阵中提取的行和索引。

row_col = np.array([
    [1, 1], # row 1, col 1
    [2, 2], # row 2, col 2
    [0, 2], # row 0, col 2
    [1, 0]  # row 1, col 0
])

How do I get the output:如何获得 output:

[5, 9, 3, 4] 

Try this:尝试这个:

mat[row_col[:, 0], row_col[:, 1]]

Output: array([5, 9, 3, 4]) Output: array([5, 9, 3, 4])

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

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