Doodle Bob
Doodle Bob

Reputation: 13

Using Python, generate 100 X 100 random matrix whose entries are sampled from the normal distribution

Generate random vectors using numpy.random. Write code in Python that produces a 100 X 100 random matrix whose entries are samples from the normal distribution.


This is what I produced and am unsure if this is correct. I have a question on normal distribution as well. Should my random numbers come from a certain pool? Such as $[0,1]$ or is it possible to get values out of this range?

This is the code if wrote in Jupyter Notebook:

import numpy as np
a = np.random.randn(100,100)
a
array([[-0.42952803, -0.55136761, -0.45544016, ..., -0.54125441,
     2.31481612,  0.93721055],
   [-0.2440975 , -0.10233273, -0.06972217, ..., -0.25760561,
     0.48431004, -0.91599734],
   [-1.39176645, -0.79784139, -0.21914249, ...,  2.38224209,
     1.57696294,  0.48747715],
   ...,
   [ 0.38458431, -1.75968742,  1.64696889, ...,  1.43273609,
    -0.74896945,  0.48588267],
   [ 1.22934075,  1.27112809, -0.40593726, ...,  0.63584471,
     0.11152366, -2.23030795],
   [ 1.5910005 ,  0.29184142, -0.01811951, ..., -0.25800051,
    -0.09681777,  0.40182752]])

Upvotes: 1

Views: 1657

Answers (2)

mdeverna
mdeverna

Reputation: 332

I think what you are doing will work, however, the numpy documentation suggests that you use numpy.random.standard_normal

See the note for the numpy.random.randn documentation for more details.

So this would look like...

import numpy as np

mat = np.random.standard_normal(size=(100, 100))
mat.size()

which produces

array([[ 0.19635784,  0.19134202, -0.90622914, ..., -0.1487657 ,
         0.210058  , -0.06616276],
       [ 0.37758747, -1.96359795, -0.1302543 , ...,  0.27463501,
         0.5956493 ,  0.95462422],
       [-0.06986621,  1.6122695 , -0.91379974, ...,  0.94488747,
        -0.05906328, -1.09491503],
       ...,
       [ 0.04273415, -0.7566953 ,  0.34079966, ..., -0.2154078 ,
        -1.42879529, -0.7601603 ],
       [ 0.87875502, -0.18143793, -0.97638314, ...,  0.19633813,
         1.19428871, -1.9585137 ],
       [ 0.75305984,  0.26421749, -1.06839234, ..., -1.10464615,
        -0.25891926,  1.2184856 ]])

Upvotes: 0

thehumaneraser
thehumaneraser

Reputation: 620

What you did is correct but there are a few things to note.

First, np.random.randn() is specifically for drawing from the standard normal distribution (the one with mean 0 and standard deviation 1). It sounds like that is what you want based on your question but just note that you could also use np.random.normal(mu, sd, size=(100, 100)) where mu is the mean of the normal distribution you want to sample from and sd is the standard deviation.

Regarding your question about what range values should be in, they most certainly are not constrained to [0, 1]. The normal distribution is a continuous probability density function defined on all the real numbers, so in theory you could see any real number, though the probability of observing each value decreases as you move away from the mean.

For more about normal distributions in general, I recommend reading this page from Wolfram.

Upvotes: 2

Related Questions