Why there is a star inside randn?
 Why there is a star inside randn? new_to_python Silly Frenchman Posts: 39 Threads: 12 Joined: Feb 2020 Reputation: Mar-04-2020, 01:02 AM (This post was last modified: Mar-04-2020, 11:40 AM by scidam.) Hi, I have question about generating a linear model from random data. I suppose that by default, the value of size in the dnorm function is one. When this function is called with N=100, size becomes 100. Am I correct? If so, what is the reason for having the * symbol in np.randn(*size)? What is the different between this and np.randn(size)? ```def dnorm(mean, variance, size=1): if isinstance(size, int): size = size, return mean + np.sqrt(variable) * np.random.randn(*size) N = 100 X = np.c_[dnorm(0, 0.4, size=N), dnorm(0, 0.6, size=N)]``` Reply Posts: 818 Threads: 1 Joined: Mar 2018 Reputation: Mar-04-2020, 11:40 AM When size is of integer type, there is no difference. However, when size is a tuple of integers, dnorm generates nd-random array. So, dnorm function allows you to pass size as an integer value (in case of one-dimensional array) and as a tuple of integers, if you want to generate nd-array. Reply new_to_python Silly Frenchman Posts: 39 Threads: 12 Joined: Feb 2020 Reputation: Mar-05-2020, 02:20 PM Thanks. How can I tell if size is an integer or a tuple of integers? 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? Reply Posts: 818 Threads: 1 Joined: Mar 2018 Reputation: 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. Reply new_to_python Silly Frenchman Posts: 39 Threads: 12 Joined: Feb 2020 Reputation: Mar-06-2020, 04:45 AM Understood. Thank you. Reply

 Possibly Related Threads… Thread Author Replies Views Last Post fpdf star character issue KatMac 3 633 May-01-2021, 06:22 PM Last Post: KatMac How to scrape and count star rating using Selenium and Python? celinafregoso99 1 761 Feb-15-2021, 02:45 PM Last Post: kashcode Optimisation problem - star identification pberrett 1 1,163 Apr-01-2019, 12:17 AM Last Post: scidam

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020