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 = [Link](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 = [Link]([1, 2, 3, 4, 5])
arr2_b = [Link]([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 = [Link](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 = [Link]([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 = [Link]([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 = [Link]([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 = [Link](1, 101, 10)
print("6. Array:", arr6, "Mean:", [Link](arr6), "Std Dev:", [Link](arr6))
7. Create a 2x3 matrix and find its transpose.
In [ ]: # 7. Transpose of a 2x3 matrix
arr7 = [Link]([[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 = [Link]([[10, 20, 30], [40, 50, 60], [70, 80, 90]]) + [Link]([1, 2, 3])
print("8.\n", arr8)
9. Given the array arr = [Link]([1, 2, 2, 3, 4, 4, 5]), find and print the unique elements in the array.
In [ ]: # 9. Unique elements in an array
arr9 = [Link]([1, 2, 2, 3, 4, 4, 5])
print("9.", [Link](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 = [Link]([1, 2, 3, 4, 5])
arr10[arr10 > 3] = 100
print("10.", arr10)
11. Create two arrays arr1 = [Link]([1, 2, 3]) and arr2 = [Link]([4, 5, 6]). Stack them both vertically and horizontally.
In [ ]: # 11. Stack arrays vertically and horizontally
arr11_a = [Link]([1, 2, 3])
arr11_b = [Link]([4, 5, 6])
print("11. Vertical:\n", [Link]((arr11_a, arr11_b)), "\nHorizontal:\n", [Link]((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 = [Link](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 = [Link](1, 10, (3, 3))
arr13_b = [Link](1, 10, (3, 3))
print("13. Dot Product:\n", [Link](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 = [Link](10, 50, (3, 3))
arr14_float = [Link](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 = [Link](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 = [Link](1, 100, 10)
standardized = (arr16 - [Link](arr16)) / [Link](arr16)
print("16.\n", standardized)
17. Generate an array of 10 evenly spaced numbers between 0 and 1 using [Link], and print the result.
In [ ]: # 17. Evenly spaced numbers using linspace
arr17 = [Link](0, 1, 10)
print("17.\n", arr17)
18. Use [Link] 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 = [Link](0, 2 * [Link], 100)
y = [Link](x)
[Link](x, y)
[Link]("Sine Wave")
[Link]()
19. Create a NumPy array arr = [Link]([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 = [Link]([1, 2, 8, 4, 3])
print("19. Index of max:", [Link](arr19))
20. Create a NumPy array arr = [Link]([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 = [Link]([5, 10, 2, 8, 3])
print("20. Index of min:", [Link](arr20))
21. Create a NumPy array arr = [Link]([10, 20, 30, 40, 50]). Use Boolean indexing to select all values greater than 25.
In [ ]: # 21. Boolean indexing for values > 25
arr21 = [Link]([10, 20, 30, 40, 50])
print("21.", arr21[arr21 > 25])
22. Create a NumPy array arr = [Link]([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 = [Link]([10, 15, 20, 25, 30, 35, 40])
print("22.", arr22[(arr22 > 15) & (arr22 < 35)])
23. Create a NumPy array arr = [Link]([1, 2, 3, 4, 5]). Use [Link]() to replace all values greater than 3 with 10, and all other values with 0.
In [ ]: # 23. Use [Link]() for replacements
arr23 = [Link]([1, 2, 3, 4, 5])
print("23.", [Link](arr23 > 3, 10, 0))
24. Create a NumPy array arr = [Link]([12, 15, 18, 20, 22, 25]). Use [Link]() to replace values greater than 20 with 10, values less than 15 with -10, and leave others unchanged.
In [ ]: # 24. Complex [Link]() condition
arr24 = [Link]([12, 15, 18, 20, 22, 25])
print("24.", [Link](arr24 > 20, 10, [Link](arr24 < 15, -10, arr24)))
25. Create a 2D array arr = [Link]([[5, 10, 15], [2, 20, 30], [7, 1, 4]]). Use [Link]() and [Link]() to find the indices of the maximum and minimum values.
In [ ]: # 25. Find indices of max and min values
arr25 = [Link]([[5, 10, 15], [2, 20, 30], [7, 1, 4]])
print("25. Max Index:", [Link](arr25), "Min Index:", [Link](arr25))