我是 numpy
的新手,我很难理解如何从 np.array
中提取具有已定义列和行的子矩阵:
Y = np.arange(16).reshape(4,4)
如果我想提取第 0 和第 3 列/行,我应该:
[[0 3]
[12 15]]
我尝试了所有的 reshape 功能......但无法弄清楚如何做到这一点。有什么想法吗?
最佳答案
给 np.ix_
试一试:
Y[np.ix_([0,3],[0,3])]
这将返回您想要的结果:
In [25]: Y = np.arange(16).reshape(4,4)
In [26]: Y[np.ix_([0,3],[0,3])]
Out[26]:
array([[ 0, 3],
[12, 15]])
关于python - Numpy 提取子矩阵,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19161512/