When performing matrix multiplication in Python using numpy.dot()
or the @
operator, you might encounter the error:
ValueError: shapes (m,n) and (p,q) not aligned for matrix multiplication
This error occurs when the number of columns in the first matrix does not match the number of rows in the second matrix.
1. Understanding Matrix Multiplication Rules
For two matrices A (m × n) and B (p × q) to be multiplied:
🔹 Condition: The number of columns of A (n) must match the number of rows of B (p).
🔹 Resulting Matrix Shape: The resulting matrix will have the shape (m × q).
Valid Multiplication Example
If:
- Matrix A has shape (3 × 2) → 3 rows, 2 columns
- Matrix B has shape (2 × 4) → 2 rows, 4 columns
- Since A’s columns (2) match B’s rows (2), the multiplication is valid.
- The resulting matrix will have shape (3 × 4).
2. Common Causes of Shape Misalignment
Mistake 1: Mismatched Dimensions
Incorrect Code:
import numpy as np
A = np.array([[1, 2, 3], [4, 5, 6]]) # Shape (2 × 3)
B = np.array([[7, 8], [9, 10]]) # Shape (2 × 2)
result = np.dot(A, B) # Error: (2×3) * (2×2) not possible
Output:
ValueError: shapes (2,3) and (2,2) not aligned: 3 (dim 1) != 2 (dim 0)
Problem:
- A (2 × 3) matrix cannot multiply with a (2 × 2) matrix.
- The number of columns in A (3) ≠rows in B (2).
Fix: Ensure Matrix Shapes Are Compatible
B = np.array([[7, 8, 9], [10, 11, 12], [13, 14, 15]]) # Shape (3 × 3)
result = np.dot(A, B) # Now (2×3) * (3×3) is valid
Mistake 2: Multiplying Vectors with Mismatched Shapes
Incorrect Code:
import numpy as np
A = np.array([1, 2, 3]) # Shape (3,)
B = np.array([4, 5]) # Shape (2,)
result = np.dot(A, B) # Error: Shape mismatch
Output:
ValueError: shapes (3,) and (2,) not aligned: 3 (dim 0) != 2 (dim 0)
Problem:
- A (3,) vector cannot multiply with a (2,) vector.
Fix: Ensure One Vector is Reshaped to Match Dimensions
A = np.array([1, 2, 3]).reshape(3, 1) # Convert to (3×1) matrix
B = np.array([4, 5, 6]) # Shape (3,)
result = np.dot(A.T, B) # (1×3) * (3×1) is valid
Mistake 3: Using @
Operator Incorrectly
Incorrect Code:
import numpy as np
A = np.array([[1, 2], [3, 4]]) # Shape (2×2)
B = np.array([5, 6, 7]) # Shape (3,)
result = A @ B # Error: Shape mismatch
Output:
ValueError: matmul: Input operand 1 has a mismatch in its core dimension 0
Problem:
A
has 2 columns, butB
has 3 elements → Shapes don’t align.
Fix: Reshape B
Properly
B = np.array([5, 6]).reshape(2, 1) # Convert to (2×1)
result = A @ B # Now (2×2) * (2×1) is valid
Mistake 4: Transposing Instead of Reshaping
Incorrect Code:
import numpy as np
A = np.array([[1, 2, 3], [4, 5, 6]]) # Shape (2×3)
B = np.array([[7, 8, 9], [10, 11, 12]]) # Shape (2×3)
result = np.dot(A, B) # Error: Shape mismatch
Problem:
- Both matrices have (2 × 3) shape, but 3rd dimension (columns) must match 1st dimension (rows).
Fix: Use .T
(Transpose) to Align Shapes
result = np.dot(A, B.T) # Now (2×3) * (3×2) is valid
3. Best Practices to Avoid Shape Errors
1. Always Print Matrix Shapes
Before performing matrix multiplication, check matrix shapes using .shape
:
A = np.array([[1, 2], [3, 4]])
B = np.array([[5, 6, 7], [8, 9, 10]])
print(A.shape) # (2, 2)
print(B.shape) # (2, 3)
if A.shape[1] == B.shape[0]: # Columns of A must match rows of B
result = np.dot(A, B)
else:
print("Shape mismatch! Cannot multiply.")
2. Reshape Vectors Correctly
A = np.array([1, 2, 3]).reshape(3, 1) # Convert to column vector (3×1)
B = np.array([4, 5, 6]) # Row vector (3,)
result = A.T @ B # (1×3) * (3,) = (1×3)
3. Use .T
for Transposing Instead of .reshape()
A = np.array([[1, 2, 3], [4, 5, 6]]) # (2×3)
B = np.array([[7, 8, 9], [10, 11, 12]]) # (2×3)
result = np.dot(A, B.T) # Convert B to (3×2) to match A (2×3)
4. Summary of Key Mistakes & Fixes
Mistake | Problem | Fix |
---|---|---|
Multiplying mismatched shapes | Columns of A ≠Rows of B | Ensure A.shape[1] == B.shape[0] |
Multiplying vectors incorrectly | Shape mismatch | Use .reshape() or .T |
Using @ operator incorrectly | Wrong operand dimensions | Convert dimensions to match |
Transposing incorrectly | Shape still misaligned | Use .T to fix alignment |
Forgetting to print shapes before multiplication | Can’t debug errors | Use print(A.shape, B.shape) before multiplication |