What is NumPy: NumPy is a core library for numerical computing in Python. NumPy is also useful in scientific computing, machine learning, and data science.
1. Watch this one-shot tutorial alongside for a better understanding.
x = np.ones((3, 4))
y = np.zeros((3, 4))
print(x)
print(y)
print(type(x[0][0]))
4.6 Technique-5 (np.full())
z = np.full((3,4), 5, dtype = 'f')
print(z)
5. All other essential operations on a NumPy array
# Checking if none of the elements in the array 'x' is zero
x = [1,1,1,2,2,2,0,4]
print(np.all(x))
# Compare two np arrays
x = np.array([3, 5])
y = np.array([2, 5])
print(np.greater(x, y))
print(np.greater_equal(x, y))
print(np.less(x, y))
print(np.less_equal(x, y))
# np.sum() and np.sort()
x = np.array([[0, 1], [2, 3]])
print(x)
print("Sum of all elements:")
print(np.sum(x))
print("Sum of each column:")
print(np.sum(x, axis=0))
print("Sum of each row:")
print(np.sum(x, axis=1))
x = np.array([[3, 2],[1, 0]])
print(x)
print(np.sort(x))
# np.multiply() and np.dot()
# Creating two NumPy arrays 'nums1' and 'nums2' containing 2x3 matrices
nums1 = np.array([[2, 5, 2],[1, 5, 5]])
nums2 = np.array([[5, 3, 4],[3, 2, 5]])
print("Array1:")
print(nums1)
print("Array2:")
print(nums2)
print("\nMultiply arrays of same size element-by-element:")
print(np.multiply(nums1, nums2))
nums1 = np.array([[2, 5, 2],
[1, 5, 5]])
nums2 = np.array([[5, 3],
[3, 2],[1,2]])
print(np.dot(nums1, nums2))
6. NumPy to an Image and Image to a NumPy Array (Conversion)
Q1. Write Python code to flatten a Matrix using NumPy.
Q2. Write Python code to tranpose a matrix using NumPy.
Q3. Write Python code to find the most frequent value in a NumPy array.
Q4. Write Python code to find the number of occurrences of a sequence in a NumPy array.
These questions are for practice, and the corresponding codes and answers have been intentionally omitted. Try solving them on your own first, and if needed, watch the video for the correct answers.