如何在numpy中保持向量的行/列方向? [英] How do I maintain row/column orientation of vectors in numpy?

查看:90
本文介绍了如何在numpy中保持向量的行/列方向?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基于Matlab/Octave的背景,我一直在尝试学习numpy.一遍又一遍让我绊倒的一件事是向量与多维数组之间的区别.对于这个问题,我将给出一个具体的问题,但是如果有人还可以解释numpy中的一维数组背后的更一般的图片,我将非常感激,为什么您首先要使用它们,如何为了避免在混合一维和多维数组等时遇到麻烦.无论如何,问题是:

Coming from a background of Matlab/Octave, I have been trying to learn numpy. One thing that has been tripping me up over and over is the distinction between vectors and multi-dimensional arrays. For this question I'll give a specific problem I'm having, but I'd be much obliged if someone could also explain the more general picture behind single-dimensional arrays in numpy, why you would want them in the first place, how to avoid trouble when mixing single and multi-dimensional arrays, etc. Anyway, the question:

我有一个称为X的二维数组:

I have a 2-D array called X:

X = numpy.arange(10).reshape(2,5)

我想获取X的最后一列并将其存储为另一个称为Y的二维数组(即列向量).为此,我唯一能提供的方法是:

and I want to take the last column of X and store it as another 2-D array (ie, a column vector) called Y. The only way I have been able to come with for this is:

Y = numpy.atleast_2d(X[:,4]).T

但是我不喜欢这样做有几个原因:

but I don't like that for a couple of reasons:

  1. 我认为当X [:,4]中隐含方向时,我不必告诉它转置向量.

  1. I don't feel like I should have to tell it to transpose the vector when the orientation should be implied in X[:,4].

使用atleast_2D看起来很麻烦,因为在代码中反复使用这种情况会很麻烦.感觉好像我做错了.

Using atleast_2D just seems so cumbersome to use over and over again in code where this situation would come up a lot. It feels like I'm doing something wrong.

因此,简而言之,还有更好的方法吗?

So, in short, is there a better way?

谢谢.

推荐答案

首先,做您想要做的简单方法:

First, the easy way to do what you want:

Y = X[:,4:]

现在,在尝试使用numpy时未执行此操作的原因与数组在Python中以及实际上在大多数编程语言中的工作方式有关.当您编写类似a[4]的内容时,这将访问数组的第五个元素,而不是让您查看原始数组的某些部分.因此,例如,如果a是数字数组,则a[4]将只是一个数字.如果a是二维数组,即有效的数组数组,则a[4]将是一维数组.基本上,访问数组元素的操作返回的维数比原始数组小一.

Now, the reason numpy wasn't doing this when you were trying it before has to do with how arrays work in Python, and actually in most programming languages. 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 a is 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.

现在,Python包含了一个叫做切片符号"的东西,它用冒号表示,这是访问数组元素的另一种方式.它不返回 element (维数比原始数组小一维的东西),而是返回原始数组的一部分的副本.本质上,a:b表示索引a(包括)到b(排除)的所有元素的列表.可以省略ab或两者都省略,在这种情况下,切片将一直指向数组的相应末端.

Now, Python includes this thing called "slice notation," represented using the colon, which is a different way of accessing array elements. Instead of returning an element (something with a dimensionality of one less than the original array), it returns a copy of a section of the original array. Essentially, a:b represents the list of all the elements at indices a (inclusive) to b (exclusive). Either a or b or both can be omitted, in which case the slice goes all the way to the corresponding end of the array.

这对您的情况意味着编写X[:,4]时,您只有一个片符号和一个常规索引符号.切片符号表示沿第一个维度的所有索引(由于数组有两行,因此分别为0和1),而4表示沿第二个维度的第五个元素.正则索引的每个实例基本上都会将返回的对象的维数减一,因此,由于X是2D数组,并且有一个正则索引,因此得到1D结果. Numpy只是将一维数组显示为行向量.如果想获得与开始时相同的尺寸,那么诀窍就是使用所有切片索引,就像我在本文顶部示例中所做的那样.

What this means for your case is that when you write X[:,4], you have one slice notation and one regular index notation. The slice notation represents all indices along the first dimension (just 0 and 1, since the array has two rows), and the 4 represents the fifth element along the second dimension. Each instance of a regular index basically reduces the dimensionality of the returned object by one, so since X is a 2D array, and there is one regular index, you get a 1D result. Numpy just displays 1D arrays as row vectors. The trick, if you want to get out something of the same dimensions you started with, is then to use all slice indices, as I did in the example at the top of this post.

如果要提取总列数超过5的东西的第五列,则可以使用X[:,4:5].如果要查看3-4行和5-7列,则可以执行X[3:5,5:8].希望你能明白.

If you wanted to extract the fifth column of something that had more than 5 total columns, you could use X[:,4:5]. If you wanted a view of rows 3-4 and columns 5-7, you would do X[3:5,5:8]. Hopefully you get the idea.

这篇关于如何在numpy中保持向量的行/列方向?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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