Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
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 current directory and file's directory:

To get the full path to the directory a Python file is contained in, write this in that file:

import os 
dir_path = os.path.dirname(os.path.realpath(__file__))

(Note that the incantation above won't work if you've already used os.chdir() to change your current working directory, since the value of the __file__ constant is relative to the current working directory and is not changed by an os.chdir() call.)

To get the current working directory use

import os
cwd = os.getcwd()

Documentation references for the modules, constants and functions used above:

  • The os and os.path modules.
  • The __file__ constant
  • os.path.realpath(path) (returns "the canonical path of the specified filename, eliminating any symbolic links encountered in the path")
  • os.path.dirname(path) (returns "the directory name of pathname path")
  • os.getcwd() (returns "a string representing the current working directory")
  • os.chdir(path) ("change the current working directory to path")

Ref: https://bit.ly/3fy0R6m