基于列中数据的 Numpy 2D 到 3D 数组
Numpy 2D to 3D array based on data in a column
假设我的数据结构如下所示:
[[1, 3, 4, 6],
[1, 4, 8, 2],
[1, 3, 2, 9],
[2, 2, 4, 8],
[2, 4, 9, 1],
[2, 2, 9, 3]]
第一列表示第三维,因此我想将其转换为以下三维数组:
[[[3, 4, 6],
[4, 8, 2],
[3, 2, 9]],
[[2, 4, 8],
[4, 9, 1],
[2, 9, 3]]]
是否有内置的 numpy 函数来执行此操作?
您可以尝试以下代码:
import numpy as np
array = np.array([[1, 3, 4, 6],
[1, 4, 8, 2],
[1, 3, 2, 9],
[2, 2, 4, 8],
[2, 4, 9, 1],
[2, 2, 9, 3]])
array = np.delete(array, 0, 1)
array.reshape(2,3,-1)
输出
array([[[3, 4, 6],
[4, 8, 2],
[3, 2, 9]],
[[2, 4, 8],
[4, 9, 1],
[2, 9, 3]]])
但是,当您知道数组的形状时可以使用此代码。但是如果你确定数组中的列数是 3 的倍数,你可以简单地使用下面的代码以所需的格式显示数组。
array.reshape(array.shape[0]//3,3,-3)
使用带有重塑函数的 numpy 数组切片。
import numpy as np
arr = [[1, 3, 4, 6],
[1, 4, 8, 2],
[1, 3, 2, 9],
[2, 2, 4, 8],
[2, 4, 9, 1],
[2, 2, 9, 3]]
# convert the list to numpy array
arr = np.array(arr)
# remove first column from numpy array
arr = arr[:,1:]
# reshape the remaining array to desired shape
arr = arr.reshape(len(arr)//3,3,-1)
print(arr)
输出:
[[[3 4 6]
[4 8 2]
[3 2 9]]
[[2 4 8]
[4 9 1]
[2 9 3]]]
您列出了一个非 numpy
数组。我不确定您是否只是建议 numpy
作为获得非 numpy
结果的一种方式,或者您实际上是在寻找 numpy
数组作为结果。如果你实际上不需要 numpy
,你可以这样做:
arr = [[1, 3, 4, 6],
[1, 4, 8, 2],
[1, 3, 2, 9],
[2, 2, 4, 8],
[2, 4, 9, 1],
[2, 2, 9, 3]]
# Length of the 3rd and 2nd dimension.
nz = arr[-1][0] + (arr[0][0]==0)
ny = int(len(arr)/nz)
res = [[arr[ny*z_idx+y_idx][1:] for y_idx in range(ny)] for z_idx in range(nz)]
输出:
[[[3, 4, 6], [4, 8, 2], [3, 2, 9]], [[2, 4, 8], [4, 9, 1], [2, 9, 3]]]
请注意,nz
的计算考虑到数组中的第 3 维索引是基于 0 的(默认情况下 python)或基于 1 的(如您所示在你的例子中)。
假设我的数据结构如下所示:
[[1, 3, 4, 6],
[1, 4, 8, 2],
[1, 3, 2, 9],
[2, 2, 4, 8],
[2, 4, 9, 1],
[2, 2, 9, 3]]
第一列表示第三维,因此我想将其转换为以下三维数组:
[[[3, 4, 6],
[4, 8, 2],
[3, 2, 9]],
[[2, 4, 8],
[4, 9, 1],
[2, 9, 3]]]
是否有内置的 numpy 函数来执行此操作?
您可以尝试以下代码:
import numpy as np
array = np.array([[1, 3, 4, 6],
[1, 4, 8, 2],
[1, 3, 2, 9],
[2, 2, 4, 8],
[2, 4, 9, 1],
[2, 2, 9, 3]])
array = np.delete(array, 0, 1)
array.reshape(2,3,-1)
输出
array([[[3, 4, 6],
[4, 8, 2],
[3, 2, 9]],
[[2, 4, 8],
[4, 9, 1],
[2, 9, 3]]])
但是,当您知道数组的形状时可以使用此代码。但是如果你确定数组中的列数是 3 的倍数,你可以简单地使用下面的代码以所需的格式显示数组。
array.reshape(array.shape[0]//3,3,-3)
使用带有重塑函数的 numpy 数组切片。
import numpy as np
arr = [[1, 3, 4, 6],
[1, 4, 8, 2],
[1, 3, 2, 9],
[2, 2, 4, 8],
[2, 4, 9, 1],
[2, 2, 9, 3]]
# convert the list to numpy array
arr = np.array(arr)
# remove first column from numpy array
arr = arr[:,1:]
# reshape the remaining array to desired shape
arr = arr.reshape(len(arr)//3,3,-1)
print(arr)
输出:
[[[3 4 6]
[4 8 2]
[3 2 9]]
[[2 4 8]
[4 9 1]
[2 9 3]]]
您列出了一个非 numpy
数组。我不确定您是否只是建议 numpy
作为获得非 numpy
结果的一种方式,或者您实际上是在寻找 numpy
数组作为结果。如果你实际上不需要 numpy
,你可以这样做:
arr = [[1, 3, 4, 6],
[1, 4, 8, 2],
[1, 3, 2, 9],
[2, 2, 4, 8],
[2, 4, 9, 1],
[2, 2, 9, 3]]
# Length of the 3rd and 2nd dimension.
nz = arr[-1][0] + (arr[0][0]==0)
ny = int(len(arr)/nz)
res = [[arr[ny*z_idx+y_idx][1:] for y_idx in range(ny)] for z_idx in range(nz)]
输出:
[[[3, 4, 6], [4, 8, 2], [3, 2, 9]], [[2, 4, 8], [4, 9, 1], [2, 9, 3]]]
请注意,nz
的计算考虑到数组中的第 3 维索引是基于 0 的(默认情况下 python)或基于 1 的(如您所示在你的例子中)。