Python 将2个向量转换为矩阵

Python 将2个向量转换为矩阵,python,pandas,numpy,Python,Pandas,Numpy,我有两个numpy数组,我想把它们转换成一个矩阵 height = np.array([3,2,4,3,3.5,2,5.5,1]) width = np.array([1.5,1,1.5,1,.5,.5,1,1]) 我想用numpy或pandas方法将它们转换为2x8矩阵 提前谢谢。使用np.连接((高度,宽度),轴=0) 您还需要对它们进行重塑就像 height = height.reshape(1,-1) width = width.reshape(1,-1) result = np.co

我有两个numpy数组,我想把它们转换成一个矩阵

height = np.array([3,2,4,3,3.5,2,5.5,1])
width = np.array([1.5,1,1.5,1,.5,.5,1,1])
我想用numpy或pandas方法将它们转换为2x8矩阵

提前谢谢。

使用
np.连接((高度,宽度),轴=0)

您还需要对它们进行
重塑
就像

height = height.reshape(1,-1)
width = width.reshape(1,-1)
result = np.concatenate((height, width), axis=0)
使用
np.连接((高度,宽度),轴=0)

您还需要对它们进行
重塑
就像

height = height.reshape(1,-1)
width = width.reshape(1,-1)
result = np.concatenate((height, width), axis=0)
检查Numpy的方法:

import numpy as np

height = np.array([3,2,4,3,3.5,2,5.5,1])
width = np.array([1.5,1,1.5,1,.5,.5,1,1])

x = np.asmatrix([height, width])
x
结果是

matrix([[ 3. ,  2. ,  4. ,  3. ,  3.5,  2. ,  5.5,  1. ],
        [ 1.5,  1. ,  1.5,  1. ,  0.5,  0.5,  1. ,  1. ]])
检查Numpy的方法:

import numpy as np

height = np.array([3,2,4,3,3.5,2,5.5,1])
width = np.array([1.5,1,1.5,1,.5,.5,1,1])

x = np.asmatrix([height, width])
x
结果是

matrix([[ 3. ,  2. ,  4. ,  3. ,  3.5,  2. ,  5.5,  1. ],
        [ 1.5,  1. ,  1.5,  1. ,  0.5,  0.5,  1. ,  1. ]])
使用
pandas.DataFrame的解决方案
既然您已经提到了熊猫,您可以通过定义一个

当然,选择使用
numpy
pandas
,这取决于您接下来对获得的矩阵所做的操作

使用向量作为行

使用向量作为列

使用
pandas.DataFrame的解决方案
既然您已经提到了熊猫,您可以通过定义一个

当然,选择使用
numpy
pandas
,这取决于您接下来对获得的矩阵所做的操作

使用向量作为行

使用向量作为列

如果您希望通过垂直堆叠两个阵列来创建二维阵列,也可以使用:

height = np.array([3,2,4,3,3.5,2,5.5,1])
width = np.array([1.5,1,1.5,1,.5,.5,1,1])
results = np.vstack((height, width))
如果希望通过垂直堆叠两个阵列来创建二维阵列,也可以使用:

height = np.array([3,2,4,3,3.5,2,5.5,1])
width = np.array([1.5,1,1.5,1,.5,.5,1,1])
results = np.vstack((height, width))

问题与
机器学习无关
-请不要垃圾邮件标签(已删除)您真的想要一个矩阵,还是只想要一个2D数组?问题与
机器学习无关
-请不要垃圾邮件标签(已删除)您真的想要一个矩阵,或者你只是想要一个2D数组?当你投反对票时,你应该解释一下原因,这样我就可以修正答案了。谢谢。我真的认为这是错误的投票。我已经标记了它。干杯当投反对票时,一个人应该评论一下原因,这样我就可以确定答案了。谢谢。我真的认为这是错误的投票。我已经标记了它。干杯