from given numpy array [1,2,3,4] and window wz=2 (two elements before and two elements after every element) I have to get pairs (central el, el from window). Pairs with unexisting elements could be skipped or substituted by zero. So on this example I have to get this:
[[1., 0.]
[2., 1.]
[3., 2.]
[4., 3.]
[1., 2.]
[2., 3.]
[3., 4.]
[4., 0.]
[1., 0.]
[2., 0.]
[3., 1.]
[4., 2.]
[1., 3.]
[2., 4.]
[3., 0.]
[4., 0.]]
My implementation is extremely unefficient and looks like:
x = np.array([1,2,3,4])
l = x.shape[0]
for i in range(1, m):
init = np.empty((x.shape[0]*2,2))
init[:,0] = np.append(x, x)
init[:l,1] = np.pad(x, (i,0), mode='constant')[:l]
init[-l:,1] = np.pad(x, (0,i), mode='constant')[-l:]
corpus.extend(init)
Could someone help with much more efficient solution? On another simple test data and variants I've implemented I've got:
285 µs ± 19.3 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
379 µs ± 7.68 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)