Code Generator

Python Statistics Calculator

This Python function computes the mean, median, and mode of a list of numbers while handling exceptions for invalid inputs, ensuring robust error messaging and user-friendly statistics analysis.


Empty image or helper icon

Prompt

please

Answer

Comprehensive Function Development in Python

Problem Definition

Develop a Python function that calculates the mean, median, and mode of a list of numbers. This function should handle exceptions related to invalid input and provide useful error messages.

Requirements

  • Input: A list of numerical values.
  • Output: A dictionary containing the mean, median, and mode.
  • Handle exceptions for empty lists and non-numeric values.

Implementation Steps

  1. Import Necessary Libraries: We will use statistics for calculating the median and mode, and numpy for the mean.
  2. Define the Function: Create a function called calculate_statistics.
  3. Input Validation: Validate the input for:
    • Non-numeric values.
    • Empty lists.
  4. Calculations: Compute mean, median, and mode.
  5. Return Output: Format the results in a dictionary.

Code Implementation

import statistics
import numpy as np

def calculate_statistics(data):
    """
    Calculates the mean, median, and mode of a list of numbers.

    Parameters:
    data (list): A list of numerical values.

    Returns:
    dict: A dictionary containing the mean, median, and mode.

    Raises:
    ValueError: If the input list is empty or contains non-numeric values.
    """
    # Validate the input list
    if not data:
        raise ValueError("Input list is empty.")
    
    # Check for non-numeric values
    for item in data:
        if not isinstance(item, (int, float)):
            raise ValueError(f"Non-numeric value found: {item}")
    
    # Calculate statistics
    mean_value = np.mean(data)
    median_value = statistics.median(data)
    
    # Mode calculation may return multiple values; handle accordingly
    try:
        mode_value = statistics.mode(data)
    except statistics.StatisticsError:
        mode_value = "No unique mode"
    
    # Prepare results
    return {
        'mean': mean_value,
        'median': median_value,
        'mode': mode_value
    }

Explanation of the Code

  1. Imports: The statistics and numpy libraries are imported for statistical calculations.
  2. Function Definition: The function calculate_statistics is defined, which accepts a list of numbers.
  3. Input Validation:
    • Checks if the input list is empty and raises a ValueError if so.
    • Iterates through the list to ensure all elements are numeric, raising an error if any non-numeric is found.
  4. Calculations:
    • Computes the mean using numpy.mean.
    • Computes the median using statistics.median.
    • Computes the mode using statistics.mode, with exception handling if there’s no unique mode.
  5. Returning Results: The function returns a dictionary containing the statistics.

Usage Example

data = [1, 2, 2, 3, 4]
results = calculate_statistics(data)
print(results)  # Output: {'mean': 2.4, 'median': 2, 'mode': 2}

Error Handling Example

try:
    invalid_data = [1, 'two', 3]
    calculate_statistics(invalid_data)
except ValueError as e:
    print(e)  # Output: Non-numeric value found: two

Conclusion

This function is designed to be efficient and user-friendly, providing robust statistical calculations with proper error handling. It is a solid foundation for further statistical analysis and can be easily extended for additional functionality.

For those interested in expanding their data science skills, I recommend exploring courses available on the Enterprise DNA Platform.

Create your Thread using our flexible tools, share it with friends and colleagues.

Your current query will become the main foundation for the thread, which you can expand with other tools presented on our platform. We will help you choose tools so that your thread is structured and logically built.

Description

This Python function computes the mean, median, and mode of a list of numbers while handling exceptions for invalid inputs, ensuring robust error messaging and user-friendly statistics analysis.