def normalize(x, axis=None):
"""Normalize the values of an ndarray to sum to 1 along the given axis.
Parameters
----------
x : np.ndarray
Input multidimensional array to normalize.
axis : int, default=None
Axis to normalize along, otherwise performed over the full array.
Returns
-------
z : np.ndarray, shape=x.shape
Normalized array.
"""
if not axis is None:
shape = list(x.shape)
shape[axis] = 1
scalar = x.astype(float).sum(axis=axis).reshape(shape)
scalar[scalar == 0] = 1.0
else:
scalar = x.sum()
scalar = 1 if scalar == 0 else scalar
return x / scalar
generate_melody.py 文件源码
python
阅读 35
收藏 0
点赞 0
评论 0
评论列表
文章目录