相关疑难解决方法(0)

在NumPy中将行向量转换为列向量

import numpy as np

matrix1 = np.array([[1,2,3],[4,5,6]])
vector1 = matrix1[:,0] # This should have shape (2,1) but actually has (2,)
matrix2 = np.array([[2,3],[5,6]])
np.hstack((vector1, matrix2))

ValueError: all the input arrays must have same number of dimensions
Run Code Online (Sandbox Code Playgroud)

问题是当我选择matrix1的第一列并将其放在vector1中时,它会转换为行向量,所以当我尝试与matrix2连接时,我得到一个维度错误.我能做到这一点.

np.hstack((vector1.reshape(matrix2.shape[0],1), matrix2))
Run Code Online (Sandbox Code Playgroud)

但是每次我必须连接矩阵和向量时,这对我来说太难看了.有更简单的方法吗?

python arrays numpy matrix

21
推荐指数
2
解决办法
5万
查看次数

标签 统计

arrays ×1

matrix ×1

numpy ×1

python ×1