Mastering NumPy: 100 Exercises with solutions for Python numerical computing
Welcome to w3resource's 100 NumPy exercises collection! This comprehensive set of exercises is designed to help you master the fundamentals of NumPy, a powerful numerical computing library in Python. Whether you're a beginner or an experienced user looking to improve your skills, these exercises cover a wide range of topics. They provide practical challenges to enhance your NumPy understanding.
Exercise 1:
Create a 1D array with values ranging from 0 to 9.
[0 1 2 3 4 5 6 7 8 9]
Exercise 2:
Convert a 1D array to a 2D array with 2 rows.
[[0 1 2 3 4] [5 6 7 8 9]]
Exercise 3:
Multiply a 5x3 matrix by a 3x2 matrix.
[[1.05787846 0.32439392] [2.18241234 0.68182548] [1.23629853 0.39596044] [0.42973189 0.12403945] [1.08039539 0.36356378]]
Exercise 4:
Extract all odd numbers from an array of 1-10.
[1 3 5 7 9]
Exercise 5:
Replace all odd numbers in an array of 1-10 with -1.
[-1 2 -1 4 -1 6 -1 8 -1]
Exercise 6:
Convert a 1D array to a boolean array where all positive values become True.
[False True False False True]
Exercise 7:
Replace all even numbers in a 1D array with their negative.
[ 1 -2 3 -4 5 -6 7 -8 9]
Exercise 8:
Create a random 3x3 matrix and normalize it.
[[ 1.07755282 -0.27940552 -1.57739216] [ 1.53962723 1.25274094 -0.97454418] [-0.30801978 -0.26192698 -0.46863236]]
Exercise 9:
Calculate the sum of the diagonal elements of a 3x3 matrix.
1.0183501284750802
Exercise 10:
Find the indices of non-zero elements from [1,2,0,0,4,0].
(array([0, 1, 4], dtype=int64),)
Exercise 11:
Reverse a 1D array (first element becomes the last).
[9 8 7 6 5 4 3 2 1 0]
Exercise 12:
Create a 3x3 identity matrix.
[[1. 0. 0.] [0. 1. 0.] [0. 0. 1.]]
Exercise 13:
Reshape a 1D array to a 2D array with 5 rows and 2 columns.
[[0 1] [2 3] [4 5] [6 7] [8 9]]
Exercise 14:
Stack two arrays vertically.
[[1 2 3] [4 5 6]]
Exercise 15:
Get the common items between two arrays.
[3 4 5]
Exercise 16:
Create a 5x5 matrix with row values ranging from 0 to 4.
[[0. 1. 2. 3. 4.] [0. 1. 2. 3. 4.] [0. 1. 2. 3. 4.] [0. 1. 2. 3. 4.] [0. 1. 2. 3. 4.]]
Exercise 17:
Find the index of the maximum value in a 1D array.
3
Exercise 18:
Normalize the values in a 1D array between 0 and 1.
[0. 0.375 1. 0.125 0.75 ]
Exercise 19:
Calculate the dot product of two arrays.
32
Exercise 20:
Count the number of elements in an array within a specific range.
4
Exercise 21:
Find the mean of each row in a 2D array.
[0.437043 0.73541944 0.45005375]
Exercise 22:
Create a random 4x4 matrix and extract the diagonal elements.
[0.3968107 0.3355669 0.91924761 0.907174 ]
Exercise 23:
Count the number of occurrences of a specific value in an array.
3
Exercise 24:
Replace all values in a 1D array with the mean of the array.
[30 30 30 30 30]
Exercise 25:
Find the indices of the maximum and minimum values in a 1D array.
Index of max: 2 Index of min: 3
Exercise 26:
Create a 2D array with 1 on the border and 0 inside.
[[1. 1. 1. 1. 1.] [1. 0. 0. 0. 1.] [1. 0. 0. 0. 1.] [1. 0. 0. 0. 1.] [1. 1. 1. 1. 1.]]
Exercise 27:
Find the unique values and their counts in a 1D array.
Unique values: [1 2 3 4 5 6] Counts: [2 2 1 2 1 1]
Exercise 28:
Create a 3x3 matrix with values ranging from 0 to 8.
[[0 1 2] [3 4 5] [6 7 8]]
Exercise 29:
Calculate the exponential of all elements in a 1D array.
[ 2.71828183 7.3890561 20.08553692 54.59815003 148.4131591 ]
Exercise 30:
Swap two rows in a 2D array.
[[0.64447186 0.98641154 0.7336092 0.79829912] [0.00753743 0.65414365 0.74147161 0.14819561] [0.53158903 0.89859906 0.75709264 0.49165449]]
Exercise 31:
Create a random 3x3 matrix and replace all values greater than 0.5 with 1 and all others with 0.
[[1. 0. 1.] [0. 0. 0.] [0. 0. 0.]]
Exercise 32:
Find the indices of the top N maximum values in a 1D array.
[4 3]
Exercise 33:
Calculate the mean of each column in a 2D array.
[0.54904302 0.4902671 0.42925161]
Exercise 34:
Normalize the values in each column of a 2D array.
[[-0.46748521 -0.77417852 -0.84330049] [ 1.0967056 1.71309044 0.71160507] [ 0.7764295 -0.58168559 1.24351291] [-1.4056499 -0.35722632 -1.11181749]]
Exercise 35:
Concatenate two 1D arrays.
[1 2 3 4 5 6]
Exercise 36:
Create a 2D array with random values and sort each row.
[[0.10858953 0.71557663 0.7986983 0.90525131] [0.318373 0.50887498 0.51900254 0.7860126 ] [0.06242782 0.12665803 0.12884579 0.1440853 ]]
Exercise 37:
Compute the mean squared error between two arrays.
1.0
Exercise 38:
Replace all negative values in an array with 0.
[0 2 0 4 0]
Exercise 39:
Find the 5th and 95th percentiles of an array.
5th Percentile: 0.04785597751800983 95th Percentile: 0.8708086577975086
Exercise 40:
Create a random 2x2 matrix and compute its determinant.
0.0019446951056262907
Exercise 41:
Count the number of elements in an array that are greater than the mean.
2
Exercise 42:
Calculate the square root of each element in a 1D array.
[2. 3. 4. 5.]
Exercise 43:
Create a 3x3 matrix and compute the matrix square root.
[[0.62607741 0.64326801 0.24778017] [1.35236384 1.43972006 0.56587005] [0.617024 0.71519995 0.29469311]]
Exercise 44:
Convert the data type of an array to float.
[1. 2. 3. 4.]
Exercise 45:
Calculate the element-wise absolute values of an array.
[1 2 3 4]
Exercise 46:
Find the indices where elements of two arrays match.
(array([1, 3, 4], dtype=int64),)
Exercise 47:
Calculate the cumulative sum of elements in a 1D array.
[ 1 3 6 10 15]
Exercise 48:
Compute the inverse of a 2x2 matrix.
[[ 1.22685384 -4.14864181] [-0.2704788 6.62123806]]
Exercise 49:
Count the number of non-zero elements in a 2D array.
4
Exercise 50:
Create a 2D array and replace all nan values with 0.
[[1. 0. 3.] [4. 5. 0.] [7. 8. 9.]]
Exercise 51:
Find the correlation coefficient between two arrays.
0.9999999999999999
Exercise 52:
Create a 1D array and remove all duplicate values.
[1 2 3 4 5]
Exercise 53:
Compute the element-wise product of two arrays.
[ 4 10 18]
Exercise 54:
Calculate the standard deviation of each column in a 2D array.
[0.22702366 0.27411548 0.25568707]
Exercise 55:
Create a 2D array and set all values above a certain threshold to that threshold.
[[0.7 0.54918234 0.7 0.01893358] [0.09114833 0.00268936 0.13766009 0.28160436] [0.40448374 0.7 0.7 0.30830747]]
Exercise 56:
Create a random 5x5 matrix and replace the maximum value by -1.
[[ 0.45916565 0.79856137 0.68998918 0.04439129 0.84814684] [ 0.4442666 0.2661443 0.83980951 0.71727557 0.30334519] [ 0.47332068 0.50437988 0.51222628 0.65334221 0.21664521] [ 0.7511065 0.77837283 0.29019334 0.82695944 0.41608473] [ 0.23775068 0.07539172 -1. 0.14166163 0.56071446]]
Exercise 57:
Convert a 1D array of Fahrenheit temperatures to Celsius.
[ 0. 20. 37.77777778 100. ]
Exercise 58:
Compute the outer product of two arrays.
[[ 4 5 6] [ 8 10 12] [12 15 18]]
Exercise 59:
Create a 1D array with 10 equidistant values between 0 and 1.
[0. 0.11111111 0.22222222 0.33333333 0.44444444 0.55555556 0.66666667 0.77777778 0.88888889 1. ]
Exercise 60:
Compute the cross product of two 3D arrays.
[-3 6 -3]
Exercise 61:
Calculate the percentile along a specific axis of a 2D array.
[0.73954718 0.63159296 0.49097014]
Exercise 62:
Create a 1D array and add a border of 0s around it.
[0 1 2 3 4 0]
Exercise 63:
Compute the histogram of a 1D array.
Histogram: [2 3 4] Bin edges: [1 2 3 4]
Exercise 64:
Create a 2D array with random values and normalize each row.
[[0.61881278 0.58433001 0.52500398] [0.50152886 0.39533452 0.76953195] [0.18399814 0.89840062 0.39877439] [0.76636647 0.25223155 0.59081442]]
Exercise 65:
Create a random 2D array and sort it by the second column.
[[0.28214407 0.30234856 0.56159219 0.34651641] [0.72579465 0.64605243 0.14824549 0.6364958 ] [0.4852539 0.96161675 0.41756529 0.7079494 ]]
Exercise 66:
Calculate the determinant of a 3x3 matrix.
0.09550080558000806
Exercise 67:
Calculate the element-wise exponentiation of a 1D array.
[ 7.3890561 20.08553692 54.59815003]
Exercise 68:
Calculate the Frobenius norm of a 2D array.
1.8692600488600242
Exercise 69:
Create a 2D array with random values and replace the maximum value with the minimum.
[[0.29383093 0.01637657 0.7794734 0.01637657] [0.76911564 0.22745882 0.32986131 0.84500937] [0.94922056 0.54607467 0.80921816 0.28474546]]
Exercise 70:
Compute the matrix multiplication of two 2D arrays.
[[0.99119942 0.85513185 0.57664501 0.31851386 0.84601174] [1.38306551 1.16773145 0.83001893 0.68922723 1.12666608] [1.72827308 1.44430652 1.03782659 1.28492828 1.24906297]]
Exercise 71:
Create a 1D array and set the values between 10 and 20 to 0.
[ 5 0 0 0 25]
Exercise 72:
Compute the inverse hyperbolic sine of each element in a 1D array.
[0.88137359 1.44363548 1.81844646 2.09471255]
Exercise 73:
Compute the Kronecker product of two arrays.
[3 4 6 8]
Exercise 74:
Calculate the mean absolute deviation of a 1D array.
1.2
Exercise 75:
Create a 3x3 matrix and set all values above the main diagonal to zero.
[[0.7344708 0. 0. ] [0.91154103 0.58004909 0. ] [0.53953411 0.73231191 0.8315308 ]]
Exercise 76:
Count the number of occurrences of each unique value in a 1D array.
Unique values: [1 2 3 4] Counts: [1 2 3 1]
Exercise 77:
Compute the cumulative product of elements along a given axis in a 2D array.
[[0.65005294 0.01951926 0.25636043 0.30961964] [0.28738887 0.00883133 0.24993819 0.06504888] [0.21895124 0.00590692 0.0116853 0.03512077]]
Exercise 78:
Round elements of a 1D array to the nearest integer.
[1. 3. 4. 5.]
Exercise 79:
Create a 1D array and append a new element to the end.
[1 2 3 4]
Exercise 80:
Calculate the element-wise absolute difference between two arrays.
[1 2 7 9 3]
Exercise 81:
Create a 2D array with random values and replace the maximum value in each row with -1.
[[ 0.14015869 0.13279371 -1. 0.4665563 ] [ 0.73795369 0.311774 -1. 0.05087541] [ 0.6516183 -1. 0.30233177 0.48674956]]
Exercise 82:
Normalize the columns of a 2D array to have a sum of 1.
[[0.00548299 0.49881449 0.16102029 0.23643045] [0.58935257 0.27548946 0.54365728 0.57390056] [0.40516443 0.22569604 0.29532244 0.18966899]]
Exercise 83:
Find the indices of the top N minimum values in a 1D array.
[3 1]
Exercise 84:
Convert the elements of a 1D array to strings.
['1' '2' '3' '4']
Exercise 85:
Compute the percentile rank of each element in a 1D array.
[ 20. 40. 60. 80. 100.]
Exercise 86:
Create a 1D array and shuffle its elements randomly.
[4 2 5 1 3]
Exercise 87:
Check if all elements in a 1D array are non-zero.
True
Exercise 88:
Find the indices of the maximum value in each row of a 2D array.
[1 3 2]
Exercise 89:
Create a 2D array and replace all nan values with the mean of the array.
[[1. 5.28571429 3. ] [4. 5. 5.28571429] [7. 8. 9. ]]
Exercise 90:
Calculate the mean of each row in a 2D array ignoring nan values.
[1.5 5. 8. ]
Exercise 91:
Compute the sum of diagonal elements in a 2D array.
1.375162416021255
Exercise 92:
Convert radians to degrees for each element in a 1D array.
[ 90. 180. 270.]
Exercise 93:
Calculate the pairwise Euclidean distance between two arrays.
5.196152422706632
Exercise 94:
Create a 1D array and set the values between the 25th and 75th percentile to 0.
[10 0 0 0 50]
Exercise 95:
Calculate the element-wise square of the difference between two arrays.
[9 9 9]
Exercise 96:
Replace all even numbers in a 1D array with the next odd number.
[ 3 5 9 13 15]
Exercise 97:
Create a 2D array and normalize each column by its range..
[[0. 1. 0. 1. ] [0.38748809 0. 1. 0. ] [1. 0.39378719 0.83348354 0.78448847]]
Exercise 98:
Compute the cumulative sum of elements along a given axis in a 2D array.
[[0.23623571 0.76181793 1.40183102 2.02358543] [0.89415619 1.38540147 1.46977913 1.99923603] [0.3626984 0.94484957 1.24671758 2.19056585]]
Exercise 99:
Check if any element in a 1D array is non-zero.
True
Exercise 100:
Create a 2D array with random integers and replace all values greater than a certain threshold with that threshold.
[[72 55 56 26] [ 8 16 54 75] [13 74 36 75]]
Python-Numpy 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 Programming skills with w3resource's quiz.