numpy:将(n,)数组转换为(n,1)数组的语法/习惯用法?

时间:2022-02-09 21:39:36

I'd like to cast a numpy ndarray object of shape (n,) into one having shape (n, 1). The best I've come up with is to roll my own _to_col function:

我想把一个形状为n的ndarray ndarray对象(n,)变成一个有形状(n,1)的对象。我提出的最好的方法是滚动我自己的_to_col函数:

def _to_col(a):
    return a.reshape((a.size, 1))

But it is hard for me to believe that such a ubiquitous operation is not already built into numpy's syntax. I figure that I just have not been able to hit upon the right Google search to find it.

但是我很难相信这种无处不在的操作还没有被纳入numpy的语法中。我认为我只是无法找到正确的Google搜索来找到它。

2 个解决方案

#1


10  

I'd use the following:

我使用以下内容:

a[:,np.newaxis]

An alternative (but perhaps slightly less clear) way to write the same thing is:

编写相同内容的另一种方法(但可能稍微不那么清晰)是:

a[:,None]

All of the above (including your version) are constant-time operations.

以上所有内容(包括您的版本)都是常量操作。

#2


2  

np.expand_dims is my favorite when I want to add arbitrary axis.

当我想添加任意轴时,np.expand_dims是我最喜欢的。

None or np.newaxis is good for code that doesn't need to have flexible axis. (aix's answer)

无或np.newaxis适用于不需要灵活轴的代码。 (aix的回答)

>>> np.expand_dims(np.arange(5), 0).shape
(1, 5)
>>> np.expand_dims(np.arange(5), 1).shape
(5, 1)

example usage: demean an array by any given axis

示例用法:按任何给定轴贬低数组

>>> x = np.random.randn(4,5)
>>> x - x.mean(1)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: shape mismatch: objects cannot be broadcast to a single shape


>>> ax = 1
>>> x - np.expand_dims(x.mean(ax), ax)
array([[-0.04152658,  0.4229244 , -0.91990969,  0.91270622, -0.37419434],
       [ 0.60757566,  1.09020783, -0.87167478, -0.22299015, -0.60311856],
       [ 0.60015774, -0.12358954,  0.33523495, -1.1414706 ,  0.32966745],
       [-1.91919832,  0.28125008, -0.30916116,  1.85416974,  0.09293965]])
>>> ax = 0
>>> x - np.expand_dims(x.mean(ax), ax)
array([[ 0.15469413,  0.01319904, -0.47055919,  0.57007525, -0.22754506],
       [ 0.70385617,  0.58054228, -0.52226447, -0.66556131, -0.55640947],
       [ 1.05009459, -0.27959876,  1.03830159, -1.23038543,  0.73003287],
       [-1.90864489, -0.31414256, -0.04547794,  1.32587149,  0.05392166]])

#1


10  

I'd use the following:

我使用以下内容:

a[:,np.newaxis]

An alternative (but perhaps slightly less clear) way to write the same thing is:

编写相同内容的另一种方法(但可能稍微不那么清晰)是:

a[:,None]

All of the above (including your version) are constant-time operations.

以上所有内容(包括您的版本)都是常量操作。

#2


2  

np.expand_dims is my favorite when I want to add arbitrary axis.

当我想添加任意轴时,np.expand_dims是我最喜欢的。

None or np.newaxis is good for code that doesn't need to have flexible axis. (aix's answer)

无或np.newaxis适用于不需要灵活轴的代码。 (aix的回答)

>>> np.expand_dims(np.arange(5), 0).shape
(1, 5)
>>> np.expand_dims(np.arange(5), 1).shape
(5, 1)

example usage: demean an array by any given axis

示例用法:按任何给定轴贬低数组

>>> x = np.random.randn(4,5)
>>> x - x.mean(1)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: shape mismatch: objects cannot be broadcast to a single shape


>>> ax = 1
>>> x - np.expand_dims(x.mean(ax), ax)
array([[-0.04152658,  0.4229244 , -0.91990969,  0.91270622, -0.37419434],
       [ 0.60757566,  1.09020783, -0.87167478, -0.22299015, -0.60311856],
       [ 0.60015774, -0.12358954,  0.33523495, -1.1414706 ,  0.32966745],
       [-1.91919832,  0.28125008, -0.30916116,  1.85416974,  0.09293965]])
>>> ax = 0
>>> x - np.expand_dims(x.mean(ax), ax)
array([[ 0.15469413,  0.01319904, -0.47055919,  0.57007525, -0.22754506],
       [ 0.70385617,  0.58054228, -0.52226447, -0.66556131, -0.55640947],
       [ 1.05009459, -0.27959876,  1.03830159, -1.23038543,  0.73003287],
       [-1.90864489, -0.31414256, -0.04547794,  1.32587149,  0.05392166]])