在Python中从矩阵中选择列向量 [英] Selecting a column vector from a matrix in Python

查看:65
本文介绍了在Python中从矩阵中选择列向量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Python/numpy的矩阵中为列向量建立索引,并将其作为列向量而不是一维数组返回.

I would like to index a column vector in a matrix in Python/numpy and have it returned as a column vector and not a 1D array.

x = np.array([[1,2],[3,4]])
x[:,1]
>array([2, 4])

给予

np.transpose(x[:,1])

不是解决方案.根据numpy.transpose文档,这将返回行向量(一维数组).

is not a solution. Following the numpy.transpose documentation, this will return a row vector (1-D array).

推荐答案

少量选项-

x[:,[1]]
x[:,None,1]
x[:,1,None]
x[:,1][:,None]
x[:,1].reshape(-1,1)
x[None,:,1].T

这篇关于在Python中从矩阵中选择列向量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆