Skip to content
Advertisement

Make a data profiler class that takes as params on the init a list of data

I need this class to include the following methods only using self:

  1. get_summary_stats: should calculate the mean, min and max.
  2. min_max_scale: converts the array to 0-1 values.
  3. score_scale: converts the array to zscores.

I’m trying it this way without luck, when you run it it smoothly goes by but when I add the last line it gives me an error saying __init__() takes 1 positional argument but 2 were given

JavaScript

I’ve tried 1)adding list to summary_stats_class(list) because I read it in other question, as well as 2)adding self.list = [] after __init__ piece and finally 3) adding , *args, **kwargs to each method without any luck, could someone please help? Thanks in advance!

Advertisement

Answer

The self is used to indicate the class level variables. The documentation on classes has some examples showing how to use the self to declare instance variables and use them in class methods.

I have updated the class to mitigate the error:

JavaScript

Output:

JavaScript

Explanation:

  • We passed the list as a parameter when we created the ssc object of the class summary_stats_class.
  • In the constructor of summary_stats_class, we set the parameter list to self.data variable.
  • We then calculated the min, max, and mean of the data and set them to self.min_data, self.max_data, and self.mean_data respectively. We set them to self so that these variables can be used from any other methods of the class.
  • In the get_summary_stats, min_max_scale, and zscore_scale method, we used the previously calculated variables using self.VARIABLE_NAME.

References:

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement