从熊猫Python和Numpy将3D阵列转换为2D阵列

时间:2019-02-21 11:13:24

标签: python arrays python-3.x pandas numpy

我已经使用pandas和numpy从csv创建了数组。 这是将2D csv转换为3D数组的代码:

>>> 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)

使用的csv是:

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

现在我想将3D阵列恢复为2D阵列格式。
请让我知道我该怎么做。没有任何线索。

1 个答案:

答案 0 :(得分:1)

在每个0th行上切片,直到最后一个块,并与最后一个块堆叠-

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

具有给定样本数据的输出-

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)

或将0th行切片在所有块上,并与最后一块跳过第一行一起堆叠-

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)
相关问题