Exercise 2.
Beginner Level NumPy Exercises
1. Create a 1D array with values ranging from 0 to 9.
2. Convert a 1D array to a 2D array with 2 rows.
3. Extract all odd numbers from an array of 1-10.
4. Replace all odd numbers in an array of 1-10 with -1.
5. Convert a 1D array to a boolean array where all positive values become True.
6. Replace all even numbers in a 1D array with their negative.
7. Find the indices of non-zero elements from [1,2,0,0,4,0].
8. Create a 3x3 identity matrix.
9. Reshape a 1D array to a 2D array with 5 rows and 2 columns.
10. Stack two arrays vertically.
11. Find the index of the maximum value in a 1D array.
12. Find the mean of each row in a 2D array.
13. Calculate the exponential of all elements in a 1D array.
14. Concatenate two 1D arrays.
15. Create a 2D array with random values and sort each row.
16. Replace all negative values in an array with 0.
17. Calculate the square root of each element in a 1D array.
18. Convert the data type of an array to float.
19. Output a sequence of equally gapped 5 numbers in the range 0 to 100 (both
inclusive).
20. Output a matrix (numpy array) of dimension 2-by-3 with each and every value
equal to 5.
21. Output a 5-by-5 array of random integers between 0 (inclusive) and 10
(exclusive).
22. Output a 3-by-3 array of random numbers following normal distribution.
23. Output the transpose of a matrix (as numpy array).
24. Calculate the sine of an array of angles (in radians) using NumPy.
25. Create a one-dimensional NumPy array of the numbers from 10 to 100, counting
by 10.
26. Create a ten-element NumPy array object of all zeros.
27. Create a ten-element array of random integers between 1 and 5 (inclusive).
28. Create a two-dimensional, 3 x 4 array (three arrays of four elements each) with
random numbers from 1 to 10.
29. Calculate the cumulative sum of elements in a 1D array.
30. Calculate the standard deviation of each column in a 2D array.
31. Create a 10x10 array with random values and find the minimum and maximum
values.
32. For this array: arr = [Link]([1, 2, 3, 4, 5, 6, 7])
a. Slice elements from index 1 to index 5 from the following array.
b. Slice elements from index 4 to the end of the array.
c. Slice elements from the beginning to index 4 (not included).
d. Slice from the index 3 from the end to index 1 from the end.
33. For the following array:
array([[ 1, 2, 3, 4, 5],
[ 6, 7, 8, 9, 10],
[11, 12, 13, 14, 15],
[16, 17, 18, 19, 20]])
a. Write a statement that prints the first row.
b. Write an expression to print the last row.
c. What does print(four_by_five[2,3]) display?
d. How could you display the first column?
e. Write an expression to return the last two columns of the middle two rows.