📜  Python| numpy matrix.choose()

📅  最后修改于: 2022-05-13 01:55:43.822000             🧑  作者: Mango

Python| numpy matrix.choose()

借助Numpy matrix.choose()方法,我们可以通过将参数作为数组传递来从矩阵中选择元素,该数组包含要选择的行号的索引。与参数中传递的大小相同的输出数组。

示例 #1:
在这个例子中,我们可以看到在matrix.choose()方法的帮助下,我们能够从矩阵中提取一个选择数组。

# import the important module in python
import numpy as np
             
# make a matrix with numpy
gfg = np.matrix('[1, 2, 3, 4; 3, 1, 5, 6]')
             
# applying matrix.choose() method
geeks = np.choose([1, 0, 1, 0], gfg)
       
print(geeks)
输出:
[[3 2 5 4]]

示例 #2:

# import the important module in python
import numpy as np
             
# make a matrix with numpy
gfg = np.matrix('[1, 2, 3; 4, 5, 6; 7, 8, 9]')
             
# applying matrix.choose() method
geeks = np.choose([2, 0, 1], gfg)
       
print(geeks)
输出:
[[7 2 6]]