简体   繁体   English

numpy.array([])和numpy.array([[]])有什么区别?

[英]What is the difference between numpy.array([]) and numpy.array([[]])?

Why can't I get the transpose when of alpha but I can get it for beta? 为什么我不能在alpha时获得转置但是我可以获得beta版? What do the additional [] do? 额外的[]做什么?

alpha = np.array([1,2,3,4])
alpha.shape
alpha.T.shape

beta = np.array([[1,2,3,4]])
beta.shape
beta.T.shape

From the documention ( link ): 从文档( 链接 ):

Transposing a 1-D array returns an unchanged view of the original array. 转置1-D数组会返回原始数组的未更改视图。

The array [1,2,3,4] is 1-D while the array [[1,2,3,4]] is a 1x4 2-D array. 阵列[1,2,3,4]是1-D,而阵列[[1,2,3,4]]是1×4 2-D阵列。

The second pair of bracket indicates that it is a 2D array, so with such and array the transposed array is different from the first array (since the transpose switches the 2 dimensions). 第二对括号表示它是一个2D数组,因此对于这样的数组,转置数组与第一个数组不同(因为转置切换了2个维度)。 However if the array is only 1D the transpose doesn't change anything and the resulting array is equal to the starting one. 但是,如果数组只有1D,则转置不会改变任何内容,结果数组等于起始数组。

alpha is a 1D array, the transpose is itself. alpha是一维数组,转置本身。

beta is a 2D array, so you can transform (1,n) to (n,1) . beta是一个2D数组,因此您可以将(1,n)(n,1)

To do the same with alpha , you need to add a dimension, you don't need to transpose it: 要对alpha执行相同操作,您需要添加维度,不需要转置它:

alpha[:, None]

alpha is a 1D array with shape (4,). alpha是具有形状(4,)的一维数组。 The transpose is just alpha again, ie alpha == alpha.T . 转置只是alpha ,即alpha == alpha.T

beta is a 2D array with shape (1,4). beta是具有形状(1,4)的2D阵列。 It's a single row, but it has two dimensions. 它是一行,但它有两个维度。 Its transpose looks like a single column with shape (4,1). 它的转置看起来像一个有形状的柱子(4,1)。

When I arrived at the programming language world, having come from the "math side of the business" this also seemed strange to me. 当我到达编程语言世界时,来自“业务的数学方面”,这对我来说似乎也很奇怪。 After giving some thought to it I realized that from a programming perspective they are different. 在考虑了一下后,我意识到从编程的角度来看,它们是不同的。 Have a look at the following list: 看看下面的列表:

a = [1,2,3,4,5]

This is a 1D structure. 这是一维结构。 This is so, because to get back the values 1,2,3,4 and 5 you just need to assign one address value. 这是如此,因为要获取值1,2,3,4和5,您只需要分配一个地址值。 3 would be returned if you issued the command a[2] for instance. 如果您发出了命令a [2],则会返回3。

Now take a look at this list: 现在看看这个列表:

b = [[ 1,  2,  3,  4,  5],
     [11, 22, 33, 44, 55]]

To get back the 11 for instance you would need two positional numbers, 1 because 11 is located in the 2nd list and 0 because in the second list it is located in the first position. 例如,要获得11,你需要两个位置编号,1因为11位于第二个列表中而0是因为在第二个列表中它位于第一个位置。 In other words b[1,0] gives back to you 11. 换句话说,b [1,0]给你回报11。

Now comes the trick part. 现在来了诀窍部分。 Look at this third list: 看看第三个清单:

c = [ [ 100, 200, 300, 400, 500] ]

If you look carefully each number requires 2 positional numbers to be taken back from the list. 如果仔细观察,每个数字需要从列表中取回2个位置编号。 300 for instance requires 0 because it is located in the first (and only) list and 2 because it is the third element of the first list. 300例如需要0,因为它位于第一个(也是唯一的)列表中,因为它是第一个列表的第三个元素。 c[0,2] gets you back 300. c [0,2]让你回到300。

This list can be transposed because it has two dimensions and the transposition operation is something that switches the positional arguments. 此列表可以转置,因为它有两个维度,转置操作是切换位置参数的东西。 So cT would give you back a list whose shape would be [5,1], since c has a [1,5] shape. 所以cT会给你一个形状为[5,1]的列表,因为c的形状为[1,5]。

Get back to list a. 回到列表a。 There you have a list with only one positional number. 那里有一个只有一个位置编号的列表。 That list has a shape of [5] only, so there´s no second positional argument to the transposition operation to work with. 该列表的形状仅为[5],因此没有第二个位置参数可供使用的转置操作。 Therefore it remains [5] and if you try aT you get back a. 因此它仍然[5],如果你尝试aT,你会得到一个。

Got it? 得到它了?

Best regards, 最好的祝福,

Gustavo, 古斯塔沃,

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM