﻿ NumPy: Compute the weighted of a given array - w3resource # 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.

Sample Solution:-

Python Code:

``````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)
```
```

Sample Output:

```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.

What is the difficulty level of this exercise?

Test your Python skills with w3resource's quiz

﻿

## Python: Tips of the Day

Chain Operators

You can chain comparison operators in Python as following: n = 10 result = 1 < n < 20

```distance = 200
checker = 100 < distance < 350

print(checker)
```

Output:

`True`