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
CodePudding user response:
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.
CodePudding user response:
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).
CodePudding user response:
Try This :
np.random.normal(0.0, 1.0, (100, 16, 16, 3))
