Skip to content

numpy/dot-on-matrices

Pack Severity Fix Versions
NumPy info unsafe, with xpd check --fix --unsafe-fixes All versions

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.

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

See all rules in the NumPy pack.