In [4]: import numpy as np
1. Create a NumPy array with values from 1 to 10 and print the array.
In [6]: # 1. Create a NumPy array with values from 1 to 10 and print it
arr1 = np.arange(1, 11)
print("1.", arr1)
1. [ 1 2 3 4 5 6 7 8 9 10]
2. Create two NumPy arrays of size 5 and add them together.
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. Create a 1D array of 12 elements and reshape it into a 3x4 matrix.
In [10]: # 3. Create a 1D array of 12 elements and reshape it into a 3x4 matrix
arr3 = np.arange(1, 13).reshape(3, 4)
print("3.\n", arr3)
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.
In [ ]: # 4. Slice elements from index 3 to 6
arr4 = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9])
print("4.", arr4[3:7])
5. Create a NumPy array of size 5 and multiply each element by 2.
In [ ]: # 5. Multiply each element by 2
arr5 = np.array([1, 2, 3, 4, 5]) * 2
print("5.", arr5)
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.
In [ ]: # 6. Random array, mean and standard deviation
arr6 = np.random.randint(1, 101, 10)
print("6. Array:", arr6, "Mean:", np.mean(arr6), "Std Dev:", np.std(arr6))
7. Create a 2x3 matrix and find its transpose.
In [ ]: # 7. Transpose of a 2x3 matrix
arr7 = np.array([[1, 2, 3], [4, 5, 6]])
print("7.\n", arr7.T)
8. Create a 2D array of shape (3, 3) and add a 1D array [1, 2, 3] to it using broadcasting.
In [ ]: # 8. Add 1D array to 2D using broadcasting
arr8 = np.array([[10, 20, 30], [40, 50, 60], [70, 80, 90]]) + np.array([1, 2, 3])
print("8.\n", arr8)
9. Given the array arr = np.array([1, 2, 2, 3, 4, 4, 5]), find and print the unique elements in the array.
In [ ]: # 9. Unique elements in an array
arr9 = np.array([1, 2, 2, 3, 4, 4, 5])
print("9.", np.unique(arr9))
10. Create a NumPy array of size 6, and replace all values greater than 3 with the number 100.
In [ ]: # 10. Replace values greater than 3 with 100
arr10 = np.array([1, 2, 3, 4, 5])
arr10[arr10 > 3] = 100
print("10.", arr10)
11. Create two arrays arr1 = np.array([1, 2, 3]) and arr2 = np.array([4, 5, 6]). Stack them both vertically and horizontally.
In [ ]: # 11. Stack arrays vertically and horizontally
arr11_a = np.array([1, 2, 3])
arr11_b = np.array([4, 5, 6])
print("11. Vertical:\n", np.vstack((arr11_a, arr11_b)), "\nHorizontal:\n", np.hstack((arr11_a, arr11_b)))
12. Create a 4x4 matrix. Using advanced indexing, extract the second column and the third row of the matrix.
In [ ]: # 12. Extract second column and third row
arr12 = np.arange(1, 17).reshape(4, 4)
print("12. Second column:", arr12[:, 1], "Third row:", arr12[2, :])
13. Create two 3x3 matrices and multiply them using the dot product. Show both the matrix multiplication and the element-wise multiplication.
In [ ]: # 13. Matrix multiplication and element-wise multiplication
arr13_a = np.random.randint(1, 10, (3, 3))
arr13_b = np.random.randint(1, 10, (3, 3))
print("13. Dot Product:\n", np.dot(arr13_a, arr13_b), "\nElement-wise:\n", arr13_a * arr13_b)
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.
In [ ]: # 14. Random matrices
arr14_int = np.random.randint(10, 50, (3, 3))
arr14_float = np.random.rand(2, 5)
print("14. Integer matrix:\n", arr14_int, "\nFloat matrix:\n", arr14_float)
15. Create a 3x3 matrix and add a scalar value 10 to each element of the matrix using broadcasting.
In [ ]: # 15. Add scalar 10 using broadcasting
arr15 = np.arange(9).reshape(3, 3) + 10
print("15.\n", arr15)
16. Standardize a NumPy array to have a mean of 0 and standard deviation of 1.
In [ ]: # 16. Standardize array
arr16 = np.random.randint(1, 100, 10)
standardized = (arr16 - np.mean(arr16)) / np.std(arr16)
print("16.\n", standardized)
17. Generate an array of 10 evenly spaced numbers between 0 and 1 using np.linspace, and print the result.
In [ ]: # 17. Evenly spaced numbers using linspace
arr17 = np.linspace(0, 1, 10)
print("17.\n", arr17)
18. Use np.linspace to create an array of 100 points between 0 and 2𝜋
, then compute the sine of these points and plot the result using Matplotlib.
In [ ]: # 18. Sine plot
x = np.linspace(0, 2 * np.pi, 100)
y = np.sin(x)
plt.plot(x, y)
plt.title("Sine Wave")
plt.show()
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.
In [ ]: # 19. Index of max value
arr19 = np.array([1, 2, 8, 4, 3])
print("19. Index of max:", np.argmax(arr19))
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.
In [ ]: # 20. Index of min value
arr20 = np.array([5, 10, 2, 8, 3])
print("20. Index of min:", np.argmin(arr20))
21. Create a NumPy array arr = np.array([10, 20, 30, 40, 50]). Use Boolean indexing to select all values greater than 25.
In [ ]: # 21. Boolean indexing for values > 25
arr21 = np.array([10, 20, 30, 40, 50])
print("21.", arr21[arr21 > 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.
In [ ]: # 22. Boolean indexing for values >15 and <35
arr22 = np.array([10, 15, 20, 25, 30, 35, 40])
print("22.", arr22[(arr22 > 15) & (arr22 < 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.
In [ ]: # 23. Use np.where() for replacements
arr23 = np.array([1, 2, 3, 4, 5])
print("23.", np.where(arr23 > 3, 10, 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.
In [ ]: # 24. Complex np.where() condition
arr24 = np.array([12, 15, 18, 20, 22, 25])
print("24.", np.where(arr24 > 20, 10, np.where(arr24 < 15, -10, arr24)))
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))