106-3 : np.mean() vs np.array().mean()

Screen Link:
Python Significance Testing Tutorial | Dataquest

The mission asks us to use numpy mean to calculate average i.e. np.mean(listname).
I would like to understand more about it and how/why it is different from the option in the next paragraph. If you could share links or some hints on beneath the hood workings. As per documentation, np.mean(listname) tries to convert list to array, providing, I guess, a shortcut compared to option mentioned next.

As per previous missions, I am used to converting a list into array and then finding the mean i.e. np.array(listname).mean()

I am not sure I understand your question entirely. But, in the documentation, usually, there is a link to the source code for that particular function/method.

So, you could check out the source code to see how it works under the hood. However, I would argue that’s not necessary or even recommended given how difficult it can be to sift through the code of such libraries.

In the documentation it’s specified that one of the input parameters to np.mean() is -

Parameters

  • a: array_like
    Array containing numbers whose mean is desired. If a is not an array, a conversion is attempted.

Since your input is a list, it converts the list to an array and then calculates the mean of that array.

In the above, you are converting listname into an array using np.array() and then calculating the mean of that array.

It really depends, I think, on what you want to do with listname. In both cases above, listname is not saved into an array. So, both use-cases are viable. But, np.mean() is just cleaner/more direct.

If you wanted to continue using the array you get from listname, then it likely makes better sense to first convert it into an array and then calculate the mean of that array separately.

arrayname = np.array(listname)
arraymean = arrayname.mean()

Both approaches, I believe, are essentially the same but np.mean() is better/simpler/cleaner/obvious/direct if you don’t plan on storing the array.

It is possible that one approach is somehow less memory intensive than the other but I doubt that to be the case for the above or similar scenarios. I would recommend either searching for that on other websites like stackoverflow or you can post such questions on those websites if this is a topic of interest for you. “Under the hood” discussions sort of reach a ceiling in such forums since that’s not the primary focus.

If I misunderstood your question, feel free to clarify!

You understood my query correctly. Thanks for the detailed response. Thought it would be good to have it documented for people on the same track, that there are more than one way to find mean and other values and that np.mean() is better