-
Notifications
You must be signed in to change notification settings - Fork 0
Open
Description
Analyse-This/Descriptive_Analysis.py
Line 169 in a1e5e1b
| harmonic_mean_value = stats.hmean(data) |
Current Code:
def harmonic_mean(data):
if (data <= 0).any().any():
st.warning("⚠️ Harmonic mean is not defined for data with negative or zero values.")
else:
harmonic_mean_value = stats.hmean(data)
st.write("Harmonic Mean:", harmonic_mean_value)
Suggested Optimization:
def harmonic_mean(data):
if (data <= 0).any().any():
st.warning("⚠️ Harmonic mean is not defined for data with negative or zero values.")
else:
harmonic_mean_value = len(data) / np.sum(1.0 / data)
st.write("Harmonic Mean:", harmonic_mean_value)
The function scipy.stats.hmean is designed for robustness, with internal checks for multidimensional inputs, type casting, and invalid values.This custom implementation avoids the overhead of additional checks, leading to better runtime performance and lower memory usage, especially for large arrays. It is mathematically equivalent in this safe context and significantly faster.
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels