import numpy as np x = np.array([4,3]) x
array([4, 3])
Among the things that come to mind when I think of a matrix or its dimensions is accessing the dimensions of an NumPy array which is actually very straightforward. To do this, we'll use x.shape.
x.shape
(2,)
This brings up that it is a two element array. Now if we had a multidimensional array for example:
# let's define a 2 by 3 array y = np.array([[1,2,3],[4,4,3]]) y
array([[1, 2, 3], [4, 4, 3]])
#I can prove this by going into its shape, y.shape
(2, 3)
An additional manipulation to matrices that is often done in mathematics is the transpose operation whereby we take a matrix. And we switch its rows for columns and its columns for rows, fundamentally flipping the matrix.
y
y.transpose()
array([[1, 4], [2, 4], [3, 3]])
You see now that I have a 3 by 2 NumPy array. And if I'd like to check that, m can always set that equal to something and then have a look at its shape.
z = y.transpose() z.shape
(3, 2)
And indeed, we see that we have ourselves a 3 by 2 matrix.
Addition and subtraction of matrices works just like we've come to it from math.
z
Two matrices may be added or subtracted only if they have the same dimension; that is, they must have the same number of rows and columns. Addition or subtraction is accomplished by adding or subtracting corresponding elements.
What's particularly interesting in the way NumPy handles multiplication. Now if we've got a matrix y and, say, I come up with a second array z, which is y plus three just to give ourselves a second array,
z = y + 3 z
array([[4, 5, 6], [7, 7, 6]])
I can multiply these as follows, z times y. What this performs is elementwise multiplication. Traditionally, NumPy will perform elementwise multiplication.
z * y
array([[ 4, 10, 18], [28, 28, 18]])
However, there will be times when one will want to perform traditional algebraic matrix multiplication. For this, number has built in np.matmul. Now let's recall that to be able to multiply two matrices,
Now let's recall that originally, I had a NumPy array x, which was a 1 by 2 matrix. So I'm going to bring in z, which we know to have precisely this, two rows.
x
Now performing a matrix multiplication would be as simple as np.matmul(x,z) and out comes algebraic matrix multiplication. And as expected, if we're multiplying a 1 by 2 matrix by a 2 by 3 matrix, the results will have one row. And three columns and here we are.
np.matmul(x,z)
array([37, 41, 42])
Shape = (1,3) . (3,1) = (1,) $\begin{bmatrix}a & b & c \end{bmatrix} \begin{bmatrix}x \\ y \\ z\end{bmatrix} = \begin{bmatrix}a.x + b.y + c.z\end{bmatrix}$ Shape = (3,1) . (1,3) = (3,3) $\begin{bmatrix}x \\ y \\ z\end{bmatrix}\begin{bmatrix}a & b & c\end{bmatrix}= \begin{bmatrix}x.a & x.b & x.c \\ y.a & y.b & y.c\\ z.a & z.b & z.c \end{bmatrix}$
More Generally Matrix of shape(x,y) $\times$ matrix of shape(y,z) will have a shape of (x,z)
We've seen all throughout math, sine, cosine, tangent, exponention, you name it. They work wonderfully with NumPy arrays.
np.exp(x)
array([54.59815003, 20.08553692])
np.log(np.exp(x))
array([4., 3.])
np.cos(x)
array([-0.65364362, -0.9899925 ])
np.sin(x)
array([-0.7568025 , 0.14112001])
np.tanh(x)
array([0.9993293 , 0.99505475])
Write a function called operations that takes as input two positive integers h and w, makes two random matrices A and B, of size h x w, and returns A,B, and s, the sum of A and B.
def operations(h,w): """ Takes two inputs, h and w, and makes two Numpy arrays A and B of size h x w, and returns A, B, and s, the sum of A and B. Arg: h - an integer describing the height of A and B w - an integer describing the width of A and B Returns (in this order): A - a randomly-generated h x w Numpy array. B - a randomly-generated h x w Numpy array. s - the sum of A and B. """ A = np.random.random([h,w]) B = np.random.random([h,w]) s = A + B return A,B,s
A,B,s = operations(3,4)
assert(A.shape == B.shape == s.shape)