0

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?

1 Answer 1

2

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]])
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.