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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17622905/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 08:42:01  来源:igfitidea点击:

Convert row vector to column vector in NumPy

pythonarraysnumpymatrix

提问by siamii

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

The problem is that when I select the first column of matrix1 and put it in vector1, it gets converted to a row vector, so when I try to concatenate with matrix2, I get a dimension error. I could do this.

问题是,当我选择 matrix1 的第一列并将其放入 vector1 时,它会转换为行向量,因此当我尝试与 matrix2 连接时,出现维度错误。我可以做到这一点。

np.hstack((vector1.reshape(matrix2.shape[0],1), matrix2))

But this looks too ugly for me to do every time I have to concatenate a matrix and a vector. Is there a simpler way to do this?

但是每次我必须连接矩阵和向量时,这对我来说都太难看了。有没有更简单的方法来做到这一点?

回答by ali_m

Here are three other options:

以下是其他三个选项:

  1. You can tidy up your solution a bit by allowing the row dimension of the vector to be set implicitly:

    np.hstack((vector1.reshape(-1, 1), matrix2))
    
  2. You can index with np.newaxis(or equivalently, None) to insert a new axis of size 1:

    np.hstack((vector1[:, np.newaxis], matrix2))
    np.hstack((vector1[:, None], matrix2))
    
  3. You can use np.matrix, for which indexing a column with an integer always returns a column vector:

    matrix1 = np.matrix([[1, 2, 3],[4, 5, 6]])
    vector1 = matrix1[:, 0]
    matrix2 = np.matrix([[2, 3], [5, 6]])
    np.hstack((vector1, matrix2))
    
  1. 您可以通过允许隐式设置向量的行维度来稍微整理您的解决方案:

    np.hstack((vector1.reshape(-1, 1), matrix2))
    
  2. 您可以使用np.newaxis(或等效地None)索引以插入大小为 1 的新轴:

    np.hstack((vector1[:, np.newaxis], matrix2))
    np.hstack((vector1[:, None], matrix2))
    
  3. 您可以使用np.matrix, 用整数索引列总是返回一个列向量:

    matrix1 = np.matrix([[1, 2, 3],[4, 5, 6]])
    vector1 = matrix1[:, 0]
    matrix2 = np.matrix([[2, 3], [5, 6]])
    np.hstack((vector1, matrix2))
    

回答by David Z

The easier way is

更简单的方法是

vector1 = matrix1[:,0:1]

For the reason, let me refer you to another answer of mine:

出于这个原因,让我向您推荐我的另一个答案

When you write something like a[4], that's accessing the fifth element of the array, not giving you a view of some section of the original array. So for instance, if a is an array of numbers, then a[4]will be just a number. If ais a two-dimensional array, i.e. effectively an array of arrays, then a[4]would be a one-dimensional array. Basically, the operation of accessing an array element returns something with a dimensionality of one less than the original array.

当你写类似的东西时a[4],就是访问数组的第五个元素,而不是让你看到原始数组的某些部分。因此,例如,如果 a 是一个数字数组,那么a[4]它将只是一个数字。如果a是二维数组,即有效的数组数组,a[4]则将是一维数组。基本上,访问数组元素的操作会返回维数比原始数组少一的东西。