Descriptive Statistics Class

Descriptive Statistics Class

Python OOP Problem

 

The goal of this Python oop problem is to write a Python class called `DataAnalyzer` that allows users to calculate statistical information for a data series. For example, assume you are a teacher and you need the figures of all your students.
In the class, define the following methods:

1. `add_data()`: This method should prompt the user to enter a number repeatedly until they enter a non-numeric value. The entered numbers should be stored in a list.

2. `calculate_mean()`: This method should calculate and return the mean of the data series. However, it should only be called if the number of items in the data series is greater than 4. Otherwise, it should return the message “Insufficient data for mean calculation (less than 5 items).”

3. `calculate_max()`: This method should calculate and return the maximum value in the data series.

4. `calculate_min()`: This method should calculate and return the minimum value in the data series.

5. `calculate_standard_deviation()`: This method should calculate and return the standard deviation of the data series. Similar to the mean calculation, it should only be called if the number of items in the data series is greater than 4. Otherwise, it should return the message “Insufficient data for standard deviation calculation (less than 5 items).”

6. `calculate_sum()`: This method should calculate and return the sum of all the values in the data series.

7. `display_statistics()`: This method should call the other methods to calculate the mean, max, min, standard deviation, and sum of the data series. It should then display these statistical values to the user.

8. Create an instance of the "DataAnalyzer” class and call its methods to analyze the data series input by the user.

Make sure to test your code by creating an instance of the `DataAnalyzer` class, adding data using the `add_data()` method, and displaying the statistics using the `display_statistics()` method.

For more projects of OOP in Python see here.

Expected output

Enter a number (or any letter to finish): 5
Enter a number (or any letter to finish): 6
Enter a number (or any letter to finish): 9
Enter a number (or any letter to finish): 10
Enter a number (or any letter to finish): 59
Enter a number (or any letter to finish): 85632
Enter a number (or any letter to finish): 2548
Enter a number (or any letter to finish): 14
Enter a number (or any letter to finish): 785
Enter a number (or any letter to finish): 32
Enter a number (or any letter to finish): 89
Enter a number (or any letter to finish): finish
Mean: 8108.090909090909
Max: 85632.0
Min: 5.0
Standard Deviation: 24526.06831499365
Sum: 89189.0

SOURCE CODE

## Define the class
class DataAnalyzer:
    def __init__(self):
        self.data_series = []

    def add_data(self):
        while True:
            try:
                data = float(input("Enter a number (or any letter to finish): "))
                self.data_series.append(data)
            except ValueError:
                break

    def calculate_mean(self):
        if len(self.data_series) > 4:
            return sum(self.data_series) / len(self.data_series)
        else:
            return "Insufficient data for mean calculation (less than 5 items)."

    def calculate_max(self):
        return max(self.data_series)

    def calculate_min(self):
        return min(self.data_series)

    def calculate_standard_deviation(self):
        if len(self.data_series) > 4:
            mean = self.calculate_mean()
            variance = sum((x - mean) ** 2 for x in self.data_series) / len(self.data_series)
            return variance ** 0.5
        else:
            return "Insufficient data for standard deviation calculation (less than 5 items)."

    def calculate_sum(self):
        return sum(self.data_series)

    def display_statistics(self):
        print(f"Mean: {self.calculate_mean()}")
        print(f"Max: {self.calculate_max()}")
        print(f"Min: {self.calculate_min()}")
        print(f"Standard Deviation: {self.calculate_standard_deviation()}")
        print(f"Sum: {self.calculate_sum()}")

We will be happy to hear your thoughts

Leave a reply

Python and Excel Projects for practice
Register New Account
Shopping cart