NumPy: Compute the weighted of a given array
NumPy Statistics: Exercise-6 with Solution
Write a NumPy program to compute the weighted of a given array.
import numpy as np x = np.arange(5) print("\nOriginal array:") print(x) weights = np.arange(1, 6) r1 = np.average(x, weights=weights) r2 = (x*(weights/weights.sum())).sum() assert np.allclose(r1, r2) print("\nWeighted average of the said array:") print(r1)
Original array: [0 1 2 3 4] Weighted average of the said array: 2.6666666666666665
Python Code Editor:
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Previous: Write a NumPy program to compute the median of flattened given array.
Next: Write a NumPy program to compute the mean, standard deviation, and variance of a given array along the second axis.
What is the difficulty level of this exercise?
- New Content published on w3resource :
- Python Numpy exercises
- Python GeoPy Package exercises
- Python Pandas exercises
- Python nltk exercises
- Python BeautifulSoup exercises
- Form Template
- Composer - PHP Package Manager
- PHPUnit - PHP Testing
- Laravel - PHP Framework