Mean-Variance-Standard Deviation CalculatorPassed

Hi!
I’ve started working on the final projects of Data Analysis with Python. Currently, I’ve finished the Mean-Variance-Standard Deviation Calculator properly. Although, I would like to ask for some feedback on my project.

Here is link to my project

  1. I’ve applied an efficient way to calculate the statistics? I mean, while I was working on it I had the temptation to write a function for each of the statistics.

  2. Raising a ValueError in a way like I’ve done is ok? I’ve been searching on Google how to raise an error and I’ve seen a ton of approaches. Some create an inherent class from Error. I’d only had 1 error during the tests and was about raising ValueError.

  3. At last, but not least, Is my code written in a good way? It’s clear and easy to follow it?

All comments would be appreciated. I will try to post my other projects asking for feedback if receive interesting answers.
Thanks for your time. :grinning:

PD: Update. I’ve been working on @sanity recommendations. I’ve added a calculator function to avoid repeating the same piece of code 6 times

Hey, looks like correct link to your project got lost somewhere during the writing of post :slight_smile:

1 Like

Thanks!
Without a link seems difficult to receive some feedback!! :stuck_out_tongue_closed_eyes: Now it works correctly?

  1. It’s very likely numpy’s calculations will be much faster than using python’s build-in functions, modules from standard library or self written functions. However for a matrix with 9 numbers efficiency concerns hardly could be an issue.
  2. Yes, that’s okay. No need to make another error class, when just standard ValueError is supposed to be used.
  3. For a question like that answer usually can be that there are things that can be improved, and there’s nothing wrong in that.

Function is clean and easy to follow. Some variable names should be improved to be more descriptive, there’s also inconsistent formatting - different number of blank lines - between code blocks in function. I like that parameter name list was changed to not shadow the build-in type, although I suspect reason for that partially could be wanting to use list() for type conversion from numpy’s arrays.

Part of what makes function clean is keeping everything structured the same way:

    ax1 = list(np.mean(a, axis=1))
    ax0 = list(np.mean(a, axis=0))
    flattened = a.flatten()
    flatt = np.mean(flattened)
    calculations["mean"] = [ax0, ax1, flatt]

Notice however this part of code repeats few times. Changed are only two variables - calculating function and which calculation it is. Such repeated pattern is a good place to try to change the relevant code into a loop, function, or both. It will significantly shorten whole function and put these calculations in a single place, allowing for easier and quicker change, if needed in future.

1 Like

Thanks for your comments @Sanity.

One last question, how can I iterate through different operations. I mean, If i write that code:

ax1 = list(np.mean(a, axis=1))
ax0 = list(np.mean(a, axis=0))
flattened = a.flatten()
flatt = np.mean(flattened)
calculations["mean"] = [ax0, ax1, flatt]

How I change the operator and the dict key? I’m thinking about using fstring but I’m not sure. It’s about calling different built functions of numpy? Sure there’s an easy way but I’m stuck with it.

Thanks again! :wink:

Remember functions in python can be passes just like other variables. For example:

def caller(func, argument):
    return func(argument)

caller(lower, 'Text')  # text
caller(upper, 'Text')  # TEXT

In similar way functions can be used in for loops, where each element of the structure iterated over gets the same name for its iteration in the loop.

1 Like

Alright! Thanks for your help @sanity. I hope to keep seeing you in this forum, your generosity is a motivation punch for beginners like me!

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.