Numpy arange an array with last number

Question:

I am new to using numpy and I want to do a simple task but I don’t know how to do this.

this is my numpy array

values_points = np.arange(1,15)

But I want to reshape it like this:

[1,2,3,4,5] 
[2,3,4,5,6] 
[3,4,5,6,7] 
[4,5,6,7,8] 
[5,6,7,8,9]   
[6,7,8,9,10]
[7,8,9,10,11]
[8,9,10,11,12]
[9,10,11,12,13]
[10,11,12,13,14]

So it will take the second number from the previous Square brackets and the next 4 number to it.
How could I do that?

Asked By: tiberhockey

||

Answers:

If I understand correctly, a stride trick is possible here:

>>> values_points = np.arange(1,15)
>>> from numpy.lib.stride_tricks import as_strided
>>> as_strided(values_points, shape=(10, 5), strides=values_points.strides*2)
array([[ 1,  2,  3,  4,  5],
       [ 2,  3,  4,  5,  6],
       [ 3,  4,  5,  6,  7],
       [ 4,  5,  6,  7,  8],
       [ 5,  6,  7,  8,  9],
       [ 6,  7,  8,  9, 10],
       [ 7,  8,  9, 10, 11],
       [ 8,  9, 10, 11, 12],
       [ 9, 10, 11, 12, 13],
       [10, 11, 12, 13, 14]])
Answered By: wim
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.