Implementing 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:
1234567import numpy as np v1 = np.array([2, 1]) v2 = np.array([1, 3]) print(f'v1 = {v1}') print(f'v2 = {v2}')
These represent the vectors:
v1=(2,1),v2=(1,3)These can now be added, subtracted, or used in dot product and magnitude calculations.
Vector Addition
To compute vector addition:
1234567import numpy as np v1 = np.array([2, 1]) v2 = np.array([1, 3]) v3 = v1 + v2 print(f'v3 = v1 + v2 = {v3}')
This performs:
(2,1)+(1,3)=(3,4)This matches the rule for vector addition:
a+b=(a1+b1,a2+b2)Vector Magnitude (Length)
To calculate magnitude in Python:
np.linalg.norm(v)
For vector [3, 4]
:
123import numpy as np print(np.linalg.norm([3, 4])) # 5.0
This uses the formula:
∣a∣=a12+a22Dot Product
To calculate the dot product:
123import numpy as np print(np.dot([1, 2], [2, 3]))
Which gives:
[1,2]⋅[2,3]=1⋅2+2⋅3=8Dot product general rule:
a⋅b=a1b1+a2b2Visualizing Vectors with Matplotlib
We can use quiver()
to draw arrows representing vectors. Here's what each one does:
- Blue: v1, drawn from the origin;
- Green: v2, starting at the head of v1;
- Red: resultant vector, drawn from origin to the final tip.
Example:
1234567891011121314151617import 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()
This produces a triangle that visualizes vector addition.
Quiz
Correct Answer: B
Correct Answer: C
Because:
32+42=5Correct Answer: B
Correct Answer: C
Correct Answer: C
1. What is the result of:
[2,1]+[1,3]2. What is the magnitude of:
(3,4)3. Which code correctly computes the dot product of [1,2] and [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?
Danke für Ihr Feedback!
Fragen Sie AI
Fragen Sie AI
Fragen Sie alles oder probieren Sie eine der vorgeschlagenen Fragen, um unser Gespräch zu beginnen
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
Implementing Vectors in Python
Swipe um das Menü anzuzeigen
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:
1234567import numpy as np v1 = np.array([2, 1]) v2 = np.array([1, 3]) print(f'v1 = {v1}') print(f'v2 = {v2}')
These represent the vectors:
v1=(2,1),v2=(1,3)These can now be added, subtracted, or used in dot product and magnitude calculations.
Vector Addition
To compute vector addition:
1234567import numpy as np v1 = np.array([2, 1]) v2 = np.array([1, 3]) v3 = v1 + v2 print(f'v3 = v1 + v2 = {v3}')
This performs:
(2,1)+(1,3)=(3,4)This matches the rule for vector addition:
a+b=(a1+b1,a2+b2)Vector Magnitude (Length)
To calculate magnitude in Python:
np.linalg.norm(v)
For vector [3, 4]
:
123import numpy as np print(np.linalg.norm([3, 4])) # 5.0
This uses the formula:
∣a∣=a12+a22Dot Product
To calculate the dot product:
123import numpy as np print(np.dot([1, 2], [2, 3]))
Which gives:
[1,2]⋅[2,3]=1⋅2+2⋅3=8Dot product general rule:
a⋅b=a1b1+a2b2Visualizing Vectors with Matplotlib
We can use quiver()
to draw arrows representing vectors. Here's what each one does:
- Blue: v1, drawn from the origin;
- Green: v2, starting at the head of v1;
- Red: resultant vector, drawn from origin to the final tip.
Example:
1234567891011121314151617import 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()
This produces a triangle that visualizes vector addition.
Quiz
Correct Answer: B
Correct Answer: C
Because:
32+42=5Correct Answer: B
Correct Answer: C
Correct Answer: C
1. What is the result of:
[2,1]+[1,3]2. What is the magnitude of:
(3,4)3. Which code correctly computes the dot product of [1,2] and [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?
Danke für Ihr Feedback!