Skip to main content
Documents
basicsArray CreationArray OperationsArray Computation & AnalysisLinear AlgebraRandom ProbabilityData Input/Output & Conversion

NumPy Matrix vs Array

NumPy's Linear Algebra module provides a suite of functions to perform operations commonly used in linear algebra, such as matrix multiplication and decomposition. It is essential for numerical computations, data analysis, and scientific research where matrix operations are frequent. The `numpy.linalg` module includes functions for determinants, singular value decomposition, and other complex operations, which are critical for various applications in these fields.

Usage

Linear Algebra in NumPy is employed when performing mathematical operations on matrices and arrays, such as solving linear equations or performing matrix factorizations. It is accessed via the `numpy.linalg` module.

import numpy as np

# Matrix multiplication
result = np.dot(matrix1, matrix2)

In this syntax, `np.dot(matrix1, matrix2)` performs a dot product of two matrices or arrays. For matrix multiplication specifically, `np.matmul(matrix1, matrix2)` or `matrix1 @ matrix2` in Python 3.5+ can be used for more readability.

Examples

1. Basic Matrix Multiplication

import numpy as np

A = np.array([[1, 2], [3, 4]])
B = np.array([[5, 6], [7, 8]])

result = np.dot(A, B)
print(result)

This example demonstrates a simple matrix multiplication between two 2x2 matrices, yielding the product of `A` and `B`. The resulting matrix will also be 2x2.

2. Solving Linear Equations

 import numpy as np

A = np.array([[3, 1], [1, 2]])
b = np.array([9, 8])

x = np.linalg.solve(A, b)
print(x) 

Here, `np.linalg.solve(A, b)` is used to find the solution `x` for the linear equation `Ax = b`. This function is suitable for square systems where `A` is a square matrix, and it returns an array of solutions.

3. Eigenvalues and Eigenvectors

import numpy as np

C = np.array([[4, -2], [1, 1]])

eigenvalues, eigenvectors = np.linalg.eig(C)
print("Eigenvalues:", eigenvalues)
print("Eigenvectors:", eigenvectors)

This example computes the eigenvalues and eigenvectors of matrix `C`, which are crucial for applications like stability analysis and principal component analysis in data science.

Tips and Best Practices

  • Prefer arrays over matrices. NumPy arrays are more versatile and often preferred over the deprecated `np.matrix` class due to better performance and wider functionality.
  • Use `@` for matrix multiplication. In Python 3.5+, use the `@` operator for more readable matrix multiplication, equivalent to `np.matmul`.
  • Ensure compatible dimensions. When performing matrix operations, ensure matrices have compatible dimensions to avoid errors.
  • Leverage NumPy functions. Use built-in NumPy functions for linear algebra tasks, as they are optimized for performance.
  • Check for singular matrices. Before solving linear equations, verify that the matrix is not singular to prevent runtime errors.
  • Utilize `np.linalg.inv` cautiously. Use `np.linalg.inv` to find the inverse of a matrix, but be aware of potential numerical instability. For non-square or singular matrices, consider using `np.linalg.pinv` to compute the pseudo-inverse.