1

I have created the array from a csv using pandas and numpy. This is my code that convert 2D csv to 3D array:

>>> import pandas as pd
>>> import numpy as npp
>>> df = pd.read_csv("test.csv")
>>> df_mat = df.values
>>> seq_len = 3
>>> data=[]
>>> for index in range(len(df_mat) - seq_len):
...     data.append(df_mat[index: index + seq_len])
...
>>> data = np.array(data)
>>> data.shape
(4, 3, 9)

The csv is used is:

input1,input2,input3,input4,input5,input6,input7,input8,output
1,2,3,4,5,6,7,8,1
2,3,4,5,6,7,8,9,0
3,4,5,6,7,8,9,10,-1
4,5,6,7,8,9,10,11,-1
5,6,7,8,9,10,11,12,1
6,7,8,9,10,11,12,13,0
7,8,9,10,11,12,13,14,1

Now I want to get the 3D array back to 2D array format.
Kindly, let me know how I can I do that. Not getting any clue.

2
  • 1
    Think that should be - for index in range(len(df_mat) - seq_len + 1): instead to cover all subarrays? Commented Feb 21, 2019 at 11:19
  • The same as previous when it was a row and column. (7, 9) Commented Feb 21, 2019 at 11:19

1 Answer 1

1

Slice on the 0th rows along each each block until the last block and stack with the last one -

np.vstack((data[np.arange(data.shape[0]-1),0],data[-1]))

Output with given sample data -

In [24]: np.vstack((data[np.arange(data.shape[0]-1),0],data[-1]))
Out[24]: 
array([[ 1,  2,  3,  4,  5,  6,  7,  8,  1],
       [ 2,  3,  4,  5,  6,  7,  8,  9,  0],
       [ 3,  4,  5,  6,  7,  8,  9, 10, -1],
       [ 4,  5,  6,  7,  8,  9, 10, 11, -1],
       [ 5,  6,  7,  8,  9, 10, 11, 12,  1],
       [ 6,  7,  8,  9, 10, 11, 12, 13,  0],
       [ 7,  8,  9, 10, 11, 12, 13, 14,  1]], dtype=int64)

Or slice 0th rows across all blocks and stack with the last block skipping the first row -

In [28]: np.vstack((data[np.arange(data.shape[0]),0],data[-1,1:]))
Out[28]: 
array([[ 1,  2,  3,  4,  5,  6,  7,  8,  1],
       [ 2,  3,  4,  5,  6,  7,  8,  9,  0],
       [ 3,  4,  5,  6,  7,  8,  9, 10, -1],
       [ 4,  5,  6,  7,  8,  9, 10, 11, -1],
       [ 5,  6,  7,  8,  9, 10, 11, 12,  1],
       [ 6,  7,  8,  9, 10, 11, 12, 13,  0],
       [ 7,  8,  9, 10, 11, 12, 13, 14,  1]], dtype=int64)
Sign up to request clarification or add additional context in comments.

5 Comments

I am not getting the last row sir. How you are getting it I didn't got that.
@JafferWilson Read my comment?
Thank you for the insights sir.
Sir, can you please help me on these question as well: stackoverflow.com/questions/54789287/… and ai.stackexchange.com/questions/10781/…? From your profile I guess you can help me.
@JafferWilson Sorry mate, don't think I have the expertise on those tags.

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.