# NumPy for Data Science: Part 3

## Topics discussing

• NumPy ufuncs: unary ufuncs, binary ufuncs
• Definition of vectorization on NumPy arrays
• Python’s built-in arithmetic operators: unary operators, binary operators
• Arithmetic operations on NumPy arrays: Addition, Subtraction, Multiplication, Division, Exponentiation, Modulus/Remainder, Negation, Square root, Absolute value, Exponentials, Logarithms

# Introduction to NumPy ufuncs | universal functions

NumPy ufuncs which stand for universal functions are functions that operate on ndarrays in an element-by-element fashion.

• binary ufuncs: Operate on two inputs, for example, np.add()

# Python’s built-in arithmetic operators

Python’s built-in arithmetic operators are +, -, *, /, **, %. They can be dived into two types:

• binary operators: Have two operands (- for subtraction)

# Arithmetic Operations on NumPy Arrays

The standard arithmetic operations on NumPy arrays perform elementwise operations. To do arithmetic operations on NumPy arrays, we can always use NumPy ufuncs. In most cases, we can also use Python’s built-in arithmetic operators.

The + operator works for addition. We can also use np.add() function to get the same result.

## Subtraction

The -operator works for subtraction. We can also use np.subtract() function to get the same result.

## Multiplication

The * operator works for multiplication. We can also use np.multiply() function to get the same result.

## Division

The / operator works for division. We can also use np.divide() function to get the same result.

## Exponentiation

The ** operator works for exponentiation. We can also use np.power() function to get the same result.

## Modulus/Remainder

The % operator works for modulus or remainder. We can also use np.mod() function to get the same result.

## Negation

The -operator works for negation. We can also use np.negative() function to get the same result.

## Square root

The np.sqrt() function calculates the square root of all elements in the array elementwise.

## Absolute value

Python’s built-in absolute value function, abs() returns the absolute value of all elements in the array elementwise. We can also use np.absolute() or np.abs() function to get the same resut.

## Exponentials

The np.exp() calculates e^x and np.exp2() calculates 2^x for each value of x in your input array.

## Logarithms

The inverse of the exponentials is the logarithms. The np.log() gives the natural logarithm, i.e. logarithm to the base of the mathematical constant e. If you prefer to compute the base-10, you can use np.log10() function.

## Technologies used in this tutorial

• Python
• NumPy
• Jupyter Notebook

--

--

## More from Data Science 365

Bring data into actionable insights.