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)
但是每次我必须连接矩阵和向量时,这对我来说太难看了.有更简单的方法吗?
我有一个 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)