简体   繁体   English

选择数组中向量的分量

[英]Selecting components of vectors in array

I have a tensor with shape (7, 2, 3) I want to select one of the two row vectors from each of the 7 2x3 matrices, ie我有一个形状为 (7, 2, 3) 的张量我想 select 来自 7 个 2x3 矩阵中的每一个矩阵的两个行向量之一,即

[
 [[0, 0, 0],
  [1, 1, 1]],

 [[0, 0, 0],
  [1, 1, 1]],
 ...x7
]

to

a = [
 [0, 0, 0],
 [0, 0, 0],
 [0, 0, 0]
 ...x7
]
b = [
 [1, 1, 1],
 [1, 1, 1],
 [1, 1, 1]
 ...x7
]

each with shape (7, 3).每个都有形状 (7, 3)。

How can I do this without reshape ?我怎样才能做到这一点而不reshape (I find reshape to be kind of confusing when some dimensions are the same). (当某些尺寸相同时,我发现reshape有点令人困惑)。

I also know of我也知道

np.array(map(lambda item: item[0], x)))

but I would like a more concise way if there is one.但如果有的话,我想要一种更简洁的方式。

just use looped indexing: data[:, i, :] where i loops from 0 through 1只需使用循环索引: data[:, i, :]其中i从 0 循环到 1

import numpy as np

a = np.array([
 [[0, 0, 0],
  [1, 1, 1]],

 [[0, 0, 0],
  [1, 1, 1]]
])
print(a[:, 1, :])

will produce会产生

[[1 1 1]
 [1 1 1]]

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

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