Numpy 重塑 "reversal"
Numpy reshape "reversal"
我从以二维形式 i、j、k、x、y、z 给出的文件中读取了一个 4D 数组。
Input file header and shape
我使用 numpy.reshape 将 2D 数组重塑为 3-D 形式。对此进行更改后,我希望以与阅读时完全相同的顺序/格式编写文件。
我不明白如何 "reverse" 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
要 "reverse" 重塑,您只需再次调用 reshape
数组即可将其重塑为原始尺寸。
如果您有一个维度为 (n
、m
) 的数组 x
,则:
x.reshape(kmax, jmax, imax, 6).reshape(n, m) == x
我从以二维形式 i、j、k、x、y、z 给出的文件中读取了一个 4D 数组。 Input file header and shape 我使用 numpy.reshape 将 2D 数组重塑为 3-D 形式。对此进行更改后,我希望以与阅读时完全相同的顺序/格式编写文件。 我不明白如何 "reverse" 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
要 "reverse" 重塑,您只需再次调用 reshape
数组即可将其重塑为原始尺寸。
如果您有一个维度为 (n
、m
) 的数组 x
,则:
x.reshape(kmax, jmax, imax, 6).reshape(n, m) == x