[英]Save rows of a bidimensional numpy array in another array
I have a bidimensional np array V (100000x50). 我有一个二维np数组V(100000x50)。 I want to create a new array V_tgt in which I keep just certain rows of V, so the dimension will be (ix50).
我想创建一个新数组V_tgt,在其中仅保留V的某些行,因此维将为(ix50)。 It may be easy to do it but I tried different things and it seems to save just the first of the 50 elements.
这样做可能很容易,但是我尝试了不同的方法,它似乎只保存了50个元素中的第一个。 My code is the following:
我的代码如下:
V_tgt = np.array([])
for i in IX_items:
if i in IX_tgt_items:
V_tgt=np.append(V_tgt, V[i])
I tried with functions such as insert and delete as well but it didn't work.How can I save all the values and create an array with the right dimension? 我也尝试了插入和删除之类的功能,但是没有用,如何保存所有值并创建尺寸正确的数组? Any help is really appreciated.
任何帮助都非常感谢。
From your comments I assume that you have some kind of list of target indices (in my example tgt_idx1
and tgt_idx2
)that tells you which elements to take from V. You could do something like this: 根据您的评论,我假设您具有某种类型的目标索引列表(在我的示例中为
tgt_idx1
和tgt_idx2
),这些列表告诉您要从V中获取哪些元素。您可以执行以下操作:
import numpy as np
V = np.array([[1,2,3], [4,5,6], [7,8,9], [10, 11, 12]])
tgt_idx1 = np.array([1, 2, 3])
tgt_idx2 = np.array([1, 3])
mask = []
for i, elem in enumerate(V):
inTargets = i in tgt_idx1 and i in tgt_idx2
mask.append(inTargets)
print mask
V_tgt = V[mask]
print V_tgt
This prints 此打印
[False, True, False, True]
[[ 4 5 6]
[10 11 12]]
Let's first simplify the problem. 让我们首先简化问题。 With an initial
array
( a
): 带有初始
array
( a
):
a = np.array([45, 29, 76, 23, 76, 98, 21, 63])
and the index
arrays
: 和
index
arrays
:
i1 = np.array([1, 3, 5, 7, 9])
i2 = np.array([0, 1, 2, 3, 4])
then we can use a simple list
comprehension to get the elements from a
that are at indexes
in both i1
and i2
: 那么我们可以用一个简单的
list
理解来获取元素a
是在indexes
均i1
和i2
:
np.array([e for i, e in enumerate(a) if i in i1 and i in i2])
which is very readable and outputs: 这是非常可读和输出:
array([29, 23])
I am sure you can adapt this to the variables
you have given to your arrays
. 我敢肯定,您可以将其适应给定
arrays
的variables
。
The performance of np.append is probably killing this, why not create a new overlap of your two indices, then subset: np.append的性能可能会消除这一点,为什么不创建两个索引然后是子集的新重叠:
#using @Joe Iddons data
a = np.array([45, 29, 76, 23, 76, 98, 21, 63])
i1 = np.array([1, 3, 5, 7, 9])
i2 = np.array([0, 1, 2, 3, 4])
Then find the intersect of i1 and i2: 然后找到i1和i2的交集:
indices = np.intersect1d(i1,i2)
and subset: 和子集:
a[indices]
array([29, 23])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.