相关疑难解决方法(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万
查看次数

Numpy 将一维数组打印为一列

我有一个 1d 数组,我想将它打印为一列。

r1 = np.array([54,14,-11,2])
print r1
Run Code Online (Sandbox Code Playgroud)

给我这个:

 [ 54  14 -11   2]
Run Code Online (Sandbox Code Playgroud)

 print r1.shape
Run Code Online (Sandbox Code Playgroud)

给我这个:

(4L,)
Run Code Online (Sandbox Code Playgroud)

有什么我可以插入 np.reshape() 以便

print r1.shape
Run Code Online (Sandbox Code Playgroud)

给我这个?

(,4L)
Run Code Online (Sandbox Code Playgroud)

打印输出看起来像

 54
 14
-11
 2
Run Code Online (Sandbox Code Playgroud)

python printing numpy reshape

5
推荐指数
1
解决办法
4298
查看次数

标签 统计

numpy ×2

python ×2

arrays ×1

matrix ×1

printing ×1

reshape ×1