如何使用NumPy的高级索引来创建此内容,我希望实现最快的性能:
array([[ 1, 2, 3, 4, 16, 31],
[ 2, 3, 4, 5, 17, 32],
[ 3, 4, 5, 6, 18, 33],
[ 4, 5, 6, 7, 19, 34],
[ 5, 6, 7, 8, 20, 35],
[ 6, 7, 8, 9, 21, 36],
[ 7, 8, 9, 10, 22, 37],
[ 8, 9, 10, 11, 23, 38],
[ 9, 10, 11, 12, 24, 39],
[10, 11, 12, 13, 25, 40]]
从这里开始:
a = np.arange(0,10)
aa = np.arange(0,50)
y = 1
AA = [(aa[np.array([x+y, 1+x+y, 2+x+y, 3+x+y, 15+x+y, 30+x+y])]) for x, i in enumerate(a)]
我明白了。
[array([ 2, 3, 4, 5, 17, 32]),
array([ 3, 4, 5, 6, 18, 33]),
array([ 4, 5, 6, 7, 19, 34]),
array([ 5, 6, 7, 8, 20, 35]),
array([ 6, 7, 8, 9, 21, 36]),
array([ 7, 8, 9, 10, 22, 37]),
array([ 8, 9, 10, 11, 23, 38]),
array([ 9, 10, 11, 12, 24, 39]),
array([10, 11, 12, 13, 25, 40]),
array([11, 12, 13, 14, 26, 41])]
broadcasting
:np.array([ 1, 2, 3, 4, 16, 31]) + np.arange(10)[:,None]
。 - Divakar