When we multiply two arrays, it actually means multiplying each of there corresponding elements. Very often, we want to perform a scalar or dot product. The dot product is an algebraic operation that takes two coordinate vectors of equal size and returns a single number.
The dot product of two vectors is calculated by multiplying corresponding entries in each vector and adding up all of those products. For example, given two vectors: a = [a1, a2, . . ., an] and b = [b1, b2, . . ., bn].
The dot product of these two vectors is: a1b1 + a2b2 + . . . + anbn.
In NumPy, dot product is accomplished using the dot() function.
Program 1:
import numpy as np x = np.array([2,3]) y = np.array([4,2]) print("The dot product of x and y is:", np.dot(x,y))
Output:
The dot product of x and y is: 14
Dot products also work on rank 2 arrays. If we perform a dot product of two rank 2 arrays, it is equivalent to the matrix multiplication.
Program 2:
x = np.array([[1,2,3],[4,5,6]]) y = np.array([[7,8],[9,10], [11,12]]) print("The dot product of x and y is:") print(np.dot(x,y))
Output:
The dot product of x and y is: [[ 58 64] [139 154]]
The first result, 58, is derived from the dot product of the first row of the first array and the first column of the second array: 1 × 7 + 2 × 9 + 3 × 11 = 58. The second result of 64 is obtained by the dot product of the first row of the first array and the second column of the second array: 1 × 8 + 2 × 10 + 3 × 12 = 64. And so on….
NumPy provides another class in addition to arrays (ndarray); that’s matrix. The matrix class is a subclass of the ndarray, and it is basically identical to the ndarray with one notable exception: A matrix is strictly two-dimensional, while an ndarray can be multidimensional.
Creating a matrix object is similar to creating a NumPy array.
Program 3:
import numpy as np a = np.matrix([[1,2],[4,5]]) b = np.matrix([[7,8],[2,3]]) print("The matrix a is:") print(a) print("The matrix b is:") print(b)
Output:
The matrix a is: [[1 2] [4 5]] The matrix b is: [[7 8] [2 3]]
We can also convert a NumPy array to a matrix using the asmatrix() function.
Program 4:
import numpy as np a = np.array([[1,2],[4,5]]) b = np.array([[7,8],[2,3]]) c = np.asmatrix(a) d = np.asmatrix(b) print("The matrix c is:") print(c) print("The matrix d is:") print(d)
Output:
The matrix c is: [[1 2] [4 5]] The matrix d is: [[7 8] [2 3]]
Another important difference between an ndarray and a matrix occurs when we perform multiplications on them. When multiplying two ndarray objects, the result is the element-by-element multiplication. On the other hand, when multiplying two matrix objects, the result is the dot product (equivalent to the np.dot() function).
Program 5:
import numpy as np x1 = np.array([[1,2],[4,5]]) y1 = np.array([[7,8],[2,3]]) print("The product of x1 and y1 is:") # element - by - element multiplication print(x1 * y1) x2 = np.matrix([[1,2],[4,5]]) y2 = np.matrix([[7,8],[2,3]]) print("The product of x1 and y1 is:") # dot product; same as np.dot() print(x2 * y2)
Output:
The product of x1 and y1 is: [[ 7 16] [ 8 15]] The product of x1 and y1 is: [[11 14] [38 47]]
Very often, when dealing with numerical data, there is a need to find the cumulative sum of numbers in a NumPy array. We can call the cumsum() function to get the cumulative sum of the array elements.
Program 6:
import numpy as np A = np.array([(1,2,3),(4,5,6), (7,8,9)]) print("The value of array A is:") print(A) print("The cumulative sum of all the elements in the array is: ") print(A.cumsum())
Output:
The value of array A is: [[1 2 3] [4 5 6] [7 8 9]] The cumulative sum of all the elements in the array is: [ 1 3 6 10 15 21 28 36 45]
In this case, the cumsum() function returns a rank 1 array containing the cumulative sum of all of the elements in the A array. The cumsum() function also takes in an optional argument as axis. Specifying an axis of 0 indicates that we want to get the cumulative sum of each column.
Program 7:
import numpy as np A = np.array([(1,2,3),(4,5,6), (7,8,9)]) print("The value of array A is:") print(A) print("The cumulative sum of all the elements in each column of the array is: ") print(A.cumsum(axis=0))
Output:
The value of array A is: [[1 2 3] [4 5 6] [7 8 9]] The cumulative sum of all the elements in each column of the array is: [[ 1 2 3] [ 5 7 9] [12 15 18]]
Specifying an axis of 1 indicates that we want to get the cumulative sum of each row.
Program 8:
import numpy as np A = np.array([(1,2,3),(4,5,6), (7,8,9)]) print("The value of array A is:") print(A) print("The cumulative sum of all the elements in each row of the array is: ") print(A.cumsum(axis=1))
Output:
The value of array A is: [[1 2 3] [4 5 6] [7 8 9]] The cumulative sum of all the elements in each row of the array is: [[ 1 3 6] [ 4 9 15] [ 7 15 24]]