0

I am currently looking into an example implementation of Random Search from Machine Learning Refined and have trouble understanding the following code snippet where w is an input vector and num_samples is number of random directions to sample:

# construct set of random unit directions
directions = np.random.randn(num_samples,np.size(w))
norms = np.sqrt(np.sum(directions*directions,axis = 1))[:,np.newaxis]
directions = directions/norms   

What I do understand is that we sample random vectors in the size of w, which we then need to normalize in order to get unit directions. So we calculate |V| = sqrt(x*x + y*y + z*z) for all directions or V/|V| = (x/|V|, y/|V|, z/|V|) respectively.

Could someone explain to me in simple terms what happens in line 3, specifically why we sum over axis=1 and what the [:,np.newaxis] does? I have a vague grasp on what this is supposed to do, but some intuition would help a lot.

Stimmot
  • 999
  • 1
  • 7
  • 22
  • 1
    Sum over `axis=1` means calculate the sum for each sample (return a `num_samples x 1` vector). If you don't specify `axis=1`, it will calculate the total sum (return a scalar). `[:,np.newaxis]` is used to add dimension. – wong.lok.yin Feb 09 '22 at 01:30

1 Answers1

0

As wong.lok.yin pointed out in the comments, the line

norms = np.sqrt(np.sum(directions*directions,axis = 1))[:,np.newaxis]

sums over axis=1 (the column axis) and adds a new axis to the resulting vector by [:,np.newaxis]. There is a good explanation of how np.newaxis works here.

Thanks @wong.lok.yin for the help.

Stimmot
  • 999
  • 1
  • 7
  • 22