Skip to main content
 首页 » 编程设计

python之numpy 循环和 where 条件的替代方法

2025年02月15日7birdshome

我有 2 个非常大的 numpy 数组。如何根据条件获取数组 2 在 array1 中的索引?条件是:对于数组 1 中的每个值,找到数组 2 中的第一个值并将索引 - 2 复制到数组 1。我希望这是有道理的。我在下面说明了我的意思。

Numpy 数组 1

+---------+------------+ 
| IndexNr |   Value    | 
+---------+------------+ 
|       0 | 1485963480 | 
|       1 | 1486029060 | 
|       2 | 1486032900 | 
|       3 | 1486032960 | 
|       4 | 1486033020 | 
|       5 | 1486033080 | 
|       6 | 1486034940 | 
|       7 | 1486038480 | 
|       8 | 1486039140 | 
|       9 | 1486039320 | 
|      10 | 1486039500 | 
|      11 | 1486041300 | 
|      12 | 1486044360 | 
|      13 | 1486048740 | 
|      14 | 1486049880 | 
|      15 | 1486049940 | 
|      16 | 1486050240 | 
|      17 | 1486114200 | 
|      18 | 1486119840 | 
|      19 | 1486119900 | 
|      20 | 1486120380 | 
|      21 | 1486134720 | 
|      22 | 1486134780 | 
|      23 | 1486135380 | 
|      24 | 1486135440 | 
+---------+------------+ 

Numpy 数组 2

+---------+------------+ 
| IndexNr |   Value    | 
+---------+------------+ 
|       0 | 1485963300 | 
|       1 | 1486029000 | 
|       2 | 1486032600 | 
|       3 | 1486032900 | 
|       4 | 1486034700 | 
|       5 | 1486038300 | 
|       6 | 1486038900 | 
|       7 | 1486039200 | 
|       8 | 1486041300 | 
|       9 | 1486044300 | 
|      10 | 1486048500 | 
|      11 | 1486049700 | 
|      12 | 1486114200 | 
|      13 | 1486119900 | 
|      14 | 1486120200 | 
|      15 | 1486134600 | 
+---------+------------+ 

像这样的东西

+-------+-------------+ 
| Index | ArrayIndex2 | 
+-------+-------------+ 
|     0 | nan         | 
|     1 | nan         | 
|     2 | nan         | 
|     3 | 2           | 
|     4 | 2           | 
|     5 | 2           | 
|     6 | 3           | 
|     7 | 4           | 
|     8 | 5           | 
|     9 | 6           | 
|    10 | 6           | 
|    11 | 6           | 
|    12 | 8           | 
|    13 | 9           | 
|    14 | 10          | 
|    15 | 10          | 
|    16 | 10          | 
|    17 | 10          | 
|    18 | 11          | 
|    19 | 11          | 
|    20 | 13          | 
|    21 | 14          | 
|    22 | 14          | 
|    23 | 14          | 
|    24 | 14          | 
+-------+-------------+ 

这是我的解决方法。

import numpy as np 
 
array1 = np.array([1486029060,1486032720,1486032900,1486032960,1486033020,1486033080,1486034940,1486038480,1486039140,1486039320,1486039500,1486041300,1486044360,1486048740,1486049880,1486049940,1486050240,1486114200,1486119840,1486119900,1486120380,1486134720,1486134780,1486135380,1486135440]) 
array2 = np.array([1485963300,1486029000,1486032600,1486032900,1486034700,1486038300,1486038900,1486039200,1486041300,1486044300,1486048500,1486049700,1486114200,1486119900,1486120200,1486134600]) 
 
# create an empty holder with the same size as array 1 
array1_result = np.empty((array1.shape[0], 1)) 
array1_result.fill(np.nan) 
 
# loop and create result array 
for rowNr in range(array1.shape[0]): 
    if (rowNr > 2): 
        currentRowValue = array1[rowNr] 
        array1_result[rowNr] = np.reshape(np.array(np.where(array2 < currentRowValue)), (-1, 1))[-2][0] #array2Index 
 
print(array1_result) 

唯一的问题是这种循环在大数组上效率很低。 array1 和 array2 都是几百万行,使用这种方法非常慢。有没有其他方法可以达到同样的效果?

请您参考如下方法:

听起来像你想要的searchsorted

>>> np.searchsorted(array2, array1) 
array([ 2,  3,  3,  4,  4,  4,  5,  6,  7,  8,  8,  8, 10, 11, 12, 12, 12, 
       12, 13, 13, 15, 16, 16, 16, 16], dtype=int64) 

或者编码你的“但不是前两行”条件

>>> np.searchsorted(array2, array1[2:]) - 2 
array([ 1,  2,  2,  2,  3,  4,  5,  6,  6,  6,  8,  9, 10, 10, 10, 10, 11, 
       11, 13, 14, 14, 14, 14], dtype=int64) 
>>> np.concatenate(([0,0], _)) 
array([ 0,  0,  1,  2,  2,  2,  3,  4,  5,  6,  6,  6,  8,  9, 10, 10, 10, 
   10, 11, 11, 13, 14, 14, 14, 14], dtype=int64) 

这在一个地方不同意 - 1。我想这就是平等和不平等的区别