w3resource

NumPy: Random Exercises, Practice, Solution

NumPy Random [17 exercises with solution]

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

1. Write a NumPy program to generate five random numbers from the normal distribution. Go to the editor
Expected Output:
[-0.43262625 -1.10836787 1.80791413 0.69287463 -0.53742101]
Click me to see the sample solution

2. Write a NumPy program to generate six random integers between 10 and 30. Go to the editor
Expected Output:
[20 28 27 17 28 29]
Click me to see the sample solution

3. Write a NumPy program to create a 3x3x3 array with random values. Go to the editor
Expected Output:
[[[ 0.48941799 0.58722213 0.43453926]
[ 0.94497547 0.81081709 0.1510409 ]
[ 0.66657127 0.29494755 0.48047144]]
[[ 0.02287253 0.95232614 0.32264936]
[ 0.67009741 0.25458304 0.16290913]
[ 0.15520198 0.86826529 0.9679322 ]]
[[ 0.13503103 0.02042211 0.24683897]
[ 0.97852158 0.22374748 0.10798856]
[ 0.62032646 0.5893892 0.16958144]]]
Click me to see the sample solution

4. Write a NumPy program to create a 5x5 array with random values and find the minimum and maximum values. Go to the editor
Expected Output:
Original Array:
[[ 0.96336355 0.12339131 0.20295196 0.37243578 0.88105252]
[ 0.93228246 0.67470158 0.38103235 0.32242645 0.40610231]
[ 0.3113495 0.31688 0.79189089 0.08676434 0.60829874]
[ 0.30360149 0.94316317 0.98142491 0.77222542 0.51532195]
[ 0.97392305 0.16669609 0.81377917 0.2165645 0.00121611]]
Minimum and Maximum Values:
0.00121610921757 0.981424910368
Click me to see the sample solution

5. Write a NumPy program to create a random 10x4 array and extract the first five rows of the array and store them into a variable. Go to the editor
Sample Output:
Original array:
[[ 0.38593391 0.52823544 0.8994567 0.22097238]
[ 0.16639229 0.74964167 0.58102198 0.2811601 ]
[ 0.56447627 0.42575759 0.71297527 0.91099347]
[ 0.00261548 0.0064798 0.66096109 0.54514293]
[ 0.7216008 0.95815426 0.53370551 0.28116107]
[ 0.16252081 0.26191659 0.40883164 0.60653848]
[ 0.55934457 0.37814126 0.63287808 0.01856616]
[ 0.03788236 0.22705078 0.82024426 0.83019741]
[ 0.31140166 0.43926341 0.38685152 0.92402934]
[ 0.00581032 0.83925377 0.95246879 0.28570894]]
First 5 rows of the above array:
[[ 0.38593391 0.52823544 0.8994567 0.22097238]
[ 0.16639229 0.74964167 0.58102198 0.2811601 ]
[ 0.56447627 0.42575759 0.71297527 0.91099347]
[ 0.00261548 0.0064798 0.66096109 0.54514293]
[ 0.7216008 0.95815426 0.53370551 0.28116107]]

Click me to see the sample solution

6. Write a NumPy program to shuffle numbers between 0 and 10 (inclusive). Go to the editor
Sample Output:
[5 7 9 0 2 3 1 6 8 4]
Same result using permutation():
[6 7 4 5 8 2 3 9 0 1]
Click me to see the sample solution

7. Write a NumPy program to normalize a 3x3 random matrix. Go to the editor
Sample output:
Original Array:
[[ 0.87311805 0.96651849 0.98078621]
[ 0.26407141 0.46784012 0.69947627]
[ 0.20013296 0.75510414 0.26290783]]
After normalization:
[[ 0.86207941 0.98172337 1. ]
[ 0.08190378 0.34292711 0.63964803]
[ 0. 0.71090613 0.08041325]]
Click me to see the sample solution

8. Write a NumPy program to create a random vector of size 10 and sort it. Go to the editor
Expected Output:
Original array:
[ 0.73123073 0.67714015 0.95615347 0.4759837 0.88789818 0.6910404 2
0.59996415 0.26144489 0.51618644 0.89943882]
Sorted array:
[ 0.26144489 0.4759837 0.51618644 0.59996415 0.67714015 0.6910404 2
0.73123073 0.88789818 0.89943882 0.95615347]
Click me to see the sample solution

9. Write a NumPy program to find the nearest value from a given value in an array. Go to the editor
Expected Output:
4.96138509939
Click me to see the sample solution

10. Write a NumPy program to check two random arrays are equal or not. Go to the editor
Sample Output:
First array:
[1 0 1 0 1 1]
Second array:
[0 0 1 1 1 0]
Test above two arrays are equal or not!
False
Click me to see the sample solution

11. Write a NumPy program to create random vector of size 15 and replace the maximum value by -1. Go to the editor
Sample output:
Original array:
[ 0.34807512 0.76714463 0.40242311 0.5634299 0.84972926 0.92247789
0.93791571 0.5127047 0.50796265 0.50074454 0.26067194 0.07207825
0.04927934 0.95309433 0.14043974]
Maximum value replaced by -1:
[ 0.34807512 0.76714463 0.40242311 0.5634299 0.84972926 0.92247789
0.93791571 0.5127047 0.50796265 0.50074454 0.26067194 0.07207825
0.04927934 -1. 0.14043974]
Click me to see the sample solution

12. Write a NumPy program to find point by point distances of a random vector with shape (10,2) representing coordinates. Go to the editor
Sample output:
[[ 0. 0.09871078 0.42100075 0.75597269 0.52281832 0.13721998
0.1761711 0.28689498 0.42061575 0.61315509]
[ 0.09871078 0. 0.43978557 0.71086596 0.59696144 0.14701023
0.26602812 0.19254215 0.36762701 0.68776127]
.....
[ 0.42061575 0.36762701 0.30691429 0.34395028 0.63326713 0.29974614
0.47787697 0.39922329 0. 0.70954707]
[ 0.61315509 0.68776127 0.4097455 0.85714768 0.09080178 0.55976699
Click me to see the sample solution

13. Write a NumPy program to find the most frequent value in an array. Go to the editor
Sample Output:
Original array:
[6 9 5 1 7 5 1 0 1 5 5 0 8 9 0 7 0 7 6 5 1 1 9 5 3 8 7 9 6 3 4 5 9 7 2 7 0
2 2 6]
Most frequent value in the above array:
5
Click me to see the sample solution

14. Write a NumPy program to convert cartesian coordinates to polar coordinates of a random 10x2 matrix representing cartesian coordinates. Go to the editor
Expected Output:
[ 0.89225122 0.68774813 0.20392039 1.22093243 1.24435921 1.00358852
0.37378547 0.8534585 0.31999648 0.567451 ]
[ 1.02751197 1.26964967 0.02567519 0.85386412 0.73152767 0.45822494
1.50634505 1.47389983 0.80818521 0.33001182]
Click me to see the sample solution

15. Write a NumPy program to find the closest value (to a given scalar) in an array. Go to the editor
Original array:
[ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99]
Value to compare:
34.99062268928913
35
Click me to see the sample solution

16. Write a NumPy program to get the n largest values of an array. Go to the editor
Sample output:
Original array:
[0 1 2 3 4 5 6 7 8 9]
[9]
Click me to see the sample solution

17. Write a NumPy program to create a three-dimension array with shape (300,400,5) and set to a variable. Fill the array elements with values using unsigned integer (0 to 255). Go to the editor
Sample output:
[[[215 42 224 219 43]
[166 69 15 133 255]
[105 95 54 37 201]
...
[240 22 66 232 132]
[ 13 85 53 220 170]
[249 62 221 146 69]]
[[ 73 79 148 132 164]
[ 3 93 98 138 200]
[174 34 31 208 130]
...
[143 20 219 154 85]
[219 190 170 227 246]
[ 39 14 127 230 158]]]
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