简体   繁体   English

如何使用特定的索引顺序重塑 numpy 数组?

[英]How to reshape numpy array with specific indexing order?

I am trying to reshape some arrays into a specific order but numpy.reshape is not solving my problem and I don't want to use any loops unless I really have to.我试图将一些数组重塑为特定的顺序,但numpy.reshape没有解决我的问题,除非我真的必须,否则我不想使用任何循环。

Let's take an array a with values:让我们以a带有值的数组a为例:

a = [['x1','x2','x3','y1','y2','y3','z1','z2','z3'],
['x4','x5','x6','y4','y5','y6','z4','z5','z6']]

and np.reshape(a,[-1,18]) returns:np.reshape(a,[-1,18])返回:

array([['x1', 'x2', 'x3', 'y1', 'y2', 'y3', 'z1', 'z2', 'z3', 
     'x4', 'x5','x6', 'y4', 'y5', 'y6', 'z4', 'z5', 'z6']], dtype='<U2')

but is it possible to get a result like this:但是否有可能得到这样的结果:

[['x1', 'x2', 'x3','x4', 'x5','x6', 'y1', 'y2', 'y3','y4', 'y5', 'y6',
 'z1', 'z2', 'z3', 'z4', 'z5', 'z6']]

You need to reshape and transpose the array:您需要重塑转置数组:

import numpy as np

a = np.array([['x1','x2','x3','y1','y2','y3','z1','z2','z3'],
              ['x4','x5','x6','y4','y5','y6','z4','z5','z6']])
b = a.reshape(2, 3, 3).transpose((1, 0, 2)).ravel()
print(b)
# ['x1' 'x2' 'x3' 'x4' 'x5' 'x6' 'y1' 'y2' 'y3' 'y4' 'y5' 'y6' 'z1' 'z2'
#  'z3' 'z4' 'z5' 'z6']

Step by step, first you have your initial array.一步一步,首先你有你的初始数组。

print(a)
# [['x1' 'x2' 'x3' 'y1' 'y2' 'y3' 'z1' 'z2' 'z3']
#  ['x4' 'x5' 'x6' 'y4' 'y5' 'y6' 'z4' 'z5' 'z6']]

Then you reshape it as "two 3x3 matrices":然后将其重塑为“两个 3x3 矩阵”:

print(a.reshape(2, 3, 3))
# [[['x1' 'x2' 'x3']
#   ['y1' 'y2' 'y3']
#   ['z1' 'z2' 'z3']]
#
#  [['x4' 'x5' 'x6']
#   ['y4' 'y5' 'y6']
#   ['z4' 'z5' 'z6']]]

Now if you flattened that, after x3 you would get y1 .现在,如果您将其展平,则在x3之后您将获得y1 You need to reorder the axes so after x3 goes x4 , which means you first want to iterate the columns ( x1 , x2 , x3 ), then jump to the next matrix to iterate the columns in its first rows ( x4 , x5 , x6 ) and then continue to the next row of the first matrix.您需要对轴重新排序,以便在x3变为x4 ,这意味着您首先要迭代列( x1x2x3 ),然后跳转到下一个矩阵以迭代其第一行中的列( x4x5x6 )然后继续到第一个矩阵的下一行。 So the innermost dimensions should be the same (the columns), but you have to swap the first two dimensions so you first change matrix and then rows instead of the other way around:所以最里面的维度应该是相同的(列),但是你必须交换前两个维度,所以你首先改变矩阵,然后改变行而不是相反:

print(a.reshape(2, 3, 3).transpose((1, 0, 2)))
# [[['x1' 'x2' 'x3']
#   ['x4' 'x5' 'x6']]
#
#  [['y1' 'y2' 'y3']
#   ['y4' 'y5' 'y6']]
#
#  [['z1' 'z2' 'z3']
#   ['z4' 'z5' 'z6']]]

Now than can be flattened to get the desired result.现在可以将其展平以获得所需的结果。

print(a.reshape(2, 3, 3).transpose((1, 0, 2)).ravel())
# ['x1' 'x2' 'x3' 'x4' 'x5' 'x6' 'y1' 'y2' 'y3' 'y4' 'y5' 'y6' 'z1' 'z2'
#  'z3' 'z4' 'z5' 'z6']

If the lengths of the x , y and z values are the same, you could use np.array_split and flatten the result with .ravel() :如果xyz值的长度相同,则可以使用np.array_split并使用.ravel()将结果展平:

np.array(np.array_split(a, 3, axis=1)).ravel()

array(['x1', 'x2', 'x3', 'x4', 'x5', 'x6', 'y1', 'y2', 'y3', 'y4', 'y5',
       'y6', 'z1', 'z2', 'z3', 'z4', 'z5', 'z6'], dtype='<U2')

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

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