简体   繁体   English

从间隔数组创建类别

[英]Create categories from array of intervals

Given a set of ranges as follow 给出一组范围如下

dates = [[1200, 1300], [1100, 1300], [1200, 1300], [1200, 1400], [1100, 1400]]

I would like to extract in an efficient way all the possible intervals and then count the number of ranges available in each interval. 我想以有效的方式提取所有可能的间隔,然后计算每个间隔中可用范围的数量。

For that example the resulting matrix of possible intervals would be: 对于该示例,可能的间隔的结果矩阵将是:

     [1100, 1200]  [1200, 1300]  [1300, 1400]
0           0          1           0
1           1          1           0
2           0          1           0
3           0          1           1
4           1          1           1

Then, the sum by column gives the number of ranges that are in each interval 然后,按列的总和给出每个间隔中的范围数

[1100, 1200]    2
[1200, 1300]    5
[1300, 1400]    2

Here is an approach giving you the wanted numpy matrix m , with boolean values: 这是一种方法,为您提供所需的numpy矩阵m ,具有布尔值:

def getOverlap(a, b):
    return max(0, min(a[1], b[1]) - max(a[0], b[0]))

nodes     = sorted(np.unique(np.array(dates).flatten()))
intervals = zip(nodes[:-1], nodes[1:])
# [(1100, 1200), (1200, 1300), (1300, 1400)]

m = np.array([[bool(getOverlap(i, d)) for d in dates] for i in intervals])

m.sum(axis=1)
# array([2, 5, 2])

Note that if you want the 'matrix' to be a pandas DataFrame , simply do: 请注意,如果您希望'matrix'是pandas DataFrame ,只需执行以下操作:

pd.DataFrame(m.transpose().astype(int), columns=intervals)

   (1100, 1200)  (1200, 1300)  (1300, 1400)
0             0             1             0
1             1             1             0
2             0             1             0
3             0             1             1
4             1             1             1

I have followed this method here. 我在这里遵循这个方法。 Can be more compact than this. 可以比这更紧凑。 Thats for another day! 那是另一天!

c=[[1200, 1300], [1100, 1300], [1200, 1300], [1200, 1400], [1100, 1400]]
print "string values", c
uniquea={}
new=[]
for i in c:
    j=str(i)
    if j in new:
        uniquea[j]+=1
    else:
        uniquea[j]=1
        new.append(j)

print uniquea, new print uniquea,new

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM