W3docs

How to normalize a NumPy array to a unit vector?

To normalize a NumPy array to a unit vector, you can use the numpy.linalg.norm function to calculate the magnitude of the vector, and then divide the array by this magnitude.

To normalize a NumPy array to a unit vector, you can use the numpy.linalg.norm function to calculate the magnitude of the vector, and then divide the array by this magnitude. Here is an example code snippet:

use the numpy.linalg.norm function to calculate the magnitude of the vector

python— editable, runs on the server

<div class="alert alert-info flex not-prose"> Watch a course <span class="hidden md:block">Watch a video course </span> Python - The Practical Guide</div>

This will output [0.26726124 0.53452248 0.80178373]