I would like to convert an n-dimensional numpy array such as this one:
我想转换一个n维numpy数组,如下这个:
[ [ a, b, c],
[ d, e, f] ]
to a 2-dimensional array of axis_0_index
, axis_1_index
, cell_value
.
到axis_0_index,axis_1_index,cell_value的二维数组。
[ [ 0, 0, a],
[ 0, 1, b],
[ 0, 2, c],
[ 1, 0, d],
[ 1, 1, e],
[ 1, 2, f] ]
Is this possible to do easily in NumPy?
这可以在NumPy中轻松完成吗?
2 个解决方案
#1
1
You can use (abuse?) np.where
to get all the indices of the array, using an array of ones of the same shape as condition, then stack those indices with the (flattened) array and finally transpose.
您可以使用(滥用?)np.where来获取数组的所有索引,使用与条件相同形状的数组,然后将这些索引与(flattened)数组堆叠起来,最后进行转置。
>>> A = np.array([ [ 'a', 'b', 'c'], [ 'd', 'e', 'f'] ])
>>> ones = np.ones(A.shape)
>>> np.vstack(np.where(ones) + (A.ravel(),)).transpose()
array([['0', '0', 'a'],
['0', '1', 'b'],
['0', '2', 'c'],
['1', '0', 'd'],
['1', '1', 'e'],
['1', '2', 'f']],
dtype='|S1')
After some more searching, its probably cleaner to use np.indices
:
经过一些搜索后,使用np.indices可能更干净:
>>> X, Y = np.indices(A.shape)
>>> np.vstack((X.ravel(), Y.ravel(), A.ravel())).T
or
>>> np.vstack((X, Y, A)).reshape(3,A.size).T
The result, in both cases, is the same as above.
在两种情况下,结果与上述相同。
I did some timing analysis using IPython's %timeit
. Curiously, my first solution with where
seems to be fastest, at least for this very small test array:
我使用IPython的%timeit做了一些时序分析。奇怪的是,我的第一个解决方案似乎是最快的,至少对于这个非常小的测试阵列:
>>> %timeit f1() # using ones and np.where
10000 loops, best of 3: 72.3 us per loop
>>> %timeit f2() # using np.indices and ravel
10000 loops, best of 3: 125 us per loop
>>> %timeit f3() # using np.indices and reshape
10000 loops, best of 3: 110 us per loop
>>> %timeit g() # using meshgrid
10000 loops, best of 3: 134 us per loop
#2
1
You can use np.meshgrid
as shown in the sample run below -
您可以使用np.meshgrid,如下面的示例运行所示 -
In [19]: A
Out[19]:
array([[19, 80, 63],
[24, 54, 44]])
In [20]: m,n = A.shape
In [21]: R,C = np.meshgrid(np.arange(m),np.arange(n))
In [22]: np.column_stack((R.ravel('F'),C.ravel('F'),A.ravel()))
Out[22]:
array([[ 0, 0, 19],
[ 0, 1, 80],
[ 0, 2, 63],
[ 1, 0, 24],
[ 1, 1, 54],
[ 1, 2, 44]])
#1
1
You can use (abuse?) np.where
to get all the indices of the array, using an array of ones of the same shape as condition, then stack those indices with the (flattened) array and finally transpose.
您可以使用(滥用?)np.where来获取数组的所有索引,使用与条件相同形状的数组,然后将这些索引与(flattened)数组堆叠起来,最后进行转置。
>>> A = np.array([ [ 'a', 'b', 'c'], [ 'd', 'e', 'f'] ])
>>> ones = np.ones(A.shape)
>>> np.vstack(np.where(ones) + (A.ravel(),)).transpose()
array([['0', '0', 'a'],
['0', '1', 'b'],
['0', '2', 'c'],
['1', '0', 'd'],
['1', '1', 'e'],
['1', '2', 'f']],
dtype='|S1')
After some more searching, its probably cleaner to use np.indices
:
经过一些搜索后,使用np.indices可能更干净:
>>> X, Y = np.indices(A.shape)
>>> np.vstack((X.ravel(), Y.ravel(), A.ravel())).T
or
>>> np.vstack((X, Y, A)).reshape(3,A.size).T
The result, in both cases, is the same as above.
在两种情况下,结果与上述相同。
I did some timing analysis using IPython's %timeit
. Curiously, my first solution with where
seems to be fastest, at least for this very small test array:
我使用IPython的%timeit做了一些时序分析。奇怪的是,我的第一个解决方案似乎是最快的,至少对于这个非常小的测试阵列:
>>> %timeit f1() # using ones and np.where
10000 loops, best of 3: 72.3 us per loop
>>> %timeit f2() # using np.indices and ravel
10000 loops, best of 3: 125 us per loop
>>> %timeit f3() # using np.indices and reshape
10000 loops, best of 3: 110 us per loop
>>> %timeit g() # using meshgrid
10000 loops, best of 3: 134 us per loop
#2
1
You can use np.meshgrid
as shown in the sample run below -
您可以使用np.meshgrid,如下面的示例运行所示 -
In [19]: A
Out[19]:
array([[19, 80, 63],
[24, 54, 44]])
In [20]: m,n = A.shape
In [21]: R,C = np.meshgrid(np.arange(m),np.arange(n))
In [22]: np.column_stack((R.ravel('F'),C.ravel('F'),A.ravel()))
Out[22]:
array([[ 0, 0, 19],
[ 0, 1, 80],
[ 0, 2, 63],
[ 1, 0, 24],
[ 1, 1, 54],
[ 1, 2, 44]])