Python NumPy: Overview and Examples
In this article, you will learn about the Python NumPy module introduction, installation process, array creation and several useful functions with examples.
NumPy is the fundamental package for scientific computing with Python. It is a highly optimized library for numerical operations. The support of NumPy makes the task more easier. Several libraries like OpenCV, SciPy, Matplotlib when combine with NumPy increase the number of weapons in your arsenal.
This module contains a number of useful concepts such as the ability to create multidimensional array objects, perform mathematical operations, tools for integrating C/C++ and Fortran code. It also has lots of useful, simple and complex functions, like linear algebra functions, Fourier Transform (FT), random number generator functions and much more.
Python NumPy Installation
We hope you have already installed the Python package. If you do not have, you can download the required version of python from python.org. Once python is installed successfully, open command prompt and install the Python NumPy module using pip tool.
pip install numpy
On successful installation, it returns success messages.
Import NumPy and Check Version
The NumPy import command is-
import numpy as np
The above command renames the numpy namespace to np. Instead of typing 'numpy', we can use a shortcut 'np'.
The given command checks your installed version of Numpy.
import numpy as np
print (np.__version__)
Output
1.18.1
Data Types supported by NumPy
These are the data types supported by NumPy -
- Integers- int
- Unsigned integers- uint
- Boolean- bool
- Floating-point numbers- float
- Complex-valued floating-point numbers- complex
Create NumPy Array
The python NumPy module provides array() function to create the numpy array. This function accepts lists of element values inside a square bracket. We can easily create a single or multi-dimensional array using this method. We can also specify the data type of the array elements.
Single Dimensional Array
The single or one-dimensional array is very simple and basic array.
import numpy as np
one_dim = np.array([30,'Priska','Kashyap','New Delhi',110067])
print(one_dim)
Output
['30' 'Priska' 'Kashyap' 'New Delhi' '110067']
Multi-dimensional Array
In a multidimensional array, array element contains one or more arrays.
import numpy as np
#Two Dimensional Array
two_dim = np.array([[30,29,32], [15,43,22]])
#Three Dimensional Array
three_dim = np.array([[[14,23,54], [14, 22, 19]], [[15,43,22], [30,29,32]]])
print(two_dim)
print(three_dim)
Output
[[30 29 32]
[15 43 22]]
[[[14 23 54]
[14 22 19]]
[[15 43 22]
[30 29 32]]]
NumPy- Get Array Characteristics
NumPy provides many attributes to get characteristics of an array, like - size of an array, dimension of an array, shape of an array and so on.
import numpy as np
arr1 = np.array([30,'Priska','Kashyap','New Delhi',110067])
arr2 = np.array([[29,'Pyush','Garg','Chennai',600005],
[31,'Smith','Soy','Pune',411045]])
# Get type of array object
print("Array arr1 is of type: ", type(arr1))
print("Array arrr2 is of type: ", type(arr2))
# Get array dimensions
print("No. of dimensions in arr1 : ", arr1.ndim)
print("No. of dimensions in arr2 : ", arr2.ndim)
# Get size of array
print("Size of array arr1: ", arr1.size)
print("Size of array arr2: ", arr2.size)
# Get shape of array
print("Shape of array arr1: ", arr1.shape)
print("Shape of array arr2: ", arr2.shape)
Output
(env) c:\python37\Scripts\projects>opencv3.py
Array arr1 is of type: <class 'numpy.ndarray'>
Array arrr2 is of type: <class 'numpy.ndarray'>
No. of dimensions in arr1 : 1
No. of dimensions in arr2 : 2
Size of array arr1: 5
Size of array arr2: 10
Shape of array arr1: (5,)
Shape of array arr2: (2, 5)
NumPy - Reshaping of an array
Reshaping means changing the shape of an array. The numpy module provides reshape() method to add or remove dimension of an array.
import numpy as np
arr1 = np.array([30,'Priska','Kashyap','S-231 S Enclave','New Delhi',110067])
arr2 = np.array([[29,23,34,21,45,24,65,33],
[31,96,43,24,77,86,43,45]])
# Reshaping of One Dimensional Array
x = arr1.reshape(2, 3)
print(x)
# Reshaping of Two Dimensional Array
y = arr2.reshape(2, 2,4)
print(y)
Output
[['30' 'Priska' 'Kashyap']
['S-231 S Enclave' 'New Delhi' '110067']]
[[[29 23 34 21]
[45 24 65 33]]
[[31 96 43 24]
[77 86 43 45]]]
NumPy: Resizing of an array
The resize() method is used to modify the existing size of the array. It returns a new array with a specified shape.
import numpy as np
a=np.array([[0,1],[2,3]])
# Array reshaping
x = np.resize(a,(4,5))
print(x)
Output
[[0 1 2 3 0]
[1 2 3 0 1]
[2 3 0 1 2]
[3 0 1 2 3]]
NumPy - Slicing of an array
Slicing is a process of getting array elements between the specified start and end index. It represents as [start:end].
import numpy as np
arr = np.array([30,'Priska','Kashyap','S-231 S Enclave','New Delhi',110067])
print(arr[0:3])
Output
['30' 'Priska' 'Kashyap']
NumPy - Sorting of an array
NumPy provides sort() method for sorting array elements. It provides 'axis' parameter to specify the axis along which the array is to be sorted. We can also mention the kind of sorting technique, by default it is 'Quick Sort'.
Exampleimport numpy as np
arr = np.array([30,'Priska','Kashyap','S-231 S Enclave','New Delhi',110067])
arr_sort = np.sort(arr)
print(arr_sort)
Output
['110067' '30' 'Kashyap' 'New Delhi' 'Priska' 'S-231 S Enclave']
NumPy - Arithmetic Operations
Numpy module provides functions like add(), subtract(), divide(), multiply(), etc to perform arithmetic operations on arrays.
import numpy as np
arr1 = np.array([30,29,32])
arr2 = np.array([15,43,22])
#Addition
x = np.add(arr1,arr2)
print(x)
#Subtraction
y = np.subtract(arr1,arr2)
print(y)
#Multiplication
z = np.multiply(arr1,arr2)
print(z)
#Division
p = np.divide(arr1,arr2)
print(p)
Output
[45 72 54]
[ 15 -14 10]
[ 450 1247 704]
[2. 0.6744186 1.45454545]
Related Articles
Geometric Transformation OpenCV PythonPython convert dataframe to numpy array
Convert Python list to numpy array
Multiply all elements in list Python
Python NumPy: Overview and Examples
Python Numpy Array Shape
Convert Python list to numpy array
Python convert dataframe to numpy array
zip function in Python
NumPy program to copy data from a given array to another array
Multiply all elements in list Python
Remove element from list Python
Inverse of a matrix in Python
Python Contour Plot Examples
Python iterate list with index
Python add list to list
Prettytable in Python
Python dict inside list
Convert array to list Python
Python Matplotlib Bar Plot