Python Numpy 提取子矩阵

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19161512/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 13:01:59  来源:igfitidea点击:

Numpy extract submatrix

pythonnumpy

提问by user1595929

I'm pretty new in numpyand I am having a hard time understanding how to extract from a np.arraya sub matrix with defined columns and rows:

我是新手,numpy我很难理解如何从np.array具有定义的列和行的子矩阵中提取:

Y = np.arange(16).reshape(4,4)

If I want to extract columns/rows 0 and 3, I should have:

如果我想提取列/行 0 和 3,我应该有:

[[0 3]
 [12 15]]

I tried all the reshape functions...but cannot figure out how to do this. Any ideas?

我尝试了所有的重塑功能......但不知道如何做到这一点。有任何想法吗?

采纳答案by JoshAdel

Give np.ix_a try:

np.ix_一试:

Y[np.ix_([0,3],[0,3])]

This returns your desired result:

这将返回您想要的结果:

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]])

回答by mdml

One solution is to index the rows/columns by slicing/striding. Here's an example where you are extracting every third column/row from the first to last columns (i.e. the first and fourth columns)

一种解决方案是通过切片/跨行索引行/列。这是一个示例,您从第一列到最后一列(即第一列和第四列)中提取每第三列/行

In [1]: import numpy as np
In [2]: Y = np.arange(16).reshape(4, 4)
In [3]: Y[0:4:3, 0:4:3]
Out[1]: array([[ 0,  3],
               [12, 15]])

This gives you the output you were looking for.

这为您提供了您正在寻找的输出。

For more info, check out this page on indexing in NumPy.

有关更多信息,请查看有关在NumPy.

回答by CT Zhu

First of all, your Yonly has 4 col and rows, so there is no col4 or row4, at most col3 or row3.

首先,你Y只有4个col和row,所以没有col4或row4,最多col3或row3。

To get 0, 3 cols: Y[[0,3],:]To get 0, 3 rows: Y[:,[0,3]]

为了得到0,3的cols:Y[[0,3],:]为了得到0,3行:Y[:,[0,3]]

So to get the array you request: Y[[0,3],:][:,[0,3]]

因此,要获取您请求的数组: Y[[0,3],:][:,[0,3]]

Note that if you just Y[[0,3],[0,3]]it is equivalent to [Y[0,0], Y[3,3]]and the result will be of two elements: array([ 0, 15])

请注意,如果你只是Y[[0,3],[0,3]]它等价于[Y[0,0], Y[3,3]]并且结果将是两个元素:array([ 0, 15])

回答by ali_m

You can also do this using:

您也可以使用以下方法执行此操作:

Y[[[0],[3]],[0,3]]

which is equivalent to doing this using indexing arrays:

这相当于使用索引数组执行此操作:

idx = np.array((0,3)).reshape(2,1)
Y[idx,idx.T]

To make the broadcasting work as desired, you need the non-singleton dimension of your indexing array to be aligned with the axis you're indexing into, e.g. for an n x m 2D subarray:

为了使广播按需要工作,您需要索引数组的非单一维度与您正在索引的轴对齐,例如对于 nxm 2D 子数组:

Y[<n x 1 array>,<1 x m array>]

This doesn't create an intermediate array, unlike CT Zhu's answer, which creates the intermediate array Y[(0,3),:], then indexes into it.

这不会创建中间数组,这与 CT Zhu 的答案不同,后者创建了中间数组Y[(0,3),:],然后对其进行索引。

回答by Hima

print y[0:4:3,0:4:3]

is the shortest and most appropriate fix .

是最短和最合适的修复。