在Python中创建垂直的NumPy数组 [英] Create vertical NumPy arrays in Python
本文介绍了在Python中创建垂直的NumPy数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在Python中使用NumPy处理数组.这是我用来创建垂直数组的方式:
I'm using NumPy in Python to work with arrays. This is the way I'm using to create a vertical array:
import numpy as np
a = np.array([[1],[2],[3]])
是否有一种简单且直接的方法来创建垂直数组?
Is there a simple and more direct way to create vertical arrays?
推荐答案
You can use reshape
or vstack
:
>>> a=np.arange(1,4)
>>> a
array([1, 2, 3])
>>> a.reshape(3,1)
array([[1],
[2],
[3]])
>>> np.vstack(a)
array([[1],
[2],
[3]])
此外,您可以使用 广播 为了重塑您的数组:
Also, you can use broadcasting in order to reshape your array:
In [32]: a = np.arange(10)
In [33]: a
Out[33]: array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
In [34]: a[:,None]
Out[34]:
array([[0],
[1],
[2],
[3],
[4],
[5],
[6],
[7],
[8],
[9]])
这篇关于在Python中创建垂直的NumPy数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文