-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy path02_linear_transformations.py
More file actions
168 lines (136 loc) · 3.87 KB
/
02_linear_transformations.py
File metadata and controls
168 lines (136 loc) · 3.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import marimo
__generated_with = "0.15.5"
app = marimo.App()
@app.cell(hide_code=True)
def _(mo):
mo.md(
r"""
# 2. Linear Transformations: Intuition & Examples
Linear transformations are operations that move, rotate, scale, or shear vectors and shapes in space. They are fundamental in machine learning for manipulating data and features.
"""
)
return
@app.cell(hide_code=True)
def _(mo):
mo.md(
r"""
## Example 1: Rotating a Vector 0001F504
- **Original vector:** $v = [2, 1]$
- **Rotation:** 45° counterclockwise
- **Transformation matrix:**
$$ R = egin{bmatrix} os heta & -in heta \ in heta & os heta nd{bmatrix} $$
- **Result:** Vector is rotated in space
"""
)
return
@app.cell
def _():
import numpy as np
import matplotlib.pyplot as plt
v = np.array([2, 1])
theta = np.pi / 4
R = np.array([[np.cos(theta), -np.sin(theta)], [np.sin(theta), np.cos(theta)]])
v_rot = R @ v
plt.figure(figsize=(6,6))
plt.quiver(0, 0, v[0], v[1], angles='xy', scale_units='xy', scale=1, color='r', label='Original')
plt.quiver(0, 0, v_rot[0], v_rot[1], angles='xy', scale_units='xy', scale=1, color='b', label='Rotated')
plt.xlim(-1, 3)
plt.ylim(-1, 3)
plt.grid(True)
plt.legend()
plt.title('Rotation of a Vector')
plt.show()
return np, plt, v
@app.cell(hide_code=True)
def _(mo):
mo.md(
r"""
**Result:** The vector is rotated by 45°.
"""
)
return
@app.cell(hide_code=True)
def _(mo):
mo.md(
r"""
## Example 2: Scaling a Vector 0001F4A1
- **Original vector:** $v = [2, 1]$
- **Scaling matrix:**
$$ S = egin{bmatrix} 2 & 0 \ 0 & 0.5 nd{bmatrix} $$
- **Result:** Vector is stretched in $x$ and compressed in $y$
"""
)
return
@app.cell
def _(np, plt, v):
S = np.array([[2, 0], [0, 0.5]])
v_scale = S @ v
plt.figure(figsize=(6,6))
plt.quiver(0, 0, v[0], v[1], angles='xy', scale_units='xy', scale=1, color='r', label='Original')
plt.quiver(0, 0, v_scale[0], v_scale[1], angles='xy', scale_units='xy', scale=1, color='g', label='Scaled')
plt.xlim(-1, 5)
plt.ylim(-1, 2)
plt.grid(True)
plt.legend()
plt.title('Scaling a Vector')
plt.show()
return
@app.cell(hide_code=True)
def _(mo):
mo.md(
r"""
**Result:** The vector is stretched horizontally and compressed vertically.
"""
)
return
@app.cell(hide_code=True)
def _(mo):
mo.md(
r"""
## Example 3: Shearing a Vector 0001F4A5
- **Original vector:** $v = [2, 1]$
- **Shearing matrix:**
$$ H = egin{bmatrix} 1 & 1.2 \ 0 & 1 nd{bmatrix} $$
- **Result:** Vector is slanted horizontally
"""
)
return
@app.cell
def _(np, plt, v):
H = np.array([[1, 1.2], [0, 1]])
v_shear = H @ v
plt.figure(figsize=(6,6))
plt.quiver(0, 0, v[0], v[1], angles='xy', scale_units='xy', scale=1, color='r', label='Original')
plt.quiver(0, 0, v_shear[0], v_shear[1], angles='xy', scale_units='xy', scale=1, color='m', label='Sheared')
plt.xlim(-1, 5)
plt.ylim(-1, 3)
plt.grid(True)
plt.legend()
plt.title('Shearing a Vector')
plt.show()
return
@app.cell(hide_code=True)
def _(mo):
mo.md(
r"""
**Result:** The vector is slanted horizontally.
"""
)
return
@app.cell(hide_code=True)
def _(mo):
mo.md(
r"""
## Why are Linear Transformations Important in ML?
- They help manipulate and preprocess data
- Used in feature engineering, PCA, neural networks, and more
- Understanding them builds intuition for how ML algorithms work
"""
)
return
@app.cell
def _():
import marimo as mo
return (mo,)
if __name__ == "__main__":
app.run()