Lesson Introduction

Welcome! Today, we're diving into basic vector operations, which are foundational to your journey in machine learning. Understanding these operations will help you grasp more complex concepts later on. Our goals are to learn vector addition and scalar multiplication. We'll see why these operations are essential, learn how to implement them in Python, and understand their real-world applications.

Vector Addition: Introduction and Example
Vector Addition: Python Code

Here's how we can implement the vector addition in python:

import numpy as np

# Vector addition using numpy
v1 = np.array([1, 2, 3])
v2 = np.array([4, 5, 6])

print("Vector Addition:", v1 + v2)  # Vector Addition: [5 7 9]

Let's break down the code:

  • np.array([1, 2, 3]) and np.array([4, 5, 6]) create numpy arrays for v1 and v2.
  • The expression v1 + v2 performs element-wise addition, resulting in [5, 7, 9].
Scalar Multiplication: Introduction and Example
Scalar Multiplication: Python Code

Here's the Python code for the scalar multiplication:

import numpy as np

# Scalar multiplication using numpy
v1 = np.array([1, 2, 3])
scalar = 2

print("Scalar Multiplication:", scalar * v1)  # Scalar Multiplication: [2 4 6]

Explanation:

  • The scalar 2 is multiplied with each element in the numpy array v1 using the expression scalar * v1.
  • This results in [2*1, 2*2, 2*3], or [2, 4, 6].
Lesson Summary

Congratulations! In this lesson, we covered:

  • Vector Addition: Combining elements from two lists element-wise.
  • Scalar Multiplication: Multiplying each element in a list by a constant value.

These operations are fundamental in machine learning and data science. By understanding and performing them in Python, you are building a solid foundation for more advanced topics.

Next, you'll get hands-on experience by practicing what you've learned. You'll perform vector addition and scalar multiplication using different examples. This practical approach will solidify your understanding and prepare you for more complex vector operations in future lessons. Happy coding!

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