Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Вивчайте Implementing Vectors in Python | Linear Algebra Foundations
Mathematics for Data Science

bookImplementing Vectors in Python

Vectors are used to represent direction, magnitude, and position in many fields — including data science, where they model feature sets, weights, embeddings, and more.

Defining Vectors in Python

In Python, we use NumPy arrays to define 2D vectors like this:

1234567
import numpy as np v1 = np.array([2, 1]) v2 = np.array([1, 3]) print(f'v1 = {v1}') print(f'v2 = {v2}')
copy

These represent the vectors:

v1=(2,1),v2=(1,3)\vec{v}_1 = (2, 1), \quad \vec{v}_2 = (1, 3)

These can now be added, subtracted, or used in dot product and magnitude calculations.

Vector Addition

To compute vector addition:

1234567
import numpy as np v1 = np.array([2, 1]) v2 = np.array([1, 3]) v3 = v1 + v2 print(f'v3 = v1 + v2 = {v3}')
copy

This performs:

(2,1)+(1,3)=(3,4)(2, 1) + (1, 3) = (3, 4)

This matches the rule for vector addition:

a+b=(a1+b1,  a2+b2)\vec{a} + \vec{b} = (a_1 + b_1, \; a_2 + b_2)

Vector Magnitude (Length)

To calculate magnitude in Python:

np.linalg.norm(v)

For vector [3, 4]:

123
import numpy as np print(np.linalg.norm([3, 4])) # 5.0
copy

This uses the formula:

a=a12+a22|\vec{a}| = \sqrt{a_1^2 + a_2^2}

Dot Product

To calculate the dot product:

123
import numpy as np print(np.dot([1, 2], [2, 3]))
copy

Which gives:

[1,2][2,3]=12+23=8[1, 2] \cdot [2, 3] = 1 \cdot 2 + 2 \cdot 3 = 8

Dot product general rule:

ab=a1b1+a2b2\vec{a} \cdot \vec{b} = a_1 b_1 + a_2 b_2

Visualizing Vectors with Matplotlib

We can use quiver() to draw arrows representing vectors. Here's what each one does:

  • Blue: v1\vec{v}_1, drawn from the origin;
  • Green: v2\vec{v}_2, starting at the head of v1\vec{v}_1;
  • Red: resultant vector, drawn from origin to the final tip.

Example:

1234567891011121314151617
import matplotlib.pyplot as plt fig, ax = plt.subplots() # v1 ax.quiver(0, 0, 2, 1, color='blue') # v2 (head-to-tail) ax.quiver(2, 1, 1, 3, color='green') # resultant ax.quiver(0, 0, 3, 4, color='red') plt.xlim(0, 5) plt.ylim(0, 5) plt.grid(True) plt.show()
copy

This produces a triangle that visualizes vector addition.

Quiz

Correct Answer: B


Correct Answer: C

Because:

32+42=5\sqrt{3^2 + 4^2} = 5

Correct Answer: B


Correct Answer: C


Correct Answer: C

1. What is the result of:

[2,1]+[1,3][2, 1] + [1, 3]

2. What is the magnitude of:

(3,4)(3, 4)

3. Which code correctly computes the dot product of [1,2][1,2] and [2,3][2,3]?

4. What does the resultant vector represent in head-to-tail addition?

5. How can you display the magnitude of the resultant vector on the plot?

question mark

What is the result of:

[2,1]+[1,3][2, 1] + [1, 3]

Select the correct answer

question mark

What is the magnitude of:

(3,4)(3, 4)

Select the correct answer

question mark

Which code correctly computes the dot product of [1,2][1,2] and [2,3][2,3]?

Select the correct answer

question mark

What does the resultant vector represent in head-to-tail addition?

Select the correct answer

question mark

How can you display the magnitude of the resultant vector on the plot?

Select the correct answer

Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 4. Розділ 2

Запитати АІ

expand

Запитати АІ

ChatGPT

Запитайте про що завгодно або спробуйте одне із запропонованих запитань, щоб почати наш чат

Suggested prompts:

Can you explain how vector subtraction works in Python?

How do I interpret the plot generated by matplotlib for these vectors?

Can you show how to calculate the angle between two vectors?

Awesome!

Completion rate improved to 1.89

bookImplementing Vectors in Python

Свайпніть щоб показати меню

Vectors are used to represent direction, magnitude, and position in many fields — including data science, where they model feature sets, weights, embeddings, and more.

Defining Vectors in Python

In Python, we use NumPy arrays to define 2D vectors like this:

1234567
import numpy as np v1 = np.array([2, 1]) v2 = np.array([1, 3]) print(f'v1 = {v1}') print(f'v2 = {v2}')
copy

These represent the vectors:

v1=(2,1),v2=(1,3)\vec{v}_1 = (2, 1), \quad \vec{v}_2 = (1, 3)

These can now be added, subtracted, or used in dot product and magnitude calculations.

Vector Addition

To compute vector addition:

1234567
import numpy as np v1 = np.array([2, 1]) v2 = np.array([1, 3]) v3 = v1 + v2 print(f'v3 = v1 + v2 = {v3}')
copy

This performs:

(2,1)+(1,3)=(3,4)(2, 1) + (1, 3) = (3, 4)

This matches the rule for vector addition:

a+b=(a1+b1,  a2+b2)\vec{a} + \vec{b} = (a_1 + b_1, \; a_2 + b_2)

Vector Magnitude (Length)

To calculate magnitude in Python:

np.linalg.norm(v)

For vector [3, 4]:

123
import numpy as np print(np.linalg.norm([3, 4])) # 5.0
copy

This uses the formula:

a=a12+a22|\vec{a}| = \sqrt{a_1^2 + a_2^2}

Dot Product

To calculate the dot product:

123
import numpy as np print(np.dot([1, 2], [2, 3]))
copy

Which gives:

[1,2][2,3]=12+23=8[1, 2] \cdot [2, 3] = 1 \cdot 2 + 2 \cdot 3 = 8

Dot product general rule:

ab=a1b1+a2b2\vec{a} \cdot \vec{b} = a_1 b_1 + a_2 b_2

Visualizing Vectors with Matplotlib

We can use quiver() to draw arrows representing vectors. Here's what each one does:

  • Blue: v1\vec{v}_1, drawn from the origin;
  • Green: v2\vec{v}_2, starting at the head of v1\vec{v}_1;
  • Red: resultant vector, drawn from origin to the final tip.

Example:

1234567891011121314151617
import matplotlib.pyplot as plt fig, ax = plt.subplots() # v1 ax.quiver(0, 0, 2, 1, color='blue') # v2 (head-to-tail) ax.quiver(2, 1, 1, 3, color='green') # resultant ax.quiver(0, 0, 3, 4, color='red') plt.xlim(0, 5) plt.ylim(0, 5) plt.grid(True) plt.show()
copy

This produces a triangle that visualizes vector addition.

Quiz

Correct Answer: B


Correct Answer: C

Because:

32+42=5\sqrt{3^2 + 4^2} = 5

Correct Answer: B


Correct Answer: C


Correct Answer: C

1. What is the result of:

[2,1]+[1,3][2, 1] + [1, 3]

2. What is the magnitude of:

(3,4)(3, 4)

3. Which code correctly computes the dot product of [1,2][1,2] and [2,3][2,3]?

4. What does the resultant vector represent in head-to-tail addition?

5. How can you display the magnitude of the resultant vector on the plot?

question mark

What is the result of:

[2,1]+[1,3][2, 1] + [1, 3]

Select the correct answer

question mark

What is the magnitude of:

(3,4)(3, 4)

Select the correct answer

question mark

Which code correctly computes the dot product of [1,2][1,2] and [2,3][2,3]?

Select the correct answer

question mark

What does the resultant vector represent in head-to-tail addition?

Select the correct answer

question mark

How can you display the magnitude of the resultant vector on the plot?

Select the correct answer

Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 4. Розділ 2
some-alt