我是一个交互式会话:
In [168]: np.source(np.expand_dims)
In file: /usr/local/lib/python3.5/dist-packages/numpy/lib/shape_base.py
def expand_dims(a, axis):
"""
.... docs
"""
a = asarray(a)
shape = a.shape
if axis > a.ndim or axis < -a.ndim - 1:
# 2017-05-17, 1.13.0
warnings.warn("Both axis > a.ndim and axis < -a.ndim - 1 are "
"deprecated and will raise an AxisError in the future.",
DeprecationWarning, stacklevel=2)
# When the deprecation period expires, delete this if block,
if axis < 0:
axis = axis + a.ndim + 1
# and uncomment the following line.
# axis = normalize_axis_index(axis, a.ndim + 1)
return a.reshape(shape[:axis] + (1,) + shape[axis:])
所以基本上它使用reshape
, 形状在正确的位置包含大小1
尺寸。这也可以用[:,:,np.newaxis,...]
语法来完成。
其中任何一个是否可移植到 Java 取决于该语言中的数组实现。