Implementing Matrix Transformation in Python
Matrices allow us to model, manipulate, and visualize linear systems and geometric transformations.
Step 1: Solving a Linear System
We define a system of equations:
2x+y=5x−y=1This is rewritten as:
123456789import numpy as np A = np.array([[2, 1], # Coefficient matrix [1, -1]]) b = np.array([5, 1]) # Constants (RHS) x_solution = np.linalg.solve(A, b) print(x_solution)
This finds the values of x and y that satisfy both equations.
Why this matters: solving systems of equations is foundational in data science — from fitting linear models to solving optimization constraints.
Step 2: Applying Linear Transformations
We define a vector:
v = np.array([[2], [3]])
Then apply two transformations:
Scaling
We stretch x by 2 and compress y by 0.5:
S = np.array([[2, 0],
[0, 0.5]])
scaled_v = S @ v
This performs:
S⋅v=[2000.5][23]=[41.5]Rotation
We rotate the vector 90° counterclockwise using the rotation matrix:
theta = np.pi / 2
R = np.array([[np.cos(theta), -np.sin(theta)],
[np.sin(theta), np.cos(theta)]])
rotated_v = R @ v
This yields:
R⋅v=[01−10][23]=[−32]Step 3: Visualizing the Transformations
Using matplotlib
, we plot each vector from the origin, with their coordinates labeled:
12345678910111213141516171819202122232425262728293031323334353637383940414243444546import matplotlib.pyplot as plt # Define original vector v = np.array([[2], [3]]) # Scaling S = np.array([[2, 0], [0, 0.5]]) scaled_v = S @ v # Rotation theta = np.pi / 2 R = np.array([[np.cos(theta), -np.sin(theta)], [np.sin(theta), np.cos(theta)]]) rotated_v = R @ v # Vizualization origin = np.zeros(2) fig, ax = plt.subplots(figsize=(6, 6)) # Plot original ax.quiver(*origin, v[0], v[1], angles='xy', scale_units='xy', scale=1, color='blue', label='Original') # Plot scaled ax.quiver(*origin, scaled_v[0], scaled_v[1], angles='xy', scale_units='xy', scale=1, color='green', label='Scaled') # Plot rotated ax.quiver(*origin, rotated_v[0], rotated_v[1], angles='xy', scale_units='xy', scale=1, color='red', label='Rotated') # Label vector tips ax.text(v[0][0]+0.2, v[1][0]+0.2, "(2, 3)", color='blue') ax.text(scaled_v[0][0]+0.2, scaled_v[1][0]+0.2, "(4, 1.5)", color='green') ax.text(rotated_v[0][0]-0.6, rotated_v[1][0]+0.2, "(-3, 2)", color='red') # Draw coordinate axes ax.annotate("", xy=(5, 0), xytext=(0, 0), arrowprops=dict(arrowstyle="->", linewidth=1.5)) ax.annotate("", xy=(0, 5), xytext=(0, 0), arrowprops=dict(arrowstyle="->", linewidth=1.5)) ax.text(5.2, 0, "X", fontsize=12) ax.text(0, 5.2, "Y", fontsize=12) ax.set_xlim(-5, 5) ax.set_ylim(-5, 5) ax.set_aspect('equal') ax.grid(True) ax.legend() plt.show()
Why this matters: data science workflows often include transformations — for example:
- Principal Component Analysis (rotates data);
- Feature normalization (scales axes;
- Dimensionality reduction (projections.
By visualizing vectors and their transformations, we see how matrices literally move and reshape data in space.
1. What does this line define in the code?
2. What is the output of this operation?
3. What is the purpose of this rotation matrix?
Bedankt voor je feedback!
Vraag AI
Vraag AI
Vraag wat u wilt of probeer een van de voorgestelde vragen om onze chat te starten.
Can you explain how the matrix equation relates to the original system of equations?
How does the scaling and rotation affect the original vector?
Can you walk me through the visualization code step by step?
Awesome!
Completion rate improved to 1.89
Implementing Matrix Transformation in Python
Veeg om het menu te tonen
Matrices allow us to model, manipulate, and visualize linear systems and geometric transformations.
Step 1: Solving a Linear System
We define a system of equations:
2x+y=5x−y=1This is rewritten as:
123456789import numpy as np A = np.array([[2, 1], # Coefficient matrix [1, -1]]) b = np.array([5, 1]) # Constants (RHS) x_solution = np.linalg.solve(A, b) print(x_solution)
This finds the values of x and y that satisfy both equations.
Why this matters: solving systems of equations is foundational in data science — from fitting linear models to solving optimization constraints.
Step 2: Applying Linear Transformations
We define a vector:
v = np.array([[2], [3]])
Then apply two transformations:
Scaling
We stretch x by 2 and compress y by 0.5:
S = np.array([[2, 0],
[0, 0.5]])
scaled_v = S @ v
This performs:
S⋅v=[2000.5][23]=[41.5]Rotation
We rotate the vector 90° counterclockwise using the rotation matrix:
theta = np.pi / 2
R = np.array([[np.cos(theta), -np.sin(theta)],
[np.sin(theta), np.cos(theta)]])
rotated_v = R @ v
This yields:
R⋅v=[01−10][23]=[−32]Step 3: Visualizing the Transformations
Using matplotlib
, we plot each vector from the origin, with their coordinates labeled:
12345678910111213141516171819202122232425262728293031323334353637383940414243444546import matplotlib.pyplot as plt # Define original vector v = np.array([[2], [3]]) # Scaling S = np.array([[2, 0], [0, 0.5]]) scaled_v = S @ v # Rotation theta = np.pi / 2 R = np.array([[np.cos(theta), -np.sin(theta)], [np.sin(theta), np.cos(theta)]]) rotated_v = R @ v # Vizualization origin = np.zeros(2) fig, ax = plt.subplots(figsize=(6, 6)) # Plot original ax.quiver(*origin, v[0], v[1], angles='xy', scale_units='xy', scale=1, color='blue', label='Original') # Plot scaled ax.quiver(*origin, scaled_v[0], scaled_v[1], angles='xy', scale_units='xy', scale=1, color='green', label='Scaled') # Plot rotated ax.quiver(*origin, rotated_v[0], rotated_v[1], angles='xy', scale_units='xy', scale=1, color='red', label='Rotated') # Label vector tips ax.text(v[0][0]+0.2, v[1][0]+0.2, "(2, 3)", color='blue') ax.text(scaled_v[0][0]+0.2, scaled_v[1][0]+0.2, "(4, 1.5)", color='green') ax.text(rotated_v[0][0]-0.6, rotated_v[1][0]+0.2, "(-3, 2)", color='red') # Draw coordinate axes ax.annotate("", xy=(5, 0), xytext=(0, 0), arrowprops=dict(arrowstyle="->", linewidth=1.5)) ax.annotate("", xy=(0, 5), xytext=(0, 0), arrowprops=dict(arrowstyle="->", linewidth=1.5)) ax.text(5.2, 0, "X", fontsize=12) ax.text(0, 5.2, "Y", fontsize=12) ax.set_xlim(-5, 5) ax.set_ylim(-5, 5) ax.set_aspect('equal') ax.grid(True) ax.legend() plt.show()
Why this matters: data science workflows often include transformations — for example:
- Principal Component Analysis (rotates data);
- Feature normalization (scales axes;
- Dimensionality reduction (projections.
By visualizing vectors and their transformations, we see how matrices literally move and reshape data in space.
1. What does this line define in the code?
2. What is the output of this operation?
3. What is the purpose of this rotation matrix?
Bedankt voor je feedback!