Mar-05-2020, 11:49 PM
(Mar-05-2020, 02:20 PM)new_to_python Wrote: Thanks. How can I tell if size is an integer or a tuple of integers?In the line #2 in your code you check if
size
is of integeter type. If size is integer, the next line (#3) size = size,
, which is equivalent to size = (size, )
, turns size
into a tuple (of len 1).So, it doesn't matter, what you assign to
size
parameter : integer or tuple. Passing alone integer as size
variable turns it to a tuple of len 1 before "sending" it to randn
. If size originally is a tuple, it doesn't change and pass to randn function as is.However, if someone pass a tuple consiting not only of integers, e.g. (1, 'bob') or (1, 3.4), this likely lead to an exception in the
randn
function. Therefore, you probably can wish to do additional checks, e.g. if all elements of a tuple passed as size variable are integers.(Mar-05-2020, 02:20 PM)new_to_python Wrote: Is the number of elements in the tuple corresponds to the dimension of the nd-array? For example if size is a tuple of (x, y), then np.random.randn(*size) generates a 2 dimensional array of x rows and y columns. If size is a tuple of three elements (x, y, z), then np.random.randn(*size) generates a 3 dimensional array of x rows, y columns and z levels?You are right. If you pass a tuple of integers of len N, randn returns a N-dimensional random array.