In this lesson, we're diving into basic matrix operations used in machine learning. Understanding matrix operations is crucial because they underpin many algorithms in this field.
Today's goal is to learn matrix addition and scalar multiplication. By the end, you'll be able to add two matrices and multiply a matrix by a scalar using Python and NumPy.
Matrix addition involves adding corresponding elements from two matrices. Imagine you have two grids of numbers and you want to create a new grid where each number is the sum of the corresponding numbers from the original grids.
Think of each matrix as a seating chart for two classrooms. Adding the matrices is like finding the total number of students in the same seats in both charts.
To add two matrices, they must have the same dimensions. Here's how you can do it in Python with NumPy:
Python1import numpy as np 2 3# Example matrices 4m1 = np.array([[1, 2, 3], [4, 5, 6]]) 5m2 = np.array([[7, 8, 9], [10, 11, 12]]) 6 7# Adding matrices 8result = m1 + m2 9print("Matrix Addition:\n", result) 10# Output: 11# Matrix Addition: 12# [[ 8 10 12] 13# [14 16 18]]
np.array()
creates NumPy arrays from the provided lists.m1 + m2
adds the corresponding elements of m1
and m2
.This sums elements at the same positions in both matrices to form a new matrix. Notice the \n
at the end of the string in the print statement. As a reminder, it is a special symbol for a new line.
Suppose you manage inventory for two warehouses. Each warehouse has a matrix representing the quantity of different products in different sections. By adding the two matrices, you can find the total quantity of each product across both warehouses.
Python1import numpy as np 2 3# Warehouse 1 inventory 4warehouse1 = np.array([[10, 20, 30], [40, 50, 60]]) 5 6# Warehouse 2 inventory 7warehouse2 = np.array([[5, 15, 25], [35, 45, 55]]) 8 9# Total inventory 10total_inventory = warehouse1 + warehouse2 11print("Total Inventory:\n", total_inventory) 12# Output: 13# Total Inventory: 14# [[ 15 35 55] 15# [ 75 95 115]]
Scalar multiplication involves multiplying every element in a matrix by a single number (scalar). Imagine you have a grid of numbers and want to adjust each number by multiplying it with another number.
It's like you have a collection of prices and want to apply a discount or markup uniformly. The scalar would be the discount or markup percentage.
Here’s how to perform scalar multiplication in Python with NumPy:
Python1import numpy as np 2 3# Example matrix and scalar 4m1 = np.array([[1, 2, 3], [4, 5, 6]]) 5scalar = 2 6 7# Scalar multiplication 8result = scalar * m1 9print("Scalar Matrix Multiplication:\n", result) 10# Output: 11# Scalar Matrix Multiplication: 12# [[ 2 4 6] 13# [ 8 10 12]]
scalar * m1
multiplies each matrix element by the scalar.This multiplies each element in the matrix by the scalar to produce a new matrix.
Suppose you have a matrix representing the monthly salaries of employees in different departments. If you want to give everyone a 10% raise, you can use scalar multiplication with a scalar of 1.10.
Python1import numpy as np 2 3# Original salaries 4salaries = np.array([[3000, 4000, 5000], [6000, 7000, 8000]]) 5 6# Scalar for 10% raise 7raise_factor = 1.10 8 9# Adjusted salaries 10adjusted_salaries = raise_factor * salaries 11print("Adjusted Salaries:\n", adjusted_salaries) 12# Output: 13# Adjusted Salaries: 14# [[3300. 4400. 5500.] 15# [6600. 7700. 8800.]]
We explored the basics of matrix operations, focusing on matrix addition and scalar multiplication. These operations are building blocks for complex tasks in machine learning and data science.
Understanding these operations makes future lessons involving matrices easier to grasp.
Now it's time to move to the practice section. In practice exercises, you'll get hands-on experience with matrix addition and scalar multiplication using NumPy. This will help solidify your understanding of the concepts and prepare you for advanced matrix operations. Enjoy your coding journey!