0% found this document useful (0 votes)
4 views5 pages

NumPy Random Number Generation Guide

Uploaded by

statusguru4u
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views5 pages

NumPy Random Number Generation Guide

Uploaded by

statusguru4u
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

NUMPY RANDOM

1. Write a NumPy program to generate five random numbers from the


normal distribution.
Expected Output:
[-0.43262625 -1.10836787 1.80791413 0.69287463 -0.53742101]

2. Write a NumPy program to generate six random integers between 10


and 30.
Expected Output:
[20 28 27 17 28 29]

3. Write a NumPy program to create a 3x3x3 array with random values.


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]]]

4. Write a NumPy program to create a 5x5 array with random values and
find the minimum and maximum values.
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

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.
Data Science 8802551718/9990951718 NumPy Random
NUMPY RANDOM

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]]

6. Write a NumPy program to shuffle numbers between 0 and 10


(inclusive).
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]

7. Write a NumPy program to normalize a 3x3 random matrix.


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]
Data Science 8802551718/9990951718 NumPy Random
NUMPY RANDOM

[ 0. 0.71090613 0.08041325]]

8. Write a NumPy program to create a random vector of size 10 and sort


it.
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]

9. Write a NumPy program to find the nearest value from a given value in
an array.
Expected Output:
4.96138509939

10. Write a NumPy program to check two random arrays are equal or
not.
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

11. Write a NumPy program to create random vector of size 15 and


replace the maximum value by -1.
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
Data Science 8802551718/9990951718 NumPy Random
NUMPY RANDOM

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]

12. Write a NumPy program to find point by point distances of a random


vector with shape (10,2) representing coordinates.
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

13. Write a NumPy program to find the most frequent value in an array.
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

14. Write a NumPy program to convert cartesian coordinates to polar


coordinates of a random 10x3 matrix representing cartesian coordinates.
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
Data Science 8802551718/9990951718 NumPy Random
NUMPY RANDOM

0.45822494
1.50634505 1.47389983 0.80818521 0.33001182]

15. Write a NumPy program to find the closest value (to a given scalar) in
an array.
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

16. Write a NumPy program to get the n largest values of an array.


Sample output:
Original array:
[0 1 2 3 4 5 6 7 8 9]
[9]

Data Science 8802551718/9990951718 NumPy Random

You might also like