为了账号安全,请及时绑定邮箱和手机立即绑定

numpy 当切片的每一行具有不同的列数时,如何从二维数组中获取元素?

numpy 当切片的每一行具有不同的列数时,如何从二维数组中获取元素?

catspeake 2021-07-16 18:15:44
当切片的每一行具有不同的列数时,如何从二维数组中获取元素?buffer = np.zeros((32, 32, 3), 'u1') # this is our data buffer 2d.buffer[2:5, (2:4, 3:7, 0:11)] # does not work.# vertical interval: 2..5; horizontal intervals: 1..3, 4..9, 7..10 multi_intervals = ((2, 5), ((1, 3), (4, 9), (7, 10)))# our very slowerest function.def gen_xy_indices(y_interval, x_multi_intervals):    x_multi_ranges = list(map(lambda x: np.arange(*x),x_multi_intervals))    y_range = np.arange(*y_interval)    y_indices = np.repeat(y_range, list(map(len, x_multi_ranges)))    x_indices = np.concatenate(x_multi_ranges)    return x_indices, y_indicesix, iy = gen_xy_indices(*multi_intervals)buffer[iy, ix].shape == (10, 3) # yeah work but slow.# IS THERE A FASTER WAY TO DO THIS?! (in python with numpy)
查看完整描述

2 回答

?
千巷猫影

TA贡献1829条经验 获得超7个赞

您可以使用np.repeat和np.concatenate。


>>> import numpy as np

>>> 

>>> class By_Row:

...     def __getitem__(self, idx):

...         y, *x = (np.arange(i.start, i.stop, i.step) for i in idx)

...         return y.repeat(np.fromiter((i.size for i in x), int, y.size)), np.concatenate(x)

... 

>>> 

>>> b_ = By_Row()

>>> 

>>> A = sum(np.ogrid[:600:100, :12])

>>> A

array([[  0,   1,   2,   3,   4,   5,   6,   7,   8,   9,  10,  11],

       [100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111],

       [200, 201, 202, 203, 204, 205, 206, 207, 208, 209, 210, 211],

       [300, 301, 302, 303, 304, 305, 306, 307, 308, 309, 310, 311],

       [400, 401, 402, 403, 404, 405, 406, 407, 408, 409, 410, 411],

       [500, 501, 502, 503, 504, 505, 506, 507, 508, 509, 510, 511]])

>>> A[b_[2:5, 2:4, 3:7, 0:11]]

array([202, 203, 303, 304, 305, 306, 400, 401, 402, 403, 404, 405, 406,

       407, 408, 409, 410])


查看完整回答
反对 回复 2021-07-27
?
Cats萌萌

TA贡献1805条经验 获得超9个赞

这是您可以做到的一种方法:


x = range(2,5)

y = range(17)

divs = [(2,4), (3,7), (12,17)]

y_vals = []

x_vals = []

for d, div in enumerate(divs):

    y_grp = y[div[0]:div[1]]

    y_vals += y_grp

    x_vals += [x[d]]*len(y_grp)


print(x_vals)

print(y_vals)


> [2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4]

> [2, 3, 3, 4, 5, 6, 12, 13, 14, 15, 16]


查看完整回答
反对 回复 2021-07-27
  • 2 回答
  • 0 关注
  • 324 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信