努比重塑"reversal"



我从一个文件中读取了一个4D数组,该文件以2D形式I,j,k,x,y,z给出。输入文件头和形状我使用numpy.reshape将2D数组重塑为3D形式。对此进行更改后,我希望以与读取文件完全相同的顺序/格式写入文件。我不知道如何"反转"numpy.reshape以使其恢复相同的格式。

import numpy as np    
import pandas as pd
from pandas import read_csv 

header = read_csv("Input/Grid1_test.csv", nrows=1,skipinitialspace=True)
print header.head()
imax=header['IMAX'].iloc[0]
jmax=header['JMAX'].iloc[0]
kmax=header['KMAX '].iloc[0]
print(imax,jmax,kmax)
#read grid data with numpy array .. it is slow but probably worth it 
grid_data = np.loadtxt('Input/Grid1_test.csv', delimiter=',', skiprows=3)

size_arr = np.array(grid_data).reshape(kmax, jmax, imax, 6)
#do something
#write the grid back into the file

要"反转"重塑,只需对数组再次调用reshape即可将其重塑为原始维度。

如果您有一个维度为(nm(的数组x,则:

x.reshape(kmax, jmax, imax, 6).reshape(n, m) == x