我一個交互式會話:
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取決於該語言的數組實現。
有一個鏈接到功能文檔頁面上的源代碼。 – hpaulj