0

我有一个 numpy 数组:

array([  2.86656000e+09,   2.86688000e+09,   2.86708000e+09,
     2.86860000e+09,   2.86884000e+09,   2.86908000e+09,
     2.86920000e+09,   2.87024000e+09,   2.87040000e+09,
     2.87056000e+09,   2.87076000e+09,   2.87108000e+09,
     2.87120000e+09,   2.87152000e+09,   2.87260000e+09,
     2.87272000e+09,   2.87280000e+09,   2.87448000e+09,
     2.87464000e+09,   2.87476000e+09,   2.87484000e+09])

对相似值进行分组的最佳方法是什么(例如,差异不大于 1000000)?谢谢解答!!

4

1 回答 1

4

作为评论中链接的解决方案的替代方案,这样的事情应该可以工作:

In [1]: import numpy as np

In [2]: arr = np.array([  2.86656000e+09,   2.86688000e+09,   2.86708000e+09,
   ...:      2.86860000e+09,   2.86884000e+09,   2.86908000e+09,
   ...:      2.86920000e+09,   2.87024000e+09,   2.87040000e+09,
   ...:      2.87056000e+09,   2.87076000e+09,   2.87108000e+09,
   ...:      2.87120000e+09,   2.87152000e+09,   2.87260000e+09,
   ...:      2.87272000e+09,   2.87280000e+09,   2.87448000e+09,
   ...:      2.87464000e+09,   2.87476000e+09,   2.87484000e+09])

In [3]: np.split(arr, np.where(np.diff(arr) > 1000000)[0] + 1)
Out[3]: 
[array([  2.86656000e+09,   2.86688000e+09,   2.86708000e+09]),
 array([  2.86860000e+09,   2.86884000e+09,   2.86908000e+09,
         2.86920000e+09]),
 array([  2.87024000e+09,   2.87040000e+09,   2.87056000e+09,
         2.87076000e+09,   2.87108000e+09,   2.87120000e+09,
         2.87152000e+09]),
 array([  2.87260000e+09,   2.87272000e+09,   2.87280000e+09]),
 array([  2.87448000e+09,   2.87464000e+09,   2.87476000e+09,
         2.87484000e+09])]
于 2013-02-10T22:03:06.000 回答