![](/img/trans.png)
[英]How to add several vectors to numpy structered array and call matrix later from fieldname?
[英]What is this called: creating numpy matrix from several vectors
我有4个形状为(10,)的独立numpy向量,我想将它们连接在一起以形成矩阵(10,4)。
这个转换叫做什么?
它称为stack
。
> import numpy as np
> a = np.arange(10)
> b = np.stack((a, a, a, a), axis=1)
> np.shape(b)
(10, 4)
> b
array([[0, 0, 0, 0],
[1, 1, 1, 1],
[2, 2, 2, 2],
[3, 3, 3, 3],
[4, 4, 4, 4],
[5, 5, 5, 5],
[6, 6, 6, 6],
[7, 7, 7, 7],
[8, 8, 8, 8],
[9, 9, 9, 9]])
您也可以(在这种情况下)使用例如np.array([a, a, a, a]).T
。
您也可以从集合中创建一个新的数组并转置结果。
np.random.seed(0)
a1 = np.random.randint(1, 10, 10)
a2 = np.random.randint(1, 10, 10)
a3 = np.random.randint(1, 10, 10)
a4 = np.random.randint(1, 10, 10)
>>> np.array([a1, a2, a3, a4]).T
array([[6, 7, 9, 2],
[1, 9, 5, 4],
[4, 9, 4, 4],
[4, 2, 1, 4],
[8, 7, 4, 8],
[4, 8, 6, 1],
[6, 8, 1, 2],
[3, 9, 3, 1],
[5, 2, 4, 5],
[8, 6, 9, 8]])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.