Implementing Matrix Transformation in Python
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.
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]Visualizing the Transformations
Using matplotlib
, we plot each vector from the origin, with their coordinates labeled:
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647import numpy as np import 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.
Thanks for your feedback!
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat
Can you explain how the solution to the system of equations is calculated?
How does the scaling and rotation transformation affect the original vector?
Can you walk me through the visualization code and what each part does?
Awesome!
Completion rate improved to 1.96
Implementing Matrix Transformation in Python
Swipe to show menu
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.
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]Visualizing the Transformations
Using matplotlib
, we plot each vector from the origin, with their coordinates labeled:
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647import numpy as np import 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.
Thanks for your feedback!