简体   繁体   English

如何将二维 numpy 阵列垂直拆分为新的二维 numpy 阵列?

[英]How to split a 2d numpy array vertically into a new 2d numpy array?

I have this code that essentially splits a two-dimensional numpy array horizontally and makes a new two-dimensional numpy array out of it.我有这段代码,它基本上水平拆分二维 numpy 数组,并从中生成一个新的二维 numpy 数组。

array1 = np.asarray([[1, 2, 3]])
array2 = np.asarray([[4, 5, 6]])
array3 = np.asarray([[7, 8, 9]])

concatenated = np.concatenate((array1, array2, array3), axis=0)

print(concatenated)

column_split = np.hsplit(concatenated, array1.size)

td_array = []

for array in column_split:
    td_array.append(array.flatten())

print(np.asarray(td_array))

Output of my code: output of the code example我的代码的 Output:代码示例的 output

How can I do this with less lines of code?我怎样才能用更少的代码行来做到这一点? I assume it could be very resource intensive, as soon as I apply this example to my larger task.我认为这可能会占用大量资源,只要我将此示例应用于我的更大任务。

I suppose using numpy.hsplit in this case is not necessary and what I am trying to do is covered by the numpy.transpose function.我想在这种情况下使用numpy.hsplit是不必要的,我想要做的是numpy.transpose function 涵盖。

concatenated = np.concatenate((array1, array2, array3), axis=0)
td_array = concatenated.T

Thank you j1-lee for pointing this out.感谢 j1-lee 指出这一点。

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

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