0

I am new to Python and curious if there is a way to instantly create a list of numpy.random.normal values of a specific shape without using a loop like this?

def get_random_normal_values():
    i = 0
    result = []
    while i < 100:
        result.append(np.random.normal(0.0, 1.0, (16, 16, 3)))
        i = i + 1
    return result
1
  • 2
    Why not just use (100, 16, 16, 3)? But no. And you should always use a for loop here, or even a list comprehension. Commented Jan 11, 2022 at 12:00

3 Answers 3

3

Simply use the size parameter to numpy.random.normal to define the final shape of the desired array:

result = np.random.normal(size=(100, 16, 16, 3)) # default parameters are loc=0.0, scale=1.0

You will get an array instead of a list of arrays, but this does not remove functionality (you can still loop over it). Please make your use case more explicit if this doesn't suit your needs.

Sign up to request clarification or add additional context in comments.

Comments

1

Yes, of course it is posssible:

import numpy as np
def get_random_normal_values(n_samples: int,  size: tuple[int]) -> list[np.array]:
    all_samples = np.random.normal(np.random.normal(0.0, 1.0, (n_samples, ) + size))
    return list(all_samples)

A distribution of (n_samples, m, n, o) shape is generated where (m, n, o) = size (input parameter). After that, we "flatten" it into a list of length n_samples where each item of that list is an array of shape (m, n, o).

Comments

1

Try This :

 np.random.normal(0.0, 1.0, (100, 16, 16, 3))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.