Data Science 365
Published in

Data Science 365

NumPy for Data Science: Part 3

Arithmetic Operations on NumPy Arrays

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.

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

Technologies used in this tutorial

  • Python
  • NumPy
  • Jupyter Notebook

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store