简体   繁体   English

如何将(5,)numpy数组转换为(5,1)?

[英]How to convert (5,) numpy array to (5,1)?

How to convert (5,) numpy array to (5,1)? 如何将(5,)numpy数组转换为(5,1)?

And how to convert backwards from (5,1) to (5,)? 以及如何从(5,1)向后转换为(5,)?

What is the purpose of (5,) array, why is one dimension omitted? (5,)数组的目的是什么,为什么省略一个维度? I mean why we didn't always use (5,1) form? 我的意思是为什么我们不总是使用(5,1)形式?

Does this happen only with 1D and 2D arrays or does it happen across 3D arrays, like can (2,3,) array exist? 这是仅在1D和2D阵列中发生还是在3D阵列中发生,如can(2,3,)数组是否存在?

UPDATE: 更新:

I managed to convert from (5,) to (5,1) by 我设法将(5,)转换为(5,1)

a= np.reshape(a, (a.shape[0], 1)) 

but suggested variant looks simpler: 但建议的变体看起来更简单:

a = a[:, None] or a = a[:, np.newaxis]

To convert from (5,1) to (5,) np.ravel can be used 要从(5,1)转换为(5,),可以使用np.ravel

a= np.ravel(a)

You can add a new axis to an array a by doing a = a[:, None] or a = a[:, np.newaxis] 您可以通过执行a = a[:, None]a = a[:, np.newaxis]将新轴添加到数组a

As far as "one dimension omitted", I don't really understand your question, because it has no end : the array could be (5, 1, 1) , etc. 至于“一维省略”,我真的不明白你的问题,因为它没有结束:数组可能是(5, 1, 1)等。

A numpy array with shape (5,) is a 1 dimensional array while one with shape (5,1) is a 2 dimensional array. 具有形状(5,) numpy阵列是1维阵列,而具有形状(5,1)的numpy阵列是2维阵列。 The difference is subtle, but can alter some computations in a major way. 差异很微妙,但可以以一种主要方式改变一些计算。 One has to be specially careful since these changes can be bull-dozes over by operations which flatten all dimensions, like np.mean or np.sum . 人们必须特别小心,因为这些变化可能会使所有维度变平,例如np.meannp.sum

In addition to @m-massias's answer, consider the following as an example: 除了@ m-massias的答案之外,请考虑以下示例:

17:00:25 [2]: import numpy as np
17:00:31 [3]: a = np.array([1,2])
17:00:34 [4]: b = np.array([[1,2], [3,4]])
17:00:45 [6]: b * a
      Out[6]: 
array([[1, 4],
       [3, 8]])
17:00:50 [7]: b * a[:,None] # Different result!
      Out[7]: 
array([[1, 2],
       [6, 8]])

a has shape (2,) and it is broadcast over the second dimension. a具有形状(2,)并在第二维上广播 So the result you get is that each row (the first dimension) is multiplied by the vector: 所以你得到的结果是每一行(第一个维度)乘以向量:

17:02:44 [10]: b * np.array([[1, 2], [1, 2]])
      Out[10]: 
array([[1, 4],
       [3, 8]])

On the other hand, a[:,None] has the shape (2,1) and so the orientation of the vector is known to be a column. 另一方面, a[:,None]具有形状(2,1) ,因此已知矢量的方向是列。 Hence, the result you get is from the following operation (where each column is multiplied by a ): 因此,您得到的结果来自以下操作(其中每列乘以a ):

17:03:39 [11]: b * np.array([[1, 1], [2, 2]])
      Out[11]: 
array([[1, 2],
       [6, 8]])

I hope that sheds some light on how the two arrays will behave differently. 我希望能够说明两个阵列的行为方式有何不同。

Use reshape() function eg open python terminal and type following: 使用reshape()函数,例如打开python终端并输入以下内容:

    >>> import numpy as np
    >>> a = np.random.random(5)
    >>> a
    array([0.85694461, 0.37774476, 0.56348081, 0.02972139, 0.23453958])
    >>> a.shape
    (5,)
    >>> b = a.reshape(5, 1)
    >>> b.shape
    (5, 1)

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

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