将N维numpy数组拆分为多个一维数组 [英] Splitting an N dimensional numpy array into multiple 1D arrays
问题描述
我有一个仿真模型,该模型集成了一组变量,这些变量的状态由任意维数的numpy数组表示.模拟之后,我现在有了一个数组列表,这些数组的元素表示特定时间点的变量状态.
I have a simulation model that integrates a set of variables whose states are represented by numpy arrays of an arbitrary number of dimensions. After the simulation, I now have a list of arrays whose elements represent the variable state at a particular point in time.
为了输出仿真结果,我想将这些数组拆分为多个1D数组,其中的各个元素在一段时间内对应于状态变量的同一组件.这是一个经过多个时间步长的2D状态变量的示例.
In order to output the simulation results I want to split these arrays into multiple 1D arrays where the elements correspond to the same component of the state variable through time. Here is an example of a 2D state variable over a number of time steps.
import numpy as np
# Arbitrary state that is constant
arr = np.arange(9).reshape((3, 3))
# State variable through 3 time steps
state = [arr.copy() for _ in range(3)]
# Stack the arrays up to 3d. Axis could be rolled here if it makes it easier.
stacked = np.stack(state)
我需要得到的输出是:
[np.array([0, 0, 0]), np.array([1, 1, 1]), np.array([2, 2, 2]), ...]
我尝试做np.split(stacked, sum(stacked.shape[:-1]), axis=...)
(尝试对axis=
进行所有操作),但出现以下错误:ValueError: array split does not result in an equal division
.有没有一种方法可以使用np.split
或也许np.nditer
适用于一般情况?
I've tried doing np.split(stacked, sum(stacked.shape[:-1]), axis=...)
(tried everything for axis=
) but get the following error: ValueError: array split does not result in an equal division
. Is there a way to do this using np.split
or maybe np.nditer
that will work for the general case?
我想这等同于做:
I, J, K = stacked.shape
result = []
for i in range(I):
for j in range(J):
result.append(stacked[i, j, :])
这也是我希望获得的订单.足够容易,但是我希望numpy中有一些可以利用的东西,它会变得更加通用.
Which is also the ordering I'm hoping to get. Easy enough, however I'm hoping there is something in numpy that I can take advantage of for this that will be more general.
推荐答案
如果我将其重塑为9x3数组,则简单的list()
会将其变成3个元素数组的列表:
If I reshape it to a 9x3 array, then a simple list()
will turn it into a list of 3 element arrays:
In [190]: stacked.reshape(-1,3)
Out[190]:
array([[0, 0, 0],
[1, 1, 1],
[2, 2, 2],
[3, 3, 3],
[4, 4, 4],
[5, 5, 5],
[6, 6, 6],
[7, 7, 7],
[8, 8, 8]])
In [191]: list(stacked.reshape(-1,3))
Out[191]:
[array([0, 0, 0]),
array([1, 1, 1]),
array([2, 2, 2]),
array([3, 3, 3]),
array([4, 4, 4]),
array([5, 5, 5]),
array([6, 6, 6]),
array([7, 7, 7]),
array([8, 8, 8])]
np.split(stacked.reshape(-1,3),9)
产生1x3数组的列表.
np.split(stacked.reshape(-1,3),9)
produces a list of 1x3 arrays.
np.split
仅在一个轴上起作用,但是您想在1st 2轴上进行分割-因此需要重塑形状或拉平.
np.split
only works on one axis, but you want to split on the 1st 2 - hence the need for a reshape or ravel.
忘了nditer
.这是在cython中重做代码的垫脚石.它对普通迭代没有帮助-除非在ndindex
中使用时,它可以简化您的i,j
双循环:
And forget about nditer
. That's a stepping stone to reworking code in cython. It does not help with ordinary iteration - except that when used in ndindex
it can streamline your i,j
double loop:
In [196]: [stacked[idx] for idx in np.ndindex(stacked.shape[:2])]
Out[196]:
[array([0, 0, 0]),
array([1, 1, 1]),
array([2, 2, 2]),
array([3, 3, 3]),
array([4, 4, 4]),
array([5, 5, 5]),
array([6, 6, 6]),
array([7, 7, 7]),
array([8, 8, 8])]
=====================
======================
使用不同的state
,只需将其堆叠在不同的轴上
With the different state
, just stack on a different axis
In [302]: state
Out[302]:
[array([[0, 1, 2],
[3, 4, 5],
[6, 7, 8]]), array([[0, 1, 2],
[3, 4, 5],
[6, 7, 8]]), array([[0, 1, 2],
[3, 4, 5],
[6, 7, 8]])]
In [303]: np.stack(state,axis=2).reshape(-1,3)
Out[303]:
array([[0, 0, 0],
[1, 1, 1],
[2, 2, 2],
[3, 3, 3],
[4, 4, 4],
[5, 5, 5],
[6, 6, 6],
[7, 7, 7],
[8, 8, 8]])
stack
类似于np.array
,不同之处在于它可以更好地控制添加尺寸的位置.但是请看一下它的代码.
stack
is rather like np.array
, except it gives more control over where the dimension is added. But do look at it's code.
这篇关于将N维numpy数组拆分为多个一维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!