0Pricing
Pandas & NumPy Academy · 课时

使用 np.matmul 和 @ 进行矩阵乘法

使用 np.matmul 和 @ 运算符正确地进行矩阵相乘,并区分逐元素乘法 * 与真正的矩阵乘法

使用 np.matmul 和 @ 进行矩阵乘法 是 CoddyKit 上的免费 Pandas & NumPy Academy 课时。 这是第 1 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Pandas & NumPy Academy 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Pandas & NumPy Academy 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

Why Matrix Multiplication Matters

Matrix multiplication is at the heart of data science and machine learning. It underpins neural networks, dimensionality reduction, and linear transformations. In Python, NumPy gives you two clean ways to multiply matrices: the np.matmul() function and the @ operator introduced in Python 3.5. Both are fast, vectorized, and far more efficient than nested Python loops.

The Rules of Matrix Multiplication

For two matrices A and B, the product A @ B is valid only when the number of columns in A equals the number of rows in B. If A is shape (m, n) and B is shape (n, k), the result is shape (m, k). This rule is called the inner-dimension constraint and applies regardless of which NumPy function you use.

import numpy as np
A = np.array([[1, 2], [3, 4]])   # shape (2, 2)
B = np.array([[5, 6], [7, 8]])   # shape (2, 2)
C = A @ B
print(C)  # [[19 22] [43 50]]

np.matmul vs the @ Operator

np.matmul(A, B) and A @ B produce identical results for 2-D arrays. The @ operator is simply syntactic sugar — it calls __matmul__ under the hood, which NumPy implements with the same C-level BLAS routine. Use @ in everyday code for readability; use np.matmul when you want to pass the function as a callable or handle edge cases explicitly.

import numpy as np
A = np.array([[1, 2], [3, 4]])
B = np.array([[5, 6], [7, 8]])
print(np.matmul(A, B))   # same as A @ B
print(A @ B)             # same result

Distinguishing @ from * and np.dot

NumPy has three multiplication functions that are easy to confuse. A * B is element-wise multiplication (Hadamard product). np.dot(A, B) behaves like np.matmul for 2-D arrays but has different semantics for higher dimensions. A @ B is strictly matrix multiplication and follows the mathematical definition precisely — prefer it over np.dot for matrix work.

import numpy as np
A = np.array([[1, 2], [3, 4]])
B = np.array([[5, 6], [7, 8]])
print('element-wise:', A * B)    # [[5 12] [21 32]]
print('matmul:', A @ B)          # [[19 22] [43 50]]

Matrix Multiplication Is Not Commutative

Unlike scalar multiplication, matrix multiplication is not commutative: A @ B generally differs from B @ A. The shapes may not even be compatible in both directions. This matters when writing transformations — the order of operations changes the result. Always keep track of which matrix is on the left versus the right.

import numpy as np
A = np.array([[1, 2], [3, 4]])
B = np.array([[0, 1], [1, 0]])
print('A @ B:', A @ B)   # [[2 1] [4 3]]
print('B @ A:', B @ A)   # [[3 4] [1 2]]  -- different!

Batched Matrix Multiplication

When your arrays have more than two dimensions, np.matmul and @ treat the extra leading dimensions as batch dimensions and multiply matrices in parallel. For example, an array of shape (32, 3, 4) multiplied by one of shape (32, 4, 5) produces a result of shape (32, 3, 5) — 32 independent 3×4 @ 4×5 multiplications at once. This is how deep learning frameworks process mini-batches efficiently.

import numpy as np
batch_A = np.random.rand(32, 3, 4)
batch_B = np.random.rand(32, 4, 5)
result = batch_A @ batch_B
print(result.shape)  # (32, 3, 5)

Matrix-Vector Multiplication

A common special case is multiplying a matrix by a vector. If A is shape (m, n) and v is a 1-D array of length n, then A @ v produces a 1-D array of length m. NumPy handles the shape alignment automatically — you do not need to reshape v into a column vector. This pattern appears everywhere: applying weights in linear regression, projecting features, and computing dot products for each row simultaneously.

import numpy as np
A = np.array([[1, 2, 3], [4, 5, 6]])  # (2, 3)
v = np.array([1, 0, -1])              # (3,)
print(A @ v)   # [1-3, 4-6] = [-2, -2]

Performance: @ vs Python Loops

NumPy matrix multiplication calls optimized BLAS/LAPACK routines and runs in compiled C code. A pure Python nested loop implementing the same operation is typically 100–1000× slower for large matrices. When you need repeated matrix products in a data pipeline — feature transformations, PCA projections, weight updates — always use @ or np.matmul and never implement the inner loop yourself.

import numpy as np, time
A = np.random.rand(500, 500)
B = np.random.rand(500, 500)
t0 = time.time()
C = A @ B
print(f'NumPy: {(time.time()-t0)*1000:.1f} ms')  # single-digit ms

Chaining Multiple Matrix Products

You can chain multiple @ operations in one expression: A @ B @ C evaluates left to right. NumPy does not optimize the multiplication order automatically (unlike MATLAB's mtimes), so if the matrices have very different sizes, the order can matter for speed. For three or more matrices consider using np.linalg.multi_dot([A, B, C]), which automatically selects the most efficient pairing.

import numpy as np
A = np.random.rand(100, 20)
B = np.random.rand(20, 50)
C = np.random.rand(50, 10)
result = np.linalg.multi_dot([A, B, C])
print(result.shape)  # (100, 10)

Common Shape Errors and How to Fix Them

The most common error with matrix multiplication is a shape mismatch: ValueError: matmul: Input operand 1 has a mismatch in its core dimension 0. The fix is to check shapes with A.shape and ensure the inner dimensions agree. If you have a row vector of shape (n,) that needs to be treated as a column vector (n, 1), use v.reshape(-1, 1). If you need the transpose, use A.T.

import numpy as np
A = np.ones((3, 4))
B = np.ones((3, 4))  # wrong -- inner dims 4 != 3
try:
    A @ B
except ValueError as e:
    print('Error:', e)
print('Fix:', (A @ B.T).shape)  # (3, 3)

Practical Example: Feature Transformation

A typical data science use case: you have a dataset X of shape (n_samples, n_features) and a weight matrix W of shape (n_features, n_outputs). The matrix product X @ W applies a linear transformation to every sample simultaneously, producing shape (n_samples, n_outputs). This is the core computation of a linear layer in a neural network and of linear regression prediction.

import numpy as np
n_samples, n_features, n_outputs = 200, 10, 3
X = np.random.rand(n_samples, n_features)
W = np.random.rand(n_features, n_outputs)
b = np.zeros(n_outputs)
predictions = X @ W + b   # shape (200, 3)
print(predictions.shape)

Quick Check

Test your understanding of NumPy matrix multiplication.

Lesson Recap

In this lesson you learned: np.matmul and @ implement true matrix multiplication (not element-wise), the inner dimensions of the two matrices must match, and @ supports batched multiplication over leading dimensions. Next up we explore determinants, inverses, and transposes — the operations that tell you whether a matrix can be 'undone'.

常见问题解答

「使用 np.matmul 和 @ 进行矩阵乘法」课时是免费的吗?

是的 — 「使用 np.matmul 和 @ 进行矩阵乘法」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Pandas & NumPy Academy 课程的其余内容,请升级到 CoddyKit PRO。 Pandas & NumPy Academy 课程共包含 4 节课。

「使用 np.matmul 和 @ 进行矩阵乘法」这节课中我会学到什么?

使用 np.matmul 和 @ 运算符正确地进行矩阵相乘,并区分逐元素乘法 * 与真正的矩阵乘法 你通过在浏览器中直接运行的动手代码来练习 Pandas & NumPy Academy,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Pandas & NumPy Academy 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Pandas & NumPy Academy 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 1 节课,共 4 节。

「使用 np.matmul 和 @ 进行矩阵乘法」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Pandas & NumPy Academy 课中编写并运行代码吗?

能。每节 Pandas & NumPy Academy 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 使用 np.matmul 和 @ 进行矩阵乘法
  2. 行列式、逆矩阵与转置
  3. 线性方程组求解
  4. 特征值与 SVD 概览
← 返回 Pandas & NumPy Academy