原文:https://docs.scipy.org/doc/numpy/reference/generated/numpy.expand_dims.html
校对:(虚位以待)
numpy.expand_dims(a, axis)[source]展开数组的形状。
插入新轴,对应于数组形状中的给定位置。
| 参数: | a:array_like
axis:int
|
|---|---|
| 返回: | res:ndarray
|
例子
>>> x = np.array([1,2])
>>> x.shape
(2,)
以下等效于x[np.newaxis,:]或x[np.newaxis]:
>>> y = np.expand_dims(x, axis=0)
>>> y
array([[1, 2]])
>>> y.shape
(1, 2)
>>> y = np.expand_dims(x, axis=1) # Equivalent to x[:,newaxis]
>>> y
array([[1],
[2]])
>>> y.shape
(2, 1)
请注意,一些示例可以使用None而不是np.newaxis。这些是相同的对象:
>>> np.newaxis is None
True