Python numpy:将(n,)数组转换为(n,1)数组的语法/习惯用法?
我想将形状为(n,)的numpyPython numpy:将(n,)数组转换为(n,1)数组的语法/习惯用法?,python,arrays,vector,numpy,casting,Python,Arrays,Vector,Numpy,Casting,我想将形状为(n,)的numpyndarray对象转换为具有形状(n,1)的对象。我想到的最好方法是使用我自己的_to_col函数: def _to_col(a): return a.reshape((a.size, 1)) 但我很难相信这样一个无处不在的操作还没有内置到numpy的语法中。我想我只是没能找到合适的谷歌搜索来找到它。我会使用以下方法: a[:,np.newaxis] 另一种(但可能不太清楚)写同样东西的方法是: a[:,None] 以上所有操作(包括您的版本)都是固
ndarray
对象转换为具有形状(n,1)的对象。我想到的最好方法是使用我自己的_to_col函数:
def _to_col(a):
return a.reshape((a.size, 1))
但我很难相信这样一个无处不在的操作还没有内置到numpy的语法中。我想我只是没能找到合适的谷歌搜索来找到它。我会使用以下方法:
a[:,np.newaxis]
另一种(但可能不太清楚)写同样东西的方法是:
a[:,None]
以上所有操作(包括您的版本)都是固定时间操作。np.expand_dims是我在添加任意轴时的最爱 None或np.newaxis适用于不需要灵活axis的代码。(aix的回答) 示例用法:按任意给定轴对数组进行降级
>>> 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]])
>x=np.random.randn(4,5)
>>>x-x.平均值(1)
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
ValueError:形状不匹配:无法将对象广播到单个形状
>>>ax=1
>>>x-np.展开尺寸(x.平均值(ax),ax)
数组([-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.展开尺寸(x.平均值(ax),ax)
数组([[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]])
要将轴添加到2d或更高的nd数组的末尾,请使用省略号而不是冒号:a[…,None]
,该省略号涵盖了所需的任意多个维度。然后a.shape
将从例如(n,m)
变为(n,m,1)
。
>>> 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]])