Prompt
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
- Import Necessary Libraries: We will use
statistics
for calculating the median and mode, andnumpy
for the mean. - Define the Function: Create a function called
calculate_statistics
. - Input Validation: Validate the input for:
- Non-numeric values.
- Empty lists.
- Calculations: Compute mean, median, and mode.
- 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
- Imports: The
statistics
andnumpy
libraries are imported for statistical calculations. - Function Definition: The function
calculate_statistics
is defined, which accepts a list of numbers. - 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.
- Checks if the input list is empty and raises a
- 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.
- Computes the mean using
- 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.
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.