[Day-8]NumPy-Matrices

Advait Joshi
4 min readApr 22, 2024

--

Hello everyone. Today I delved into NumPy Matrices.

You can pass Python lists of lists to create a 2-D array (or “matrix”) to represent them in NumPy.

data = np.array([[1, 2], [3, 4], [5, 6]])
data
#Output :
array([[1, 2],
[3, 4],
[5, 6]])

Indexing and slicing operations are useful when you’re manipulating matrices:

data[0, 1]
#Output :
2

data[1:3]
#Output :
array([[3, 4],
[5, 6]])

data[0:2, 0]
#Output :
array([1, 3])

You can aggregate matrices the same way you aggregated vectors:

data.max()
#Output :
6

data.min()
#Output :
1

data.sum()
#Output :
21

You can aggregate all the values in a matrix and you can aggregate them across columns or rows using the axis parameter. To illustrate this point, let’s look at a slightly modified dataset:

data = np.array([[1, 2], [5, 3], [4, 6]])
data
#Output :
array([[1, 2],
[5, 3],
[4, 6]])

data.max(axis=0)
#Output :
array([5, 6])

data.max(axis=1)
#Output :
array([2, 5, 6])

Once you’ve created your matrices, you can add and multiply them using arithmetic operators if you have two matrices that are the same size.

data = np.array([[1, 2], [3, 4]])
ones = np.array([[1, 1], [1, 1]])
data + ones
#Output :
array([[2, 3],
[4, 5]])

You can do these arithmetic operations on matrices of different sizes, but only if one matrix has only one column or one row. In this case, NumPy will use its broadcast rules for the operation.

data = np.array([[1, 2], [3, 4], [5, 6]])
ones_row = np.array([[1, 1]])
data + ones_row
#Output :
array([[2, 3],
[4, 5],
[6, 7]])

Be aware that when NumPy prints N-dimensional arrays, the last axis is looped over the fastest while the first axis is the slowest. For instance:

>>> np.ones((4, 3, 2))
#Output :
array([[[1., 1.],
[1., 1.],
[1., 1.]],

[[1., 1.],
[1., 1.],
[1., 1.]],

[[1., 1.],
[1., 1.],
[1., 1.]],

[[1., 1.],
[1., 1.],
[1., 1.]]])
  • np.ones: This is a function from the NumPy library that creates an array filled with ones.
  • (4, 3, 2): This is the shape argument passed to np.ones. It specifies the dimensions of the array. In this case, it creates a 3-dimensional array with dimensions 4x3x2.
  • The first dimension has size 4.
  • The second dimension has size 3.
  • The third dimension has size 2.

So, the resulting array has a shape of (4, 3, 2), meaning it has 4 “layers”, each containing 3 rows and 2 columns.

Visualizing the array:

Layer 1:
[
[1, 1],
[1, 1],
[1, 1]
]Layer 2:
[
[1, 1],
[1, 1],
[1, 1]
]Layer 3:
[
[1, 1],
[1, 1],
[1, 1]
]Layer 4:
[
[1, 1],
[1, 1],
[1, 1]
]

Each element of the array is a 1, and the array is organized into 4 layers, each containing 3 rows and 2 columns.

There are often instances where we want NumPy to initialize the values of an array. NumPy offers functions like ones() and zeros(), and the random.Generator class for random number generation for that. All you need to do is pass in the number of elements you want it to generate:

np.ones(3)
#Output :
array([1., 1., 1.])

np.zeros(3)
#Output :
array([0., 0., 0.])

rng = np.random.default_rng() # the simplest way to generate random numbers
rng.random(3)
#Output :
array([0.63696169, 0.26978671, 0.04097352])

You can also use ones(), zeros(), and random() to create a 2D array if you give them a tuple describing the dimensions of the matrix:

np.ones((3, 2))
#Output :
array([[1., 1.],
[1., 1.],
[1., 1.]])

np.zeros((3, 2))
#Output :
array([[0., 0.],
[0., 0.],
[0., 0.]])

rng.random((3, 2))
#Output :
array([[0.01652764, 0.81327024],
[0.91275558, 0.60663578],
[0.72949656, 0.54362499]]) # may vary

This is all that I learned about the different numpy matrices today. My learnings will be documented here on Medium for better understanding. Stay tuned for the next blog.

All The Best People☺️.

Follow me on Linkedin:

www.linkedin.com/in/advaitszone

To read my previous blog “[Day-7]NumPy Array-Operations”:

https://medium.com/@advaitszone/day-7-numpy-array-operations-4d3ab6d2a61e

--

--