def normalize(vec):
"""
Given an input vector normalize the vector
Parameters
==========
vec : array_like
input vector to normalize
Returns
=======
out : array_like
normalized vector
Examples
========
>>> import spacepy.toolbox as tb
>>> tb.normalize([1,2,3])
[0.0, 0.5, 1.0]
"""
# check to see if vec is numpy array, this is fastest
if isinstance(vec, np.ndarray):
out = (vec - vec.min())/np.ptp(vec)
else:
vecmin = np.min(vec)
ptp = np.ptp(vec)
out = [(val - vecmin)/ptp for val in vec]
return out
评论列表
文章目录