diff --git a/linear_algebra/02_linear_transformations.py b/linear_algebra/02_linear_transformations.py new file mode 100644 index 0000000..7a190bf --- /dev/null +++ b/linear_algebra/02_linear_transformations.py @@ -0,0 +1,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() diff --git a/linear_algebra/linear_algebra_foundations.py b/linear_algebra/linear_algebra_foundations.py new file mode 100644 index 0000000..ae43b4e --- /dev/null +++ b/linear_algebra/linear_algebra_foundations.py @@ -0,0 +1,185 @@ +import marimo + +__generated_with = "0.15.5" +app = marimo.App() + + +@app.cell(hide_code=True) +def _(mo): + mo.md( + r""" + # Linear Algebra Foundations for Machine Learning + + Welcome! This notebook introduces the essential concepts of linear algebra needed for machine learning, with a focus on visualization and geometric intuition. + """ + ) + return + + +@app.cell(hide_code=True) +def _(mo): + mo.md( + r""" + ## Topics Covered + - Vectors and vector operations + - Matrices and matrix operations + - Visualizing vectors and matrices + - Why these concepts matter in ML + """ + ) + return + + +@app.cell +def _(): + # Import required libraries + import numpy as np + import matplotlib.pyplot as plt + from mpl_toolkits.mplot3d import Axes3D + return np, plt + + +@app.cell(hide_code=True) +def _(mo): + mo.md( + r""" + ## Vectors: Definition and Visualization + A vector is an ordered list of numbers, representing a point or direction in space. + """ + ) + return + + +@app.cell +def _(np, plt): + # Define two vectors in 2D + v1 = np.array([2, 3]) + v2 = np.array([4, 1]) + + # Plot the vectors + plt.figure(figsize=(6,6)) + plt.quiver(0, 0, v1[0], v1[1], angles='xy', scale_units='xy', scale=1, color='r', label='v1') + plt.quiver(0, 0, v2[0], v2[1], angles='xy', scale_units='xy', scale=1, color='b', label='v2') + plt.xlim(-1, 5) + plt.ylim(-1, 5) + plt.grid(True) + plt.legend() + plt.title('2D Vectors') + plt.show() + return v1, v2 + + +@app.cell(hide_code=True) +def _(mo): + mo.md( + r""" + ## Vector Operations + Let's add two vectors and visualize the result. + """ + ) + return + + +@app.cell +def _(plt, v1, v2): + # Vector addition + v_sum = v1 + v2 + + plt.figure(figsize=(6,6)) + plt.quiver(0, 0, v1[0], v1[1], angles='xy', scale_units='xy', scale=1, color='r', label='v1') + plt.quiver(0, 0, v2[0], v2[1], angles='xy', scale_units='xy', scale=1, color='b', label='v2') + plt.quiver(0, 0, v_sum[0], v_sum[1], angles='xy', scale_units='xy', scale=1, color='g', label='v1 + v2') + plt.xlim(-1, 7) + plt.ylim(-1, 7) + plt.grid(True) + plt.legend() + plt.title('Vector Addition') + plt.show() + return + + +@app.cell(hide_code=True) +def _(mo): + mo.md( + r""" + ## Matrices: Definition and Visualization + A matrix is a rectangular array of numbers. In ML, matrices often represent datasets or transformations. + """ + ) + return + + +@app.cell +def _(np): + # Define a matrix + A = np.array([[1, 2], [3, 4]]) + print('Matrix A:') + print(A) + return (A,) + + +@app.cell(hide_code=True) +def _(mo): + mo.md( + r""" + ## Matrix-Vector Multiplication + Matrix multiplication can be seen as a transformation of a vector. + """ + ) + return + + +@app.cell +def _(A, v1): + # Apply matrix A to vector v1 + v1_transformed = A @ v1 + print('A @ v1 =', v1_transformed) + return (v1_transformed,) + + +@app.cell(hide_code=True) +def _(mo): + mo.md( + r""" + ## Visualizing Matrix Transformation + Let's see how matrix A transforms vector v1. + """ + ) + return + + +@app.cell +def _(plt, v1, v1_transformed): + plt.figure(figsize=(6,6)) + plt.quiver(0, 0, v1[0], v1[1], angles='xy', scale_units='xy', scale=1, color='r', label='Original v1') + plt.quiver(0, 0, v1_transformed[0], v1_transformed[1], angles='xy', scale_units='xy', scale=1, color='m', label='Transformed v1') + plt.xlim(-1, 10) + plt.ylim(-1, 10) + plt.grid(True) + plt.legend() + plt.title('Matrix Transformation') + plt.show() + return + + +@app.cell(hide_code=True) +def _(mo): + mo.md( + r""" + ## Why Linear Algebra Matters in ML + - Data is often represented as vectors/matrices + - Transformations (e.g., PCA, neural networks) use matrix operations + - Understanding these basics helps in grasping ML algorithms + """ + ) + return + + +@app.cell +def _(): + import marimo as mo + return (mo,) + + +if __name__ == "__main__": + app.run()