Gregory Price
Gregory Price

Reputation: 89

Normalize a vector where the dot product equals 1 in Python?

I already know, and understand how I can normalize an array in Python, but I am trying to create a random array. I want the dot product of the array (when I dot it with itself) to equal a value of one. I have been looking for a way to do this for over twelve hours now, and can not find a way. Any help or ideas would be great. Thank you.

Upvotes: 2

Views: 4571

Answers (1)

Miriam Farber
Miriam Farber

Reputation: 19634

This will do the job:

import numpy as  np
x=np.random.randn(5)
x=x/np.linalg.norm(x)

Then np.dot(x,x) is 1.0

Upvotes: 3

Related Questions