如果我有一个数组x
,一个起始索引数组s
和一个距离d
,我该如何创建一个数组y
,s.t。 y[i] == x[s[i]:s[i]+d]
?
答案 0 :(得分:1)
您可以创建滑动d
- 窗口的纵向视图到x
,然后使用高级索引:
>>> x = np.random.randint(0, 10, (20,))
>>> d = 4
>>> s = np.random.randint(0, 17, (10,))
>>>
>>> x
array([2, 4, 5, 6, 6, 4, 8, 2, 6, 6, 0, 2, 8, 3, 5, 1, 6, 2, 9, 8])
>>> s
array([16, 4, 6, 0, 16, 1, 14, 4, 6, 6])
>>>
>>> assert x.ndim == 1
>>> assert x.flags.contiguous
>>>
>>> xx = np.lib.stride_tricks.as_strided(x, (x.size-d+1, d), 2*x.strides)
>>> y = xx[s]
>>> y
array([[6, 2, 9, 8],
[6, 4, 8, 2],
[8, 2, 6, 6],
[2, 4, 5, 6],
[6, 2, 9, 8],
[4, 5, 6, 6],
[5, 1, 6, 2],
[6, 4, 8, 2],
[8, 2, 6, 6],
[8, 2, 6, 6]])
答案 1 :(得分:0)
不确定这是否是最佳方式,但您可以使用元组索引:
select CAST(timestamp AS DATE) as date, execution_id, min(timestamp) as begin_timestamp, max(timestamp) as end_timestamp, new_block_id,
sum(case when Type = 'P' then 1 else 0 end) as P_count,
sum(case when Type = 'D' then 1 else 0 end) as D_count
from (
select *,
max(block_id) over (partition by EmpID order by timestamp ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) new_block_id from myTable
) d
group by EmpID, new_block_id, CAST(timestamp AS DATE)
order by EmpID, new_block_id, CAST(timestamp AS DATE)