Numpy 数组 - 使用 reshape 将多列堆叠为一 [英] Numpy array - stack multiple columns into one using reshape
本文介绍了Numpy 数组 - 使用 reshape 将多列堆叠为一的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
对于这样的二维数组:
table = np.array([[11,12,13],[21,22,23],[31,32,33],[41,42,43]])
是否可以在table
上使用np.reshape
得到一个数组single_column
,其中table
垂直堆叠?这可以通过拆分 table
并结合 vstack
来实现.
Is it possible to use np.reshape
on table
to get an array single_column
where each column of table
is stacked vertically? This can be accomplished by splitting table
and combining with vstack
.
single_column = np.vstack(np.hsplit(table , table .shape[1]))
Reshape 可以将所有行合并为一行,我想知道它是否也可以合并列以使代码更简洁,可能更快.
Reshape can combine all the rows into a single row, I'm wondering if it can combine the columns as well to make the code cleaner and possibly faster.
single_row = table.reshape(-1)
推荐答案
可以先转置,再整形:
table.T.reshape(-1, 1)
array([[11],
[21],
[31],
[41],
[12],
[22],
[32],
[42],
[13],
[23],
[33],
[43]])
这篇关于Numpy 数组 - 使用 reshape 将多列堆叠为一的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文