Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Basic Linear Algebra with NumPy | Math with NumPy
Ultimate NumPy
course content

Course Content

Ultimate NumPy

Ultimate NumPy

1. NumPy Basics
2. Indexing and Slicing
3. Commonly used NumPy Functions
4. Math with NumPy

book
Basic Linear Algebra with NumPy

Linear algebra is a fundamental branch of mathematics that plays a crucial role in various fields, including machine learning, deep learning, and data analysis.

Vectors and Matrices

In linear algebra, a vector is an ordered set of values. 1D NumPy arrays can efficiently represent vectors. A matrix is a two-dimensional array of numbers, which can be represented by a 2D array in NumPy.

We have already covered vector and matrix addition and subtraction, as well as scalar multiplication, in the "Basic Mathematical Operations" chapter. Here, we will focus on other operations.

Transposition

Transposition is an operation that flips a matrix over its diagonal. In other words, it converts the rows of the matrix into columns and the columns into rows.

You can transpose a matrix using the .T attribute of a NumPy array:

12345
import numpy as np matrix = np.array([[1, 2, 3], [4, 5, 6]]) # Transposing a matrix transposed_matrix = matrix.T print(transposed_matrix)
copy

Dot Product

The dot product is perhaps the most commonly used linear algebra operation in machine and deep learning. The dot product of two vectors (which must have an equal number of elements) is the sum of their element-wise products. The result is a scalar:

Matrix Multiplication

Matrix multiplication is defined only if the number of columns in the first matrix is equal to the number of rows in the second matrix. The resulting matrix will have the same number of rows as the first matrix and the same number of columns as the second matrix.

As you can see, every element of the resulting matrix is the dot product of two vectors. The row number of the element corresponds to the number of the row vector in the first matrix, and the column number corresponds to the number of the column vector in the second matrix.

The number of columns in the first matrix must be equal to the number of rows in the second matrix, as the dot product requires the two vectors to have the same number of elements.

Dot Product and Matrix Multiplication in NumPy

NumPy provides the dot() function for both the dot product and matrix multiplication. This function takes two arrays as its arguments.

However, you can also use the @ operator between two arrays to achieve the same results.

12345678910111213
import numpy as np vector_1 = np.array([1, 2, 3]) vector_2 = np.array([4, 5, 6]) # Dot product using the dot() function print(np.dot(vector_1, vector_2)) # Dot product using the @ operator print(vector_1 @ vector_2) matrix_1 = np.array([[1, 2, 3], [4, 5, 6]]) matrix_2 = np.array([[7, 10], [8, 11], [9, 12]]) # Matrix multiplication using the dot() function print(np.dot(matrix_1, matrix_2)) # Matrix multiplication using the @ operator print(matrix_1 @ matrix_2)
copy

If the right argument in matrix multiplication is a vector (1D array), NumPy treats it as a matrix where the last dimension is 1. For example, when multiplying a 6x4 matrix by a vector with 4 elements, the vector is considered a 4x1 matrix.

If the left argument in matrix multiplication is a vector, NumPy treats it as a matrix where the first dimension is 1. For instance, when multiplying a vector with 4 elements by a 4x6 matrix, the vector is treated as a 1x4 matrix.

The picture below shows the structure of the exam_scores and coefficients arrays used in the task:

Task
test

Swipe to show code editor

You are working with the exam_scores array, which contains simulated exam scores of three students (each row represents a student) across three subjects (each column represents a subject).

  1. Multiply the scores of each subject exam by the respective coefficient.

  2. Add the resulting scores for each student to calculate their final score.

  3. Compute the dot product between exam_scores and coefficients.

This will give you the final scores for all students based on the weighted contributions of their subject scores.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 4. Chapter 4
toggle bottom row

book
Basic Linear Algebra with NumPy

Linear algebra is a fundamental branch of mathematics that plays a crucial role in various fields, including machine learning, deep learning, and data analysis.

Vectors and Matrices

In linear algebra, a vector is an ordered set of values. 1D NumPy arrays can efficiently represent vectors. A matrix is a two-dimensional array of numbers, which can be represented by a 2D array in NumPy.

We have already covered vector and matrix addition and subtraction, as well as scalar multiplication, in the "Basic Mathematical Operations" chapter. Here, we will focus on other operations.

Transposition

Transposition is an operation that flips a matrix over its diagonal. In other words, it converts the rows of the matrix into columns and the columns into rows.

You can transpose a matrix using the .T attribute of a NumPy array:

12345
import numpy as np matrix = np.array([[1, 2, 3], [4, 5, 6]]) # Transposing a matrix transposed_matrix = matrix.T print(transposed_matrix)
copy

Dot Product

The dot product is perhaps the most commonly used linear algebra operation in machine and deep learning. The dot product of two vectors (which must have an equal number of elements) is the sum of their element-wise products. The result is a scalar:

Matrix Multiplication

Matrix multiplication is defined only if the number of columns in the first matrix is equal to the number of rows in the second matrix. The resulting matrix will have the same number of rows as the first matrix and the same number of columns as the second matrix.

As you can see, every element of the resulting matrix is the dot product of two vectors. The row number of the element corresponds to the number of the row vector in the first matrix, and the column number corresponds to the number of the column vector in the second matrix.

The number of columns in the first matrix must be equal to the number of rows in the second matrix, as the dot product requires the two vectors to have the same number of elements.

Dot Product and Matrix Multiplication in NumPy

NumPy provides the dot() function for both the dot product and matrix multiplication. This function takes two arrays as its arguments.

However, you can also use the @ operator between two arrays to achieve the same results.

12345678910111213
import numpy as np vector_1 = np.array([1, 2, 3]) vector_2 = np.array([4, 5, 6]) # Dot product using the dot() function print(np.dot(vector_1, vector_2)) # Dot product using the @ operator print(vector_1 @ vector_2) matrix_1 = np.array([[1, 2, 3], [4, 5, 6]]) matrix_2 = np.array([[7, 10], [8, 11], [9, 12]]) # Matrix multiplication using the dot() function print(np.dot(matrix_1, matrix_2)) # Matrix multiplication using the @ operator print(matrix_1 @ matrix_2)
copy

If the right argument in matrix multiplication is a vector (1D array), NumPy treats it as a matrix where the last dimension is 1. For example, when multiplying a 6x4 matrix by a vector with 4 elements, the vector is considered a 4x1 matrix.

If the left argument in matrix multiplication is a vector, NumPy treats it as a matrix where the first dimension is 1. For instance, when multiplying a vector with 4 elements by a 4x6 matrix, the vector is treated as a 1x4 matrix.

The picture below shows the structure of the exam_scores and coefficients arrays used in the task:

Task
test

Swipe to show code editor

You are working with the exam_scores array, which contains simulated exam scores of three students (each row represents a student) across three subjects (each column represents a subject).

  1. Multiply the scores of each subject exam by the respective coefficient.

  2. Add the resulting scores for each student to calculate their final score.

  3. Compute the dot product between exam_scores and coefficients.

This will give you the final scores for all students based on the weighted contributions of their subject scores.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 4. Chapter 4
Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
We're sorry to hear that something went wrong. What happened?
some-alt