Menu

gitpiper

weighted_average python Code Snippet in 2024

mathlistintermediate

Last Updated: 22 April 2024

Returns the weighted average of two or more numbers.

  • Use sum() to sum the products of the numbers by their weight and to sum the weights.
  • Use zip() and a list comprehension to iterate over the pairs of values and weights.
def weighted_average(nums, weights): return sum(x * y for x, y in zip(nums, weights)) / sum(weights)
weighted_average([1, 2, 3], [0.6, 0.2, 0.3]) # 1.72727

python snippet similar to weighted_average For You in April 2024

Subscribe to our Newsletter

Subscribe to get resources directly to your inbox. You won't receive any spam! ✌️

© 2024 GitPiper. All rights reserved

Rackpiper Technology Inc

Company

About UsBlogContact

Subscribe to our Newsletter

Subscribe to get resources directly to your inbox. You won't receive any spam! ✌️