Skip to content

numpy/dot-on-matrices

Rule dot-on-matrices
Pack NumPy
Severity info
Fix unsafe, with xpd check --fix --unsafe-fixes
Library versions every version

For two 2-D arrays, NumPy's docs prefer the @ operator to np.dot. Write a @ b.

cov = np.dot(X.T, X) / n
cov = X.T @ X / n

The rule reports np.dot(a, b), because the docs of np.dot prefer matmul or a @ b for two 2-D arrays.

xpd check --fix --unsafe-fixes applies the fixes of this rule. Check the result, because an unsafe fix can change what the code does.

The unsafe fix writes a @ b, with parentheses around an operand such as a + 1, and around the product where it is part of a larger operand, as in (a @ b).T. @ raises on a scalar and on two Python lists, and on arrays of more than two dimensions it works on stacks of matrices, where np.dot does not.

xpd prints this message with the issue. A name that starts with $ stands for code from your file.

For 2-D arrays NumPy's docs prefer the @ operator to $NP.dot. Write $PA @ $PB.

Browse the other rules of the NumPy pack.