np.expand_dims()用法详解
numpy.expand_dims(a, axis)
作用:扩展数组的维度
例:
def load_pose_cords_from_strings(y_str, x_str):
"""
x_str = '[..., ..., ..., ...]' (str)
x_cords = [..., ..., ..., ...] (list)
x_cords_expanded = [[...], [...], [...], [...],....] (ndarray)
"""
y_cords = json.loads(y_str) # json.loads()把str转成list
x_cords = json.loads(x_str)
# 在最后一个维度扩展一维,即将长度为18的list转成shape为(18,1)的ndarray数组
y_cords_expanded = np.expand_dims(y_cords, -1)
x_cords_expanded = np.expand_dims(x_cords, -1)
return np.concatenate([y_cords_expanded, x_cords_expanded], axis=1)