Arithmetic Operations on NumPy Arrays - onlinetutorialspoint

PROGRAMMINGJava ExamplesC Examples
Java Examples
C Examples
C Tutorials
aws
JAVAEXCEPTIONSCOLLECTIONSSWINGJDBC
EXCEPTIONS
COLLECTIONS
SWING
JDBC
JAVA 8
SPRING
SPRING BOOT
HIBERNATE
PYTHON
PHP
JQUERY
PROGRAMMINGJava ExamplesC Examples
Java Examples
C Examples
C Tutorials
aws
In this tutorial, we will see how to perform basic arithmetic operations, apply trigonometric and logarithmic functions on the array elements of a NumPy array. We will also see how to find sum, mean, maximum and minimum of elements of a NumPy array and then we will also see how to perform matrix multiplication using NumPy arrays.
In NumPy, Arithmetic operations are element-wise operations. i.e. we can perform arithmetic operations on the entire array and every element of the array gets updated by the same operation.
For example, suppose we have an array ‘A’ with elements from 1 to 10 and we want to add 4 to each element. We can do this by simply doing ‘A+4’ and we don’t have to iterate the whole array and then add 4 to each element.
import numpy as np

A= np.arange(1,11)
print("Array A is:")
print(A)
C=A+4
print("Array C is:")
print(C)
Output:
Array A is:
[ 1  2  3  4  5  6  7  8  9 10]
Array C is:
[ 5  6  7  8  9 10 11 12 13 14]
Similarly, we can perform subtraction, division, multiplication etc. on NumPy arrays.
import numpy as np
A= np.arange(1,11)
print("Array A is:")
print(A)

D=A-2
print("Array D is:")
print(D)
E=A/2
print("Array E is:")
print(E)
F=A*2
print("Array F is:")
print(F)
Output:
Array A is:
[ 1  2  3  4  5  6  7  8  9 10]
Array D is:
[-1  0  1  2  3  4  5  6  7  8]
Array E is:
[0.5 1.  1.5 2.  2.5 3.  3.5 4.  4.5 5. ]
Array F is:
[ 2  4  6  8 10 12 14 16 18 20]
There are many functions which are called universal functions and which operate on a NumPy array in an element by element manner. Functions to calculate trigonometric ratios, logarithm and square root are some of the universal functions.
We can calculate the square root of each element of an array by simply using np.sqrt() function and passing the array as a parameter. The output is also an array with the same shape that of the input array and elements having square roots of the input array.
import numpy as np

A= np.arange(1,11)
print("Array A is:")
print(A)

B=np.sqrt(A)
print("Array B is :")
print(B)
Output:
Array A is:
[ 1  2  3  4  5  6  7  8  9 10]
Array B is :
[1.         1.41421356 1.73205081 2.         2.23606798 2.44948974
 2.64575131 2.82842712 3.         3.16227766]
To calculate the logarithm of each element of the NumPy array, we can use np.log() function and pass the input array as a parameter to it. The output is a new array of the same shape of the input array and having elements as logarithms of elements of the input array.
import numpy as np

A= np.arange(1,11)
print("Array A is:")
print(A)

C=np.log(A)
print("Array C is :")
print(C)
Output:
Array A is:
[ 1  2  3  4  5  6  7  8  9 10]
Array C is :
[0.         0.69314718 1.09861229 1.38629436 1.60943791 1.79175947
 1.94591015 2.07944154 2.19722458 2.30258509]
Trigonometric functions can also be applied on NumPy array in a similar manner to sqrt and log functions.
import numpy as np
A= np.arange(1,11)
print("Array A is:")
print(A)

D=np.sin(A)
print("Array D is :")
print(D)
Output:
Array A is:
[ 1  2  3  4  5  6  7  8  9 10]
Array D is :
[ 0.84147098  0.90929743  0.14112001 -0.7568025  -0.95892427 -0.2794155
  0.6569866   0.98935825  0.41211849 -0.54402111]
import numpy as np

A= np.arange(1,11)
print("Array A is:")
print(A)

E=np.cos(A)
print("Array E is :")
print(E)
Output:
Array A is:
[ 1  2  3  4  5  6  7  8  9 10]
Array E is :
[ 0.54030231 -0.41614684 -0.9899925  -0.65364362  0.28366219  0.96017029
  0.75390225 -0.14550003 -0.91113026 -0.83907153]
import numpy as np
A= np.arange(1,11)
print("Array A is:")
print(A)

F=np.tan(A)
print("Array F is :")
print(F)

There are also aggregate functions which perform an operation on the whole array and produce a single result. These functions include calculation of sum, minimum, maximum, mean and standard deviation of all the elements of a NumPy array.
We can calculate the sum of elements of a given NumPy array using sum() method. Suppose we have to calculate the sum of elements of a NumPy array A, then we can simply call A.sum() and it returns the sum of all the elements in A.
import numpy as np
A= np.arange(1,11)
print("Array A is:")
print(A)

b= A.sum()
print("sum of elements of array A is:")
print(b)
Output:
Array A is:
[ 1  2  3  4  5  6  7  8  9 10]
sum of elements of array A is:
55
To find the minimum element in a NumPy array A, we can simply call A.min() method and it returns the minimum number among the elements of A.
import numpy as np
A= np.arange(1,11)
print("Array A is:")
print(A)

min=A.min()
print("Minimum number among the elements of A is:")
print(min)
Output:
Array A is:
[ 1  2  3  4  5  6  7  8  9 10]
Minimum number among the elements of A is:
1
We can find maximum element in a NumPy array A by using method A.max() and it returns maximum number among all the elements of the array.
import numpy as np
A= np.arange(1,11)
print("Array A is:")
print(A)

max=A.max()
print("Maximum number among the elements of A is:")
print(max)
Output:
Array A is:
[ 1  2  3  4  5  6  7  8  9 10]
Maximum number among the elements of A is:
10
We can find the average value/mean of elements of a NumPy array by using the mean() method.
import numpy as np
A= np.arange(1,11)
print("Array A is:")
print(A)

e= A.mean()
print("Average of elements of array A is:")
print(e)
Output:
Array A is:
[ 1  2  3  4  5  6  7  8  9 10]
Average of elements of array A is:
5.5
Introduction to NumPy
NumPy Math functions
Happy Learning 🙂

What is Python NumPy Library
Java Program to swap two arrays Example
Different ways to use Lambdas in Python
What is Java Arrays and how it works ?
Java 8 Stream API and Parallelism
Python Set Data Structure in Depth
PHP Multidimensional Arrays Example
Python Decorators – Classes and Functions
AngularJs Directive Example Tutorials
C Program – Bubble Sort Program in C
Binary Search using Java
Python How to read input from keyboard
Python Selenium Automate the Login Form
Bubble Sort In Java
jQuery Get Attributes Example Tutorials

What is Python NumPy Library
Java Program to swap two arrays Example
Different ways to use Lambdas in Python
What is Java Arrays and how it works ?
Java 8 Stream API and Parallelism
Python Set Data Structure in Depth
PHP Multidimensional Arrays Example
Python Decorators – Classes and Functions
AngularJs Directive Example Tutorials
C Program – Bubble Sort Program in C
Binary Search using Java
Python How to read input from keyboard
Python Selenium Automate the Login Form
Bubble Sort In Java
jQuery Get Attributes Example Tutorials
 


Δ
 Python – Introduction
 Python – Features
 Python – Install on Windows
 Python – Modes of Program
 Python – Number System
 Python – Identifiers
 Python – Operators
 Python – Ternary Operator
 Python – Command Line Arguments
 Python – Keywords
 Python – Data Types
 Python – Upgrade Python PIP
 Python – Virtual Environment
 Pyhton – Type Casting
 Python – String to Int
 Python – Conditional Statements
 Python – if statement
 Python – *args and **kwargs
 Python – Date Formatting
 Python – Read input from keyboard
 Python – raw_input
 Python – List In Depth
 Python – List Comprehension
 Python – Set in Depth
 Python – Dictionary in Depth
 Python – Tuple in Depth
 Python – Stack Datastructure
 Python – Classes and Objects
 Python – Constructors
 Python – Object Introspection
 Python – Inheritance
 Python – Decorators
 Python – Serialization with Pickle
 Python – Exceptions Handling
 Python – User defined Exceptions
 Python – Multiprocessing
 Python – Default function parameters
 Python – Lambdas Functions
 Python – NumPy Library
 Python – MySQL Connector
 Python – MySQL Create Database
 Python – MySQL Read Data
 Python – MySQL Insert Data
 Python – MySQL Update Records
 Python – MySQL Delete Records
 Python – String Case Conversion
 Howto – Find biggest of 2 numbers
 Howto – Remove duplicates from List
 Howto – Convert any Number to Binary
 Howto  – Merge two Lists
 Howto – Merge two dicts
 Howto – Get Characters Count in a File
 Howto – Get Words Count in a File
 Howto – Remove Spaces from String
 Howto –  Read Env variables
 Howto – Read a text File
 Howto – Read a JSON File
 Howto –  Read Config.ini files
 Howto – Iterate Dictionary
 Howto – Convert List Of Objects to CSV
 Howto – Merge two dict in Python
 Howto – create Zip File
 Howto – Get OS info
 Howto – Get size of Directory
 Howto – Check whether a file exists
 Howto –  Remove key from dictionary
 Howto – Sort Objects
 Howto – Create or Delete Directories
 Howto – Read CSV File
 Howto – Create Python Iterable class
 Howto – Access for loop index
 Howto – Clear all elements from List
 Howto – Remove empty lists from a List
 Howto – Remove special characters from String
 Howto – Sort dictionary by key
 Howto – Filter a list