如何有条件地组合两个形状相同的 numpy 数组 [英] How to conditionally combine two numpy arrays of the same shape

查看:60
本文介绍了如何有条件地组合两个形状相同的 numpy 数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这听起来很简单,我想我把它复杂化了.

This sounds simple, and I think I'm overcomplicating this in my mind.

我想制作一个数组,其元素由两个形状相同的源数组生成,具体取决于源数组中哪个元素更大.

I want to make an array whose elements are generated from two source arrays of the same shape, depending on which element in the source arrays is greater.

举例说明:

import numpy as np
array1 = np.array((2,3,0))
array2 = np.array((1,5,0))

array3 = (insert magic)
>> array([2, 5, 0))

我不知道如何生成一个 array3,它结合了 array1 和 array2 的元素来生成一个数组,其中只采用两个 array1/array2 元素值中的较大值.

I can't work out how to produce an array3 that combines the elements of array1 and array2 to produce an array where only the greater of the two array1/array2 element values is taken.

任何帮助将不胜感激.谢谢.

Any help would be much appreciated. Thanks.

推荐答案

我们可以使用 NumPy 内置的 np.maximum,正是为此目的而制作的 -

We could use NumPy built-in np.maximum, made exactly for that purpose -

np.maximum(array1, array2)

另一种方法是使用 NumPy ufunc np.max2D 堆叠阵列和 max-reduce 沿第一轴 (axis=0) -

Another way would be to use the NumPy ufunc np.max on a 2D stacked array and max-reduce along the first axis (axis=0) -

np.max([array1,array2],axis=0)

100 万个数据集的计时 -

Timings on 1 million datasets -

In [271]: array1 = np.random.randint(0,9,(1000000))

In [272]: array2 = np.random.randint(0,9,(1000000))

In [274]: %timeit np.maximum(array1, array2)
1000 loops, best of 3: 1.25 ms per loop

In [275]: %timeit np.max([array1, array2],axis=0)
100 loops, best of 3: 3.31 ms per loop

# @Eric Duminil's soln1
In [276]: %timeit np.where( array1 > array2, array1, array2)
100 loops, best of 3: 5.15 ms per loop

# @Eric Duminil's soln2
In [277]: magic = lambda x,y : np.where(x > y , x, y)

In [278]: %timeit magic(array1, array2)
100 loops, best of 3: 5.13 ms per loop

<小时>

扩展到其他支持的 ufuncs

类似地,有 np.minimum 用于在两个相同或可广播形状的数组之间查找元素方面的最小值.因此,要在 array1array2 之间找到元素方面的最小值,我们将:

Similarly, there's np.minimum for finding element-wise minimum values between two arrays of same or broadcastable shapes. So, to find element-wise minimum between array1 and array2, we would have :

np.minimum(array1, array2)

有关支持此功能的 ufuncs 的完整列表,请参阅 docs 并查找关键字:element-wise.Grep-ing 对于那些,我得到了以下 ufunc:

For a complete list of ufuncs that support this feature, please refer to the docs and look for the keyword : element-wise. Grep-ing for those, I got the following ufuncs :

加、减、乘、除、logaddexp、logaddexp2、true_divide、floor_divide、幂、余数、mod、fmod、divmod、heaviside、gcd、lcm、arctan2、hypot、bitwise_and、bitwise_or、bitwise_xor、left_shift、right_shift,更大,greater_equal,更少,less_equal,not_equal,相等,logical_and,logical_or,logical_xor,最大值,最小值,fmax,fmin、copysign、nextafter、ldexp、fmod

add, subtract, multiply, divide, logaddexp, logaddexp2, true_divide, floor_divide, power, remainder, mod, fmod, divmod, heaviside, gcd, lcm, arctan2, hypot, bitwise_and, bitwise_or, bitwise_xor, left_shift, right_shift, greater, greater_equal, less, less_equal, not_equal, equal, logical_and, logical_or, logical_xor, maximum, minimum, fmax, fmin, copysign, nextafter, ldexp, fmod

这篇关于如何有条件地组合两个形状相同的 numpy 数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆