Matrix Operations (NumPy)
Matrix operations in NumPy include multiplication with @, transposition with .T, inversion and solving via np.linalg, and the crucial distinction between element-wise (*) and matrix (@) products.
NumPy
The single most important rule is that * is element-wise while @ is matrix multiplication. A * B multiplies matching cells and needs identical or broadcastable shapes; A @ B follows linear-algebra rules, multiplying rows by columns, and needs the inner dimensions to match.
Around those two, NumPy provides .T to transpose, np.linalg.inv to invert, np.linalg.solve to solve systems, np.eye to build identity matrices, and np.trace for the diagonal sum. Most of the heavy numerical routines live in the linalg submodule.
These operations are the backbone of machine learning, graphics, and scientific computing, and NumPy runs them through optimized BLAS/LAPACK libraries so they are fast even on large matrices.
Syntax
A @ B | A * B | A.T | np.linalg.inv(A)Examples
Python — * is element-wise; @ is matrix product.
import numpy as np
A = np.array([[1, 2], [3, 4]])
print(A * A)
print(A @ A)Output:
[[ 1 4]
[ 9 16]]
[[ 7 10]
[15 22]]