def unit_vector(data, axis=None, out=None):
if out is None:<EOL><INDENT>data = numpy.array(data, dtype=numpy.float64, copy=True)<EOL>if data.ndim == <NUM_LIT:1>:<EOL><INDENT>data /= math.sqrt(numpy.dot(data, data))<EOL>return data<EOL><DEDENT><DEDENT>else:<EOL><INDENT>if out is not data:<EOL><INDENT>out[:] = numpy.array(data, copy=False)<EOL><DEDENT>data = out<EOL><DEDENT>length = numpy.atleast_1d(numpy.sum(data*data, axis))<EOL>numpy.sqrt(length, length)<EOL>if axis is not None:<EOL><INDENT>length = numpy.expand_dims(length, axis)<EOL><DEDENT>data /= length<EOL>if out is None:<EOL><INDENT>return data<EOL><DEDENT>
Return ndarray normalized by length, i.e. Euclidean norm, along axis.

    >>> v0 = numpy.random.random(3)
    >>> v1 = unit_vector(v0)
    >>> numpy.allclose(v1, v0 / numpy.linalg.norm(v0))
    True
    >>> v0 = numpy.random.rand(5, 4, 3)
    >>> v1 = unit_vector(v0, axis=-1)
    >>> v2 = v0 / numpy.expand_dims(numpy.sqrt(numpy.sum(v0*v0, axis=2)), 2)
    >>> numpy.allclose(v1, v2)
    True
    >>> v1 = unit_vector(v0, axis=1)
    >>> v2 = v0 / numpy.expand_dims(numpy.sqrt(numpy.sum(v0*v0, axis=1)), 1)
    >>> numpy.allclose(v1, v2)
    True
    >>> v1 = numpy.empty((5, 4, 3))
    >>> unit_vector(v0, axis=1, out=v1)
    >>> numpy.allclose(v1, v2)
    True
    >>> list(unit_vector([]))
    []
    >>> list(unit_vector([1]))
    [1.0]