Linear Algebra with NumPy: Dot Products & Matrix Multiplication

Linear Algebra with NumPy: Dot Products & Matrix Multiplication

Linear algebra is fundamental to data science, machine learning, and scientific computing. NumPy provides powerful tools for efficient linear algebra operations. Today, we'll focus on two core concepts: dot products and matrix multiplication.

1. Dot Product (Vector Dot Product)

The dot product (or scalar product) of two vectors is a single number. It's calculated by multiplying corresponding elements and summing the results. Geometrically, it relates to the angle between vectors.

  • 1D Arrays (Vectors):

    Python
    import numpy as np
    
    v1 = np.array([1, 2, 3])
    v2 = np.array([4, 5, 6])
    
    # Using np.dot()
    dot_product = np.dot(v1, v2)
    print("Vector Dot Product (np.dot):", dot_product) # Output: 32 (1*4 + 2*5 + 3*6)
    
    # Using the @ operator (Python 3.5+)
    dot_product_at = v1 @ v2
    print("Vector Dot Product (@ operator):", dot_product_at) # Output: 32
    

2. Matrix Multiplication

Matrix multiplication is a more complex operation where the result is another matrix. It's crucial for transformations, solving systems of equations, and neural networks.

  • Rules for Matrix Multiplication:

    • The number of columns in the first matrix must equal the number of rows in the second matrix.

    • If matrix A is (m x n) and matrix B is (n x p), the resulting matrix C will be (m x p).

    Matrix 1 ShapeMatrix 2 ShapeResult Shape
    (m, n)(n, p)(m, p)
    (2, 3)(3, 2)(2, 2)
    (4, 2)(2, 1)(4, 1)
  • Methods in NumPy:

    1. np.dot(A, B):

      This is a versatile function. For 1D arrays, it's the dot product. For 2D arrays, it performs matrix multiplication.

      Python
      A = np.array([[1, 2],
                    [3, 4]]) # Shape (2, 2)
      B = np.array([[5, 6],
                    [7, 8]]) # Shape (2, 2)
      
      C_dot = np.dot(A, B)
      print("\nMatrix A:\n", A)
      print("Matrix B:\n", B)
      print("Matrix Multiplication (np.dot):\n", C_dot)
      # Output:
      # [[19 22]
      #  [43 50]]
      
    2. np.matmul(A, B):

      Explicitly designed for matrix products. It handles higher-dimensional arrays (tensors) by treating the last two dimensions as matrices and broadcasting over the others. Generally preferred for clear matrix multiplication intent.

      Python
      C_matmul = np.matmul(A, B)
      print("\nMatrix Multiplication (np.matmul):\n", C_matmul)
      # Output (same as np.dot for 2D):
      # [[19 22]
      #  [43 50]]
      
    3. A @ B (The @ operator):

      This is the infix operator for matrix multiplication (introduced in Python 3.5), equivalent to np.matmul(). It's often the most readable for direct matrix multiplication.

      Python
      C_at_operator = A @ B
      print("\nMatrix Multiplication (@ operator):\n", C_at_operator)
      # Output (same again):
      # [[19 22]
      #  [43 50]]
      
  • Matrix-Vector Multiplication:

    When multiplying a matrix by a 1D vector, NumPy implicitly treats the vector as a column vector for multiplication.

    Python
    matrix = np.array([[1, 2, 3],
                       [4, 5, 6]]) # Shape (2, 3)
    vector = np.array([7, 8, 9]) # Shape (3,) - implicitly treated as (3, 1)
    
    result_mv = matrix @ vector
    print("\nMatrix-Vector Multiplication:\n", result_mv)
    # Output: [ 50 122] (1*7 + 2*8 + 3*9 = 7 + 16 + 27 = 50)
    #                 (4*7 + 5*8 + 6*9 = 28 + 40 + 54 = 122)
    

Understanding these operations is crucial for building and working with many numerical models in data science and machine learning.


Useful Video Links


Comments

Popular posts from this blog

Virtual Environments: Keeping Your Data Science Projects Clean and Sane

Python Decorators: Enhancing Your Data Functions with a Dash of Magic