简体   繁体   English

从一维数据整形生成四维数组

[英]Generating a 4-D array from 1-D data reshape

I have a 1-D set of permeability data for an oil reservoir, 我有一个一维油藏的一维渗透率数据集,

It is a 1-D array in the format 6 columns, X rows, it needs to be restructured in the shape of: 它是一维数组,格式为6列X行,需要将其重构为以下形式:

60 x 220 x 85 cells (1.122x106 cells) 60 x 220 x 85单元(1.122x106单元)

If i import the data, and use the re-shape function with z = 1, it fills in the correct format (reading left to right and filling y first with x=1,z=1 then filling y with x=2 etc. 如果我导入数据,并使用z = 1的re-shape函数,它将以正确的格式填充(从左到右读取并先用x = 1,z = 1填充y,然后用x = 2填充y等)。

If i use z=2 it fills [x1,y,z2] after it reaches the top of [x1,y,z1], and then goes back and fills [x2,y,z1] effectively splitting it between each slice, 如果我使用z = 2,它将在到达[x1,y,z1]的顶部后填充[x1,y,z2],然后返回并填充[x2,y,z1],在每个切片之间进行有效分割,

import numpy as np
data = np.loadtxt("spe_phi_sample.prn")
print(data.shape)
data = np.reshape(data, (60,85,2))
print(data.shape)
x,y,z = data.nonzero()
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

ax.scatter(x, y, -z, c=data[x,y,z] ,  zdir='z')

z=1 Data with tracer (correct format) z = 1带有跟踪器的数据(格式正确) z = 1带有跟踪器的数据(正确格式)

z=2 data with tracer (see how it populated wrong slice) z = 2带有跟踪器的数据(请参阅如何填充错误的切片) z = 2带有跟踪器的数据(请参阅如何填充错误的切片)

Is there a way to determine how re-shape populates the data? 有没有一种方法可以确定重新整形填充数据的方式? Is there a more suitable function for creating a 4-d array from a large 1-d array by determining when to slice for each axis, or does this need to be done manually? 是否存在更合适的功能,可以通过确定何时为每个轴切片来从大型1-d数组创建4-d数组,还是需要手动完成? I'm not too experienced in python, thanks 我不太熟悉python,谢谢

When you use numpy.reshape you must have in mind that it will read the elements using the index order and will place the elements into the reshaped array using this index order. 使用numpy.reshape ,必须记住,它将使用索引顺序读取元素,并使用此索引顺序将元素放置到reshaped数组中。 The default index order is given by: the last axis index changing fastest, back to the first axis index changing slowest (where ..., the second axis is y and the first axis is x ). 默认索引顺序如下:最后一个轴索引变化最快,回到第一个轴索引变化最快(其中...,第二个轴为y ,第一个轴为x )。

To better understand what happens, here is a piece of code: 为了更好地理解会发生什么,下面是一段代码:

import numpy as np

# Create array.
size1 = 10
size2 = 60
shape = (size1, size2)
a = np.mod(np.arange(1, size1 * size2 + 1), size2).reshape(shape)
# Reshape array with z = 1.
b = np.reshape(a, (size1, size2, 1))
# Reshape array with z = 2.
c = np.reshape(a, (size1, size2 / 2, 2))
# Reshape array correctly.
d = np.reshape(a, (size1, 2, size2 / 2))
d = np.swapaxes(d, 1, 2)

Here is b (with blue for low value (1) and red for high value (10)): 这是b (蓝色表示低值(1),红色表示高值(10)): 在此处输入图片说明

Here is c : 这是c 在此处输入图片说明

And here is d : 这是d 在此处输入图片说明

As you can see, c has been populated along the z axis first whereas d has been populated along the y axis first, thanks to the numpy.swapaxes function. 如您所见,由于使用了numpy.swapaxes函数, c首先沿z轴填充,而d首先沿y轴填充。

I hope this will help you. 我希望这能帮到您。

'It is a 1-D array in the format 6 columns, X rows,' from loadtxt makes me suspect that it is a structured array. loadtxt 'It is a 1-D array in the format 6 columns, X rows,' ,使我怀疑它是结构化数组。

Show us both data.shape and data.dtype . 向我们展示data.shapedata.dtype

While you are at show us the same values for data after reshape. 重塑后,当您向我们展示相同的data值时。

It might also help to see a few 'rows' of data, eg data[:3,...] . 观察一些“行”数据也可能有所帮助,例如data[:3,...]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM