【问题标题】:Splitting lists by short numbers按短数字拆分列表
【发布时间】:2015-07-26 06:57:35
【问题描述】:

我正在使用 NumPy 在图表上查找交叉点,但 isClose 每个交叉点返回多个值

所以,我将尝试找出他们的平均值。但首先,我想隔离相似的值。这也是我觉得很有用的技能。

我有一个名为idx 的交叉点的 x 值列表,看起来像这样

[-8.67735471 -8.63727455 -8.59719439 -5.5511022  -5.51102204 -5.47094188
 -5.43086172 -2.4248497  -2.38476954 -2.34468938 -2.30460922  0.74148297
  0.78156313  0.82164329  3.86773547  3.90781563  3.94789579  3.98797595
  7.03406814  7.0741483   7.11422846]

我想把它分成由相似数字组成的列表。

这是我目前所拥有的:

n = 0
for i in range(len(idx)):
    try:
        if (idx[n]-idx[n-1])<0.5:
            sdx.append(idx[n-1])
        else:
            print(sdx)
            sdx = []
    except:
        sdx.append(idx[n-1])
    n = n+1

它在大多数情况下都有效,但它忘记了一些数字:

[-8.6773547094188377, -8.6372745490981959]
[-5.5511022044088181, -5.5110220440881763, -5.4709418837675354]
[-2.4248496993987976, -2.3847695390781567, -2.3446893787575149]
[0.7414829659318638, 0.78156312625250379]
[3.8677354709418825, 3.9078156312625243, 3.9478957915831661]

可能有一种更有效的方法来做到这一点,有人知道吗?

【问题讨论】:

  • 这个的最终用途是什么?你在做直方图吗?是什么决定了分组——仅仅是它们之间的距离在 0.5 以内吗?您希望在 [-0.5, 0.0, 0.5, 1.0, 1.5] 上发生什么?
  • 请描述这应该做什么。从非功能代码中猜测它不是一种选择。您的 idx 缺少的所有逗号在哪里?
  • 这是一个 numpy 数组吗?
  • 你为什么要循环使用for i in range(len(idx)):,然后使用n(你必须手动增加)来索引列表?
  • 我对问题进行了编辑以回答其中的一些问题,抱歉。 idx 也是一个 numpy 数组,这可能就是它没有逗号的原因。

标签: python python-3.x numpy split


【解决方案1】:

考虑到你有一个numpy数组,你可以使用np.split,区别在哪里拆分>.5

import numpy as np
x = np.array([-8.67735471, -8.63727455, -8.59719439, -5.5511022, -5.51102204, -5.47094188,
     -5.43086172, -2.4248497, -2.38476954, -2.34468938, -2.30460922, 0.74148297,
     0.78156313, 0.82164329, 3.86773547, 3.90781563, 3.94789579, 3.98797595,
     7.03406814, 7.0741483])


print np.split(x, np.where(np.diff(x) > .5)[0] + 1)

[array([-8.67735471, -8.63727455, -8.59719439]), array([-5.5511022 , -5.51102204, -5.47094188, -5.43086172]), array([-2.4248497 , -2.38476954, -2.34468938, -2.30460922]), array([ 0.74148297,  0.78156313,  0.82164329]), array([ 3.86773547,  3.90781563,  3.94789579,  3.98797595]), array([ 7.03406814,  7.0741483 ])]

np.where(np.diff(x) &gt; .5)[0] 返回以下元素不满足np.diff(x) &gt; .5) 条件的索引:

In [6]: np.where(np.diff(x) > .5)[0]
Out[6]: array([ 2,  6, 10, 13, 17])

+ 1 每个索引加 1:

In [12]: np.where(np.diff(x) > .5)[0] + 1
Out[12]: array([ 3,  7, 11, 14, 18])

然后将[ 3, 7, 11, 14, 18] 传递给 np.split 将元素拆分为子数组,x[:3], x[3:7],x[7:11] ...

【讨论】:

    【解决方案2】:

    如果您的最终目的地正在查找每个集群/组的平均值,其中每个集群将被标记为不超过特定阈值的微小差异,您可以使用下面列出的方法。

    基本上,我们将输入列表转换为 numpy 数组,对其进行排序,然后找到连续的差异。基于与某个阈值比较时的差异,我们为来自同一组的元素创建一个具有相同 ID 的 ID 数组。最后,使用这些 ID,我们在 np.bincount 的 bin 内进行 binning 和平均,基本上得到每个组的平均值。

    这是实现 -

    import numpy as np
    
    # Input list
    AList = [-8.67735471, -8.63727455, -8.59719439, -5.5511022,  -5.51102204,
             -5.47094188, -5.43086172, -2.4248497,  -2.38476954, -2.34468938,
             -2.30460922,  0.74148297,  0.78156313,  0.82164329,  3.86773547,
        3.90781563, 3.94789579,  3.98797595,  7.03406814,  7.0741483, 7.11422846]
    
    # Tolerance as thresholding parameter to distinguish between two "groups"
    tolerance = 1
    
    # Convert to a numpy array and sort if not already sorted
    A = np.sort(np.asarray(AList))
    
    # ID array that has the same IDs for elements of the same group
    ID_array = (np.append([False],np.diff(A)>tolerance)).cumsum()
    
    # Finally get the average values for each group    
    average_values = np.bincount(ID_array,A)/np.bincount(ID_array)
    

    示例运行 -

    In [301]: A
    Out[301]: 
    array([-8.67735471, -8.63727455, -8.59719439, -5.5511022 , -5.51102204,
           -5.47094188, -5.43086172, -2.4248497 , -2.38476954, -2.34468938,
           -2.30460922,  0.74148297,  0.78156313,  0.82164329,  3.86773547,
            3.90781563,  3.94789579,  3.98797595,  7.03406814,  7.0741483 ,
            7.11422846])
    
    In [302]: average_values
    Out[302]: 
    array([-8.63727455, -5.49098196, -2.36472946,  0.78156313,  3.92785571,
            7.0741483 ])
    

    【讨论】:

      猜你喜欢
      • 2019-11-20
      • 1970-01-01
      • 2020-01-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-17
      • 2012-06-06
      • 2020-12-11
      相关资源
      最近更新 更多