簡體   English   中英

Python - 根據標准組合兩個 arrays

[英]Python - Combining two arrays based on criteria

我正在嘗試根據某些標准將兩個 numpy 2D arrays 組合成一個數組,例如

# array1
[1,2,3,4,5]
[10,11,12,13,14]

# array2
[15,16,17,18,19]
[6,7,8,9,10] 

根據標准 - 最高值 - 生成的數組 1 和數組 2 的組合如下所示;

[15,16,17,18,19]
[10,11,12,13,14]

我知道如何將兩個 arrays 連接在一起,過濾,但我似乎找不到可以為我提供上述結果的 function。

也許是np.where的變體?

使用numpy.where ,對於 arrays ab ,您可以使用:

np.where(a>b, a, b)

對於您的示例:

>>> import numpy as np
>>> a = np.array([[1,2,3,4,5],[10,11,12,13,14]])
>>> b = np.array([[15,16,17,18,19],[6,7,8,9,10]])
>>> np.where(a>b, a, b)
array([[15, 16, 17, 18, 19],
       [10, 11, 12, 13, 14]])

您可以使用numpy.maximum

import numpy as np

a1 = np.array([[1,2,3,4,5],[10,11,12,13,14]])
a2 = np.array([[15,16,17,18,19],[10,11,12,13,14]])

print(np.maximum(a1, a2))
# [[15 16 17 18 19]
#  [10 11 12 13 14]]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM