Lesson Introduction

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!

Concept: Vector
Creating and Displaying a Row Vector in Python

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.

import numpy as np

# Create a row vector
vector = np.array([1, 2, 3])

# Display the vector
print("Vector:", vector)  # Output: Vector: [1 2 3]

This shows a simple row vector with three numbers.

Creating and Displaying a Column Vector in Python

And this is how we represent the column vector using np.array():

import numpy as np

# Create a column vector
vector = np.array([
    [1],
    [2],
    [3]
])

# Display the vector
print("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.

Concept: Matrix
Creating and Displaying a Matrix in Python

Next, let's create a matrix using np.array():

import numpy as np

# Create a matrix
matrix = np.array([
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
])

# Display the matrix
print("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.

Getting Dimensions in Python: Vectors

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:

import numpy as np

# Create a row vector
row_vector = np.array([1, 2, 3])
print("Length of the row vector:", len(row_vector))  
# Output: Length of the row vector: 3

# Create a column vector
column_vector = np.array([
    [1],
    [2],
    [3]
])
print("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.

Getting Dimensions in Python: Matrices

Here's how to get the number of rows and columns in a matrix:

import numpy as np

# Create a matrix
matrix = np.array([
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
])

# Number of rows
num_rows = matrix.shape[0]
print("Number of rows:", num_rows)  # Output: Number of rows: 3

# Number of columns
num_columns = matrix.shape[1]
print("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.

Lesson Summary

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!

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal