Uniformly distributed data in d-dimensions

How can I generate uniformly distributed data [-1,1] ^ d in Python? For instance. d is a size such as 10.

I know how to generate evenly distributed data like np.random.randn (N), but the dimensional thing really bothers me.

+4
source share
4 answers

Assuming the independence of individual coordinates, then the following generates a random point in [-1, 1)^d

 np.random.random(d) * 2 - 1 

The following will generate observations n , where each row is an observation

 np.random.random((n, d)) * 2 - 1 
+6
source

As already noted, randn produces a normally distributed number (aka Gaussian). To get an even distribution, you must use a โ€œuniformโ€.

If you need only one sample at a time in 10 evenly distributed numbers, you can use:

 import numpy as np x = np.random.uniform(low=-1,high=1,size=10) 

OR if you want to generate lots (for example, 100) from them immediately, you can do:

 import numpy as np X = np.random.uniform(low=-1,high=1,size=(100,10)) 

Now X [0], X [1], ... each has a length of 10.

+6
source

You can import the random module and call random.random to get a random sample from [0, 1). You can double this and subtract 1 to get a sample from [-1, 1].

Draw the values โ€‹โ€‹of d in this way, and the tuple will be a uniform separation from the cube [-1, 1) ^ d.

+2
source

Without numpy:

 [random.choice([-1,1]) for _ in range(N)] 

There may be reasons to use numpy internal mechanisms or to use random() manually, etc. But these are implementation details, as well as the entropy bits that the operating system provides that generate rations for generating random numbers.

-1
source

All Articles