简体   繁体   English

如何在numpy数组和向量行之间执行包含操作?

[英]How to perform contain operation between a numpy array and a vector row-wise?

Now I have a numpy array, 现在我有一个numpy数组,

[[1 2]
 [3 4]
 [2 5]]

and a vector. 和一个矢量。

[2,
 5,
 2]

I want to perform a contain operation between the array and the vector row wise. 我想在数组和向量行之间执行包含操作。 In other words, I want to check whether the first row [1, 2] contain 2 , whether the second row [3, 4] contain 5 . 换句话说,我想检查第一行[1, 2]是否包含2 ,第二行[3, 4]包含5 The expected output would look like: 预期的输出看起来像:

[True, False, True] 

How could I implement this function? 我怎么能实现这个功能? Many thanks in advance. 提前谢谢了。

You can broadcast the vector into a column, equate it to all the elements in the rows of the matrix, and see if any element is True in each row: 您可以将向量广播到列中,将其等同于矩阵行中的所有元素,并查看每行中是否有any元素为True

import numpy as np

a = np.array(
    [[1, 2],
     [3, 4],
     [2, 5]])
v = np.array([2, 5, 2]).reshape(-1, 1)

np.any(a == v, axis=1)

https://ideone.com/YmWtlv https://ideone.com/YmWtlv

reshape turns your 1D (row) vector into a column vector. reshape将您的1D(行)向量转换为列向量。 This is necessary because normally broadcasting lines up the shapes along the right, so you need an explicit trailing dimension of 1. Another way to accomplish the same thing is to use newaxis (aka None ): 这是必要的,因为通常广播沿着右边的方向排列,所以你需要一个明确的尾随维度1.另一种完成同样事情的方法是使用newaxis (又名None ):

v = np.array([2, 5, 2])[..., np.newaxis]

Note 注意

My original answer suggested reduce using logical_or , which is just a more complicated way of saying any : 我原来的答复建议reduce使用logical_or ,这只是说的更复杂的方法any

np.logical_or.reduce(a == v, axis=1)

using a list comprehension and zip 使用列表理解和zip

arr = np.array([[1, 2],[3, 4],[2, 5]])
a = np.array([2,5,2])

[y in x for x, y in zip(arr, a)]
# outputs: [True, False, True]

Using np.any with axis=1 : 使用axis=1 np.any:

np.any(arr == a[:, None], axis=1)
# outputs: array([ True, False,  True])

This one-liner should do it. 这个单行应该这样做。 It works for any number of columns, too. 它也适用于任意数量的列。

# Set up
import numpy as np
array = np.array([[1, 2],[3, 4],[2, 5]])
vector = np.array([2,5,2])

# Solution
result = np.logical_or.reduce(array == vector[:,None], 1)

Output: 输出:

[ True, False,  True]

This compares all of the elements against the column vector vector and then reduces over rows. 这将所有元素与向量vector进行比较,然后减少行数。

We can also make use of broadcasting and the dot product. 我们也可以利用广播和点积。

a = np.array([[1, 2], [3, 4], [2, 5]])
b = np.array([2, 5, 2]) 

Solution

(a == b[:,None]) @ np.array([1, 1]).astype(bool) 

You can compare the first column and second column of the 2D array with the vector and perform a logical or operation on the results obtained. 您可以将2D阵列的第一列和第二列与矢量进行比较,并对获得的结果执行logical or操作。

import numpy as np
a = np.array([[1, 2],[3, 4],[2, 5]])
b = [2,5,2]
np.logical_or(a[:,0] == b , a[:,1] == b)

Output: 输出:

array([ True, False,  True], dtype=bool)

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

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