w3resource

NumPy: Statistics Exercises, Practice, Solution

NumPy Statistics [14 exercises with solution]

[An editor is available at the bottom of the page to write and execute the scripts.]

NumPy Statistics

1. Write a Python program to find the maximum and minimum value of a given flattened array. Go to the editor
Expected Output:
Original flattened array:
[[0 1]
[2 3]]
Maximum value of the above flattened array:
3
Minimum value of the above flattened array:
0
Click me to see the sample solution

2. Write a NumPy program to get the minimum and maximum value of a given array along the second axis. Go to the editor
Expected Output:
Original array:
[[0 1]
[2 3]]
Maximum value along the second axis:
[1 3]
Minimum value along the second axis:
[0 2]
Click me to see the sample solution

3. Write a NumPy program to calculate the difference between the maximum and the minimum values of a given array along the second axis. Go to the editor
Expected Output:
Original array:
[[ 0 1 2 3 4 5]
[ 6 7 8 9 10 11]]
Difference between the maximum and the minimum values of the said array:
[5 5]
Click me to see the sample solution

4. Write a NumPy program to compute the 80th percentile for all elements in a given array along the second axis. Go to the editor
Expected Output:
Original array:
[1.0, 2.0, 3.0, 4.0]
Largest integer smaller or equal to the division of the inputs:
[ 0. 1. 2. 2.]
Click me to see the sample solution

5. Write a NumPy program to compute the median of flattened given array. Go to the editor
Note: First array elements raised to powers from second array
Expected Output:
Original array:
[[ 0 1 2 3 4 5]
[ 6 7 8 9 10 11]]
Median of said array:
5.5
Click me to see the sample solution

6. Write a NumPy program to compute the weighted of a given array. Go to the editor
Sample Output:
Original array:
[0 1 2 3 4]
Weighted average of the said array:
2.6666666666666665
Click me to see the sample solution

7. Write a NumPy program to compute the mean, standard deviation, and variance of a given array along the second axis. Go to the editor
Sample output:
Original array:
[0 1 2 3 4 5]
Mean: 2.5
std: 1
variance: 2.9166666666666665
Click me to see the sample solution

8. Write a NumPy program to compute the covariance matrix of two given arrays. Go to the editor
Sample Output:
Original array1:
[0 1 2]
Original array1:
[2 1 0]
Covariance matrix of the said arrays:
[[ 1. -1.]
[-1. 1.]]
Click me to see the sample solution

9. Write a NumPy program to compute cross-correlation of two given arrays. Go to the editor
Sample Output:
Original array1:
[0 1 3]
Original array1:
[2 4 5]
Cross-correlation of the said arrays:
[[2.33333333 2.16666667]
[2.16666667 2.33333333]]
Click me to see the sample solution

10. Write a NumPy program to compute pearson product-moment correlation coefficients of two given arrays. Go to the editor
Sample Output:
Original array1:
[0 1 3]
Original array1:
[2 4 5]
Pearson product-moment correlation coefficients of the said arrays:
[[1. 0.92857143]
[0.92857143 1. ]]
Click me to see the sample solution

11. Write a NumPy program to test element-wise of a given array for finiteness (not infinity or not Not a Number), positive or negative infinity, for NaN, for NaT (not a time), for negative infinity, for positive infinity. Go to the editor
Sample output:
Test element-wise for finiteness (not infinity or not Not a Number):
True
True
False
Test element-wise for positive or negative infinity:
True
False
True
Test element-wise for NaN:
[ True False False]
Test element-wise for NaT (not a time):
[ True False]
Test element-wise for negative infinity:
[1 0 0]
Test element-wise for positive infinity:
[0 0 1]
Click me to see the sample solution

12. Write a Python NumPy program to compute the weighted average along the specified axis of a given flattened array. Go to the editor
From Wikipedia: The weighted arithmetic mean is similar to an ordinary arithmetic mean (the most common type of average), except that instead of each of the data points contributing equally to the final average, some data points contribute more than others. The notion of weighted mean plays a role in descriptive statistics and also occurs in a more general form in several other areas of mathematics.
Sample output:
Original flattened array:
[[0 1 2]
[3 4 5]
[6 7 8]]
Weighted average along the specified axis of the above flattened array:
[1.2 4.2 7.2]
Click me to see the sample solution

13. Write a Python program to count number of occurrences of each value in a given array of non-negative integers. Go to the editor
Note: bincount() function count number of occurrences of each value in an array of non-negative integers in the range of the array between the minimum and maximum values including the values that did not occur.
Sample Output:
Original array:
[0, 1, 6, 1, 4, 1, 2, 2, 7]
Number of occurrences of each value in array:
[1 3 2 0 1 0 1 1]
Click me to see the sample solution

14. Write a NumPy program to compute the histogram of nums against the bins. Go to the editor
Sample Output:
nums: [0.5 0.7 1. 1.2 1.3 2.1]
bins: [0 1 2 3]
Result: (array([2, 3, 1], dtype=int64), array([0, 1, 2, 3]))
NumPy Statistics: Compute the histogram of nums against the bins
Click me to see the sample solution

Python-Numpy Code Editor:

More to Come !

Do not submit any solution of the above exercises at here, if you want to contribute go to the appropriate exercise page.

Test your Python skills with w3resource's quiz



Python: Tips of the Day

Find the index of an item in a list?

Given a list ["foo", "bar", "baz"] and an item in the list "bar", how do I get its index (1) in Python?

>>> ["foo", "bar", "baz"].index("bar")
1

Caveats follow

Note that while this is perhaps the cleanest way to answer the question as asked, index is a rather weak component of the list API, and I can't remember the last time I used it in anger. It's been pointed out to me in the comments that because this answer is heavily referenced, it should be made more complete. Some caveats about list.index follow. It is probably worth initially taking a look at the documentation for it:

list.index(x[, start[, end]])

Linear time-complexity in list length

An index call checks every element of the list in order, until it finds a match. If your list is long, and you don't know roughly where in the list it occurs, this search could become a bottleneck. In that case, you should consider a different data structure. Note that if you know roughly where to find the match, you can give index a hint. For instance, in this snippet, l.index(999_999, 999_990, 1_000_000) is roughly five orders of magnitude faster than straight l.index(999_999), because the former only has to search 10 entries, while the latter searches a million:

>>> import timeit
>>> timeit.timeit('l.index(999_999)', setup='l = list(range(0, 1_000_000))', number=1000)
9.356267921015387
>>> timeit.timeit('l.index(999_999, 999_990, 1_000_000)', setup='l = list(range(0, 1_000_000))', number=1000)
0.0004404920036904514

Only returns the index of the first match to its argument

A call to index searches through the list in order until it finds a match, and stops there. If you expect to need indices of more matches, you should use a list comprehension, or generator expression.

>>> [1, 1].index(1)
0
>>> [i for i, e in enumerate([1, 2, 1]) if e == 1]
[0, 2]
>>> g = (i for i, e in enumerate([1, 2, 1]) if e == 1)
>>> next(g)
0
>>> next(g)
2

Most places where I once would have used index, I now use a list comprehension or generator expression because they're more generalizable. So if you're considering reaching for index, take a look at these excellent Python features.

Throws if element not present in list

A call to index results in a ValueError if the item's not present.

>>> [1, 1].index(2)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: 2 is not in list

If the item might not be present in the list, you should either

  • Check for it first with item in my_list (clean, readable approach), or
  • Wrap the index call in a try/except block which catches ValueError (probably faster, at least when the list to search is long, and the item is usually present.)

Ref: https://bit.ly/2ALwXwe