NumPy for Data Science: Part 3

Arithmetic Operations on NumPy Arrays

Rukshan Pramoditha
Data Science 365
4 min readMay 17, 2020

--

Hello! Welcome to the 3rd tutorial of NumPy: Arithmetic Operations on NumPy Arrays. In this tutorial, I discuss the following things with examples.

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.

The term Vectorization on ndarray objects is defined as the process of performing a repeated elementwise operation on an array. Vectorized operations in NumPy are implemented via ufuncs which quickly execute repeated operations on values in NumPy arrays.

Computations using vectorization through ufuncs are always more efficient than their counterpart implemented through Python loops, especially as the arrays grow in size.

ufuncs exist in two types:

  • unary ufuncs: Operate on a single input, for example, np.negative()
  • 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:

  • unary operators: Have only one operand (- for negation)
  • 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.

Addition

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

Adding two np arrays
Scalar Addition

Subtraction

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

Subtracting two np arrays
Scalar Subtraction

Multiplication

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

Multiplying two np arrays
Scalar Multiplication

Division

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

Dividing two np arrays
Scalar Division

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.

Data Science 365

This tutorial was designed and created by Rukshan Pramoditha, the Author of Data Science 365 Blog.

Technologies used in this tutorial

  • Python
  • NumPy
  • Jupyter Notebook

2020–05–17

--

--

Rukshan Pramoditha
Data Science 365

3,000,000+ Views | BSc in Stats | Top 50 Data Science, AI/ML Technical Writer on Medium | Data Science Masterclass: https://datasciencemasterclass.substack.com