np_r function with two values

Question:

I have found the following code:

x=0.3*np.random.randn(100,2)
x_train=np.r_[x+2,x-2]

In the first case x is an array of 100 rows and two columns in a format list of list, for what I see. In this case when I use size it returns 200. However, in the x_train part it is using np.r_. For what I know this instruction serves to concatenate arrays, so when I run size again it returns 400. However, I cannot get what does x+2 and x-2 perform in this case. For example, why in the first case is adding 2 and in the other case is subtracting 2?

I have read the documentation and still not get any clue.

Asked By: Little

||

Answers:

The linked scikit is showing how to find two separate classes in 2 dimensions. The code you are asking about generates random x&y coordinate data for those two separate classes

The purpose of np.random.randn is to generate 100 normally-distributed random x and y coordinate pairs (ie x is a 100×2 matrix). Side note, the .3 multiplier is probably used to decreased standard deviation for tighter clusters.

By adding 2 to x (ie add the value 2 to each element in x), they create a set of x and y coordinates that are closely scattered around (2,2) and by subtracting 2 from x, they create a set of x and y coordinates that are scattered around (-2,-2).

np.r_ ,in this case, is the same as using np.concatenate((x-2,x+2),0) which creates a 200×2 array with 100 observations of x&y points scattered around (2,2) and 100 scattered around (-2,-2) in one matrix

Answered By: Gene Burinsky
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.