Search This Blog

NumPy operators and their corresponding NumPy universal functions (ufuncs)

NumPy operators and their corresponding NumPy universal functions (ufuncs)

This table was compiled from the 3 tables shown on pages 53, 72, 75 of the book Python Data Science Handbook by Jake VanderPlas. I put it here just for my own reference.

Arithmetic Operator

Equivalent ufunc

Description

+

np.add

Addition (e.g., 1 + 1 = 2)

-

np.subtract

Subtraction (e.g., 3 - 2 = 1)

-

np.negative

Unary negation (e.g., -2)

*

np.multiply

Multiplication (e.g., 2 * 3 = 6)

/

np.divide

Division (e.g., 3 / 2 = 1.5)

//

np.floor_divide

Floor division (e.g., 3 // 2 = 1)

**

np.power

Exponentiation (e.g., 2 ** 3 = 8)

%

np.mod

Modulus/remainder (e.g., 9 % 4 = 1)

 

 

 

Comparison Operator

Equivalent ufunc

 

==

np.equal

 

!=

np.not_equal

 

< 

np.less

 

<=

np.less_equal

 

> 

np.greater

 

>=

np.greater_equal

 

 

 

 

Logical Operator

Equivalent ufunc

See also

&

np.bitwise_and

np.logical_and

|

np.bitwise_or

np.logical_or

^

np.bitwise_xor

np.logical_xor

~

np.bitwise_not

np.logical_not


NumPy rules of broadcasting

These rules are listed in the book Python Data Science Handbook by Jake VanderPlas (see page 65).

Broadcasting in NumPy is simply a set of rules for applying binary universal functions (like addition, subtraction, multiplication, etc.) on NumPy arrays of different sizes.

Here are the NumPy broadcasting rules. 

• Rule 1: If the two arrays differ in their number of dimensions, the shape of the
one with fewer dimensions is
padded with ones on its leading (left) side.

• Rule 2: If the shape of the two arrays does not match in any dimension, the array
with shape equal to 1 in that dimension is stretched to match the other shape.


• Rule 3: If in any dimension the sizes disagree and neither is equal to 1, an error is
raised.