Welcome to this exciting course path! This path focuses on teaching math concepts that are important for machine learning. Note that this path assumes that you have basic knowledge of the python programming language. You can use our Introduction Programming in Python course path to gain or solidify this knowledge.
Welcome to our lesson on the basics of vectors and matrices!
In this lesson, we'll explore what vectors and matrices are, why they're important in machine learning, and how to create and work with them in Python. By the end of this lesson, you'll understand how to define, display, and interpret vectors and matrices. Let's get started!
Imagine you have a list of your favorite ice cream flavors: ["vanilla", "chocolate", "strawberry"]
. A vector is such a list, but with numbers. For example, a vector could be [1, 2, 3]
.
A vector is a list (or array) of numbers arranged in a specific order. It's one-dimensional, meaning it has only one row or one column. If a vector is a row, it is called a row vector. Here is an example of it:
If a vector is a column, it is called a column vector:
They look the same, but some mathematical operations treat row vectors and column vectors differently, so it is vital to differentiate them.
Let's see how we can create a row vector in Python using np.array()
from the NumPy library. NumPy provides advanced linear algebra features that will be useful in the upcoming units of our course.
Python1import numpy as np 2 3# Create a row vector 4vector = np.array([1, 2, 3]) 5 6# Display the vector 7print("Vector:", vector) # Output: Vector: [1 2 3]
This shows a simple row vector with three numbers.
And this is how we represent the column vector using np.array()
:
Python1import numpy as np 2 3# Create a column vector 4vector = np.array([ 5 [1], 6 [2], 7 [3] 8]) 9 10# Display the vector 11print("Vector:", vector) # Output: Vector: [[1] [2] [3]]
Using np.array()
, we specify a 2-dimensional structure for the column vector, but with one item in each row.
Now, imagine you have a class of students, and you want to record their scores in three subjects: Math, Science, and English. You can use a matrix for this. A matrix looks like a grid, with numbers arranged in rows and columns; each number represents a score.
Here is an example matrix:
Each row could represent the scores for a single student, and each column – a separate subject. As we have 3
students and 3
subjects, the matrix is a square one.
Let's take this example further. Imagine there are 5
students in the class. In this case, the corresponding matrix will be rectangular:
A matrix can be of any form. In machine learning, you will mostly deal with large rectangular matrices.
Next, let's create a matrix using np.array()
:
Python1import numpy as np 2 3# Create a matrix 4matrix = np.array([ 5 [1, 2, 3], 6 [4, 5, 6], 7 [7, 8, 9] 8]) 9 10# Display the matrix 11print("Matrix:", matrix) # Output: Matrix: [[1 2 3] [4 5 6] [7 8 9]]
This is a 3x3 matrix, represented with a 2-dimensional NumPy array.
Understanding the dimensions of vectors and matrices is essential for many operations in machine learning.
Let's see how to print the length of a vector:
Python1import numpy as np 2 3# Create a row vector 4row_vector = np.array([1, 2, 3]) 5print("Length of the row vector:", len(row_vector)) 6# Output: Length of the row vector: 3 7 8# Create a column vector 9column_vector = np.array([ 10 [1], 11 [2], 12 [3] 13]) 14print("Length of the column vector:", column_vector.shape[0]) # Output: 3
For np.array()
, the length of a row vector can be found using len()
, and for a column vector, we use the .shape
attribute to get the number of rows.
Here's how to get the number of rows and columns in a matrix:
Python1import numpy as np 2 3# Create a matrix 4matrix = np.array([ 5 [1, 2, 3], 6 [4, 5, 6], 7 [7, 8, 9] 8]) 9 10# Number of rows 11num_rows = matrix.shape[0] 12print("Number of rows:", num_rows) # Output: Number of rows: 3 13 14# Number of columns 15num_columns = matrix.shape[1] 16print("Number of columns:", num_columns) # Output: Number of columns: 3
Using the .shape
attribute of np.array()
, we can easily access both the number of rows and columns in a matrix.
Now you know how to create and display vectors and matrices and determine their dimensions using np.array()
from the NumPy library. This will provide the linear algebra features that are crucial for the upcoming units of our course.
To sum up, we’ve learned the concepts of vectors and matrices and how to create and display them in Python using np.array()
. Vectors are one-dimensional arrays of numbers, while matrices are two-dimensional grids. Understanding these basics is crucial as they are the building blocks for many operations in machine learning.
Now, it's time to put what you’ve learned into practice. You'll get hands-on experience creating, modifying, and displaying vectors and matrices in Python. Let's dive into the exercises to solidify your understanding!