Numpy_Revision_Exercise
Numpy_Revision_Exercise
1. Create a NumPy array with values from 1 to 10 and print the array.
1. [ 1 2 3 4 5 6 7 8 9 10]
In [8]: # 2. Create two NumPy arrays of size 5 and add them together
arr2_a = np.array([1, 2, 3, 4, 5])
arr2_b = np.array([6, 7, 8, 9, 10])
sum_arr = arr2_a + arr2_b
print("2.", sum_arr)
2. [ 7 9 11 13 15]
3.
[[ 1 2 3 4]
[ 5 6 7 8]
[ 9 10 11 12]]
4. Given the array arr = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9]), slice and print the sub-array containing elements from index 3 to 6.
6. Create a NumPy array of 10 random numbers between 1 and 100. Then, calculate and print the mean and standard deviation of the array.
8. Create a 2D array of shape (3, 3) and add a 1D array [1, 2, 3] to it using broadcasting.
9. Given the array arr = np.array([1, 2, 2, 3, 4, 4, 5]), find and print the unique elements in the array.
10. Create a NumPy array of size 6, and replace all values greater than 3 with the number 100.
11. Create two arrays arr1 = np.array([1, 2, 3]) and arr2 = np.array([4, 5, 6]). Stack them both vertically and horizontally.
12. Create a 4x4 matrix. Using advanced indexing, extract the second column and the third row of the matrix.
13. Create two 3x3 matrices and multiply them using the dot product. Show both the matrix multiplication and the element-wise multiplication.
14. Generate a 3x3 matrix of random integers between 10 and 50, and a 2x5 matrix of random floats with values between 0 and 1.
15. Create a 3x3 matrix and add a scalar value 10 to each element of the matrix using broadcasting.
17. Generate an array of 10 evenly spaced numbers between 0 and 1 using np.linspace, and print the result.
, then compute the sine of these points and plot the result using Matplotlib.
19. Create a NumPy array arr = np.array([1, 2, 8, 4, 3]). Use argmax() to find the index of the maximum value in the array.
20. Create a NumPy array arr = np.array([5, 10, 2, 8, 3]). Use argmin() to find the index of the minimum value in the array.
21. Create a NumPy array arr = np.array([10, 20, 30, 40, 50]). Use Boolean indexing to select all values greater than 25.
22. Create a NumPy array arr = np.array([10, 15, 20, 25, 30, 35, 40]). Use Boolean indexing to select values that are greater than 15 but less than 35.
23. Create a NumPy array arr = np.array([1, 2, 3, 4, 5]). Use np.where() to replace all values greater than 3 with 10, and all other values with 0.
24. Create a NumPy array arr = np.array([12, 15, 18, 20, 22, 25]). Use np.where() to replace values greater than 20 with 10, values less than 15 with -10, and leave others unchanged.
25. Create a 2D array arr = np.array([[5, 10, 15], [2, 20, 30], [7, 1, 4]]). Use np.argmax() and np.argmin() to find the indices of the maximum and minimum values.
In [ ]: # 25. Find indices of max and min values
arr25 = np.array([[5, 10, 15], [2, 20, 30], [7, 1, 4]])
print("25. Max Index:", np.argmax(arr25), "Min Index:", np.argmin(arr25))