1

The Task is to find standard Deviation of a given array. Using Class and Methods. Since I'm new to classes and Objects So, I'm not getting what's the problem in the code.

Input: 10 40 30 50 20

   class Mean:
       def __init__(self,X):
           self.mean=(sum(X))/len(X)
           #return self.mean
   class Deviation(Mean):
        def deviation(self,X):
           Mean.__init__(self,X)
           m=Mean(X)
           self.d=0
           for i in X:
               self.d = self.d + (i-m)**2
               return self.d
   class Standard_Deviation:
       def display(self,X):
           sd=pow((Deviation(X))/(len(X)),0.5)
           return(sd)

   X=list(map(int,input().rstrip().split()))
   t=Standard_Deviation()
   z=t.display(X)
   print(z)

The expected result is 14.1 but I'm getting a TypeError.

Traceback (most recent call last):
  File "Solution.py", line 29, in <module>
    z=t.display(X)
  File "Solution.py", line 24, in display
    sd=pow((Deviation(X))/(len(X)),0.5)
TypeError: unsupported operand type(s) for /: 'Deviation' and 'int'
Reblochon Masque
  • 35,405
  • 10
  • 55
  • 80
lokesh bihani
  • 89
  • 1
  • 9

2 Answers2

0

The main problem in the code is a design problem: You probably should read a little bit about object oriented programming, in order to understand what objects are like, what they do, and how to use them.

Here is an example of a class StatsData that takes a set of values as input, then allow to calculate the classical statistical characteristics values of the data:

class StatsData:
    def __init__(self, values):
        self.values = values[:]
        self._mean = None
        self._dev = None
        self._std_dev = None

    @property
    def mean(self):
        if self._mean is None:
            self._mean = sum(self.values) / len(self.values)
        return self._mean

    @property
    def dev(self):
        if self._dev is None:
            self._dev = 0
            for value in self.values:
                self._dev += (value - self.mean)**2
        return self._dev

    @property
    def std_dev(self):
        if self._std_dev is None:
            self._std_dev = pow(self.dev / len(self.values), 0.5)
        return self._std_dev

    def __str__(self):
        return f'mean: {stats.mean}, deviation: {stats.dev}, standard deviation: {stats.std_dev: 0.2f}'


input_values = [int(elt) for elt in "10 40 30 50 20".split()]
stats = StatsData(input_values)
print(stats)

output:

mean: 30.0, deviation: 1000.0, standard deviation:  14.14
Reblochon Masque
  • 35,405
  • 10
  • 55
  • 80
  • what does @property mean in that class StatsData? – lokesh bihani Jul 02 '19 at 08:00
  • 1
    @property allows you to call a method as if it were an attribute: `stats.mean` instead of `stats.mean()` - [more info](https://stackoverflow.com/questions/17330160/how-does-the-property-decorator-work) – Reblochon Masque Jul 02 '19 at 08:14
0

There are a few bugs in your code. And indeed, the way your construction/connection of these classes is not the best or right way of OOP.

While @Reblochon Masque's great answer provides you a more mature way to do OOP. I just slightly modified your code to a runnable one. So you can compare the differences and update your OOP experiences :)

class Mean:
    def __init__(self,X):
        self.data = X
        self.mean = sum(X)/len(X)
        #return self.mean


class Deviation(Mean):
    def __init__(self, X):
        # Since class Deviation is inherit from Mean, and 
        # class Mean has its own properties and calculations 
        # in its `__init__()`, (namely self.data and self.mean)
        # In order to get those from Mean, the subclass Deviation
        # need to explictly call its mother's __init__ with super,
        # Read the doc here https://docs.python.org/3/library/functions.html#super
        super(Deviation, self).__init__(X) 
        # This line does the same thing with # super().__init__(X) 
        # but more explictly.

        self.dev = self.deviation()
        self.var = self.dev / len(self.data)

    def deviation(self):
        dev = [(i-self.mean)**2 for i in self.data]
        return sum(dev)

class Standard_Deviation:
    def display(self, X):
        d = Deviation(X)
        sd = pow(d.dev/len(X), 0.5)
        return sd

# X=list(map(int,input().rstrip().split()))
X = [10, 40, 30, 50, 20]
t = Standard_Deviation()
z = t.display(X)
print(z)
YaOzI
  • 16,128
  • 9
  • 76
  • 72
  • I'm not getting this part of the code: class ``` Deviation(Mean): def __init__(self, X): super(Deviation, self).__init__(X) self.dev = self.deviation()``` – lokesh bihani Jul 02 '19 at 08:06