Olg*_*lga 7 python arrays indexing numpy
我用Python numpy.
我有一个numpy索引数组a:
>>> a
array([[5, 7],
[12, 18],
[20, 29]])
>>> type(a)
<type 'numpy.ndarray'>
Run Code Online (Sandbox Code Playgroud)
我有一个numpy索引数组b:
>>> b
array([[2, 4],
[8, 11],
[33, 35]])
>>> type(b)
<type 'numpy.ndarray'>
Run Code Online (Sandbox Code Playgroud)
我需要加入一个数组a的数组b:
a+ b=>[2, 4] [5, 7] [8, 11] [12, 18] [20, 29] [33, 35]
=> a并且b有索引数组=> [2, 18] [20, 29] [33, 35]
(索引([2, 4][5, 7][8, 11][12, 18])按顺序进行
=> 2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18=> [2, 18])
对于这个例子:
>>> out_c
array([[2, 18],
[20, 29],
[33, 35]])
Run Code Online (Sandbox Code Playgroud)
有人可以建议,我该怎么办out_c?
更新:@Geoff建议解决多个范围的python联合.这个解决方案是否是最快和最好的大数据阵列?
ranges = np.vstack((a,b))
ranges.sort(0)
# List of non-overlapping ranges
nonoverlapping = (ranges[1:,0] - ranges[:-1,1] > 1).nonzero()[0]
# Starts are 0, and all the starts not overlapped by their predecessor
starts = np.hstack(([0], nonoverlapping + 1))
# Ends are -1 and all the ends who aren't overlapped by their successor
ends = np.hstack(( nonoverlapping, [-1]))
# Result
result = np.vstack((ranges[starts, 0], ranges[ends, 1])).T
Run Code Online (Sandbox Code Playgroud)
(旧答案)使用列表和集合
import numpy as np
import itertools
def ranges(s):
""" Converts a list of integers into start, end pairs """
for a, b in itertools.groupby(enumerate(s), lambda(x, y): y - x):
b = list(b)
yield b[0][1], b[-1][1]
def intersect(*args):
""" Converts any number of numpy arrays containing start, end pairs
into a set of indexes """
s = set()
for start, end in np.vstack(args):
s = s | set(range(start,end+1))
return s
a = np.array([[5,7],[12, 18],[20,29]])
b = np.array([[2,4],[8,11],[33,35]])
result = np.array(list(ranges(intersect(a,b))))
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7095 次 |
| 最近记录: |