Every line of 'numpy harmonic mean' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.
58 def harmonic_mean(a, weights=None): 59 if any([x == 0 for x in a]): 60 return 0 61 else: 62 assert weights is None or len(weights) == len(a), 'Weights has length {} which is different from that of the array ({}).'.format(len(weights), len(a)) 63 if weights is None: 64 return len(a) / sum([1/x for x in a]) 65 else: 66 return sum(weights) / sum(w/x for x, w in zip(a, weights))