Behdad
Behdad

Reputation: 1629

Reshaping dataset in Python

I have a dataset :

[[0.08007146 1.         0.96428571 0.02050692 0.        ]
 [0.01779764 0.85714286 0.85714286 0.0176427  0.        ]
 [0.02669778 0.64285714 0.5        0.03108454 1.        ]
 ...
 [0.01552716 0.45454545 1.         0.01019869 0.        ]
 [0.00931678 1.         0.25       0.0136772  1.        ]
 [0.03105702 0.83333333 1.         0.02045807 0.33333333]]

Whenever I reshaped it to (5,5) , I got this which is expected:

[[[0.08007146 1.         0.96428571 0.02050692 0.        ]
  [0.01779764 0.85714286 0.85714286 0.0176427  0.        ]
  [0.02669778 0.64285714 0.5        0.03108454 1.        ]
  [0.02966641 0.83333333 1.         0.01141099 0.33333333]
  [0.00889919 0.5        1.         0.00837062 1.        ]]

 [[0.01483161 0.83333333 1.         0.00847276 0.33333333]
  [0.0148321  0.83333333 0.83333333 0.01239681 0.33333333]
  [0.00593259 0.66666667 1.         0.00833658 0.33333333]
  [0.00296632 1.         0.16666667 0.00900119 0.        ]
  [0.04449483 1.         0.9375     0.00967617 1.        ]]

 [[0.04450035 0.9375     1.         0.01646444 0.33333333]
  [0.04449446 0.88235294 0.9375     0.01299926 1.        ]
  [0.05042079 0.73913043 0.94444444 0.02087993 1.        ]
  [0.10085577 0.97142857 1.         0.02407424 1.        ]
  [0.00296554 1.         1.         0.00803905 1.        ]]

How can I reshaped it to having second element of first sequence in second sequence? I mean something like this:

[[[0,1,2,3,4][1,2,3,4,5][2,3,4,5,6][3,4,5,6,7][4,5,6,7,8]] ... ]]]

I tried to reshape/slice in a loop by this:

x = np.ndarray

for i in range(0,len(X)):
    a = X[i:i+5]
    x = np.concatenate((a,x))

But I got errors.

Upvotes: 0

Views: 927

Answers (1)

Daniel F
Daniel F

Reputation: 14399

Using my as_strided recipe window_nd from here:

input = np.random.rand(15, 5)
current_output = input.reshape(-1, 5, 5)  #I think?
expected_output = window_nd(input, 5, steps = 1, axis = 0)

steps and axis parameters aren't technically needed in this case but are included for clarity.

Upvotes: 2

Related Questions