MLCrack

    ML MATH & LINEAR ALGEBRA ยท INTERVIEW QUESTION

    What is a matrix and why are matrices important in machine learning?

    ๐ŸŸข beginner ยท Linear Algebra

    Hint: Think about data representation and linear transformations.

    Answer

    A matrix is a 2D array of numbers. In ML, matrices represent:

    1. Datasets: rows = samples, columns = features
    2. Weight matrices in neural networks: y=Wx+by = Wx + b
    3. Covariance matrices: capture feature relationships
    4. Attention matrices in transformers: nร—nn \times n

    Common Vector Operations

    Addition: element-wise [123]+[456]=[579]\begin{bmatrix} 1 \\ 2 \\ 3 \end{bmatrix} + \begin{bmatrix} 4 \\ 5 \\ 6 \end{bmatrix} = \begin{bmatrix} 5 \\ 7 \\ 9 \end{bmatrix}

    Dot product: [1,2,3]โ‹…[4,5,6]=1ร—4+2ร—5+3ร—6=32[1, 2, 3] \cdot [4, 5, 6] = 1{\times}4 + 2{\times}5 + 3{\times}6 = 32

    Magnitude (L2 norm): โˆฅvโˆฅ=12+22+32=14\|\mathbf{v}\| = \sqrt{1^2 + 2^2 + 3^2} = \sqrt{14}

    python
    import numpy as np # Row and column vectors row = np.array([1, 2, 3]) col = np.array([[1], [2], [3]]) # Dot product print(np.dot(row, [4, 5, 6])) # 32 # Magnitude print(np.linalg.norm(row)) # 3.742

    GPUs excel at parallel matrix operations โ€” this is why deep learning is fast on GPUs.