top of page Search

# NumPy Introduction

NumPy (short form of ‘Numerical Python’) is a python module which is used in domain of linear algebra, matrices, fourier transform and in data science field.

NumPy arrays are faster than Python Lists because they provide an array object. An array object is called ndarray in NumPy.

# Installation of NumPy

As we know, Numpy is a Python library. To install NumPy, the system should have Python and Pip version. I am using Pycharm Django in which I have python 3.7 version. In command line we will write a code as follows:

` C:\Users\Your Name>pip install numpy`

# Import NumPy

To write a program in python for numpy, after installation, we will import NumPy as np keyword from python library.

`import numpy as np`

Note: To check a version of NumPy installed in python, the command should be as follows:

```import numpy as np
print(np._version_)```
Output:1.16.3

# NumPy Array Object as ndarray with array()

We can use array() function for creating an array object ndarray in NumPy.

```import numpy as np
arr = np.array([10,25,35,24])
print(arr)
print(type(arr))```
Output: [10 25 35 24 ] <class ‘numpy.ndarray’>

# Tuple into NumPy array

```import numpy as np
arr = np.array((10,22,33,14))
print(arr)```
Output: [10 22 33 14 ]

# NumPy Array Dimensions

In Numpy , there are 0-D, 1-D, 2-D, 3-D …….nth-D dimensions. With ndim attribute, we can get the dimensions of the array in NumPy.

In NumPy, we can define the dimensions of an array with ndmin argument for creating high dimensional array.

```import numpy as np
#Creation   of Zero Dimensional Array
arr_a = np.array()
print("Zero Dimensional Array: ",   arr_a)
#Creation   One Dimensional Array
arr_b = np.array([1,2,3])
print("One Dimensional Array: ",   arr_b)
#Creation   Two Dimensional Array
arr_c = np.array([[1,2,3],   [4,5,6]])
print("Two Dimensional Array: ",   arr_c)
#Creation Three-Dimensional Array
arr_d= np.array([[[1,2,3,], [4,5,6]], [[1,2,3,], [4,5,6]]])
print("Three Dimensional Array: ",arr_d)
#Dimensional   Command: ndim
print(arr_a.ndim, "\n", arr_b.ndim,"\n", arr_c.ndim, "\n", arr_d.ndim
#Define   dimension with ndmin argument
arr   = np.array([2,5,6,8], ndmin=4)
print("Array",arr)
print('Number of dimensions :', arr.ndim)```
Output: Zero Dimensional Array:  One Dimensional Array: [1 2 3] Two Dimensional Array: [[1 2 3] [4 5 6]] Three Dimensional Array: [[[1 2 3 ] [4 5 6]] [[1 2 3] [4 5 6]]] Dimensions of array: arr_a: 1 arr_b: 1 arr_c: 2 arr_d: 3 Array [[[[2 5 6 8]]]] Number of dimensions: 4

# NumPy Array Indexing

In NumPy, the elements of an array can be defined same as elements of python lists. The first element would be start from 0 index, second element from 1 index….so on

Example:

Index: 0 1 2 3

Array: [3, 4 ,5 ,8]

0-index = first element as 3

1-index = second element as 4

2-index = third element as 5

```import numpy as np

# Array   Indexing
arr_one = np.array([10, 15, 25, 30, 5, 20])
print("Array:", arr_one)
print("Array   Indexing:")
print("arr:", arr_one)
print("arr:", arr_one)
print("Adding first and   third element of array: ", arr_one + arr_one)

arr_two = np.array([[5, 10, 15], [20, 25, 30]])
print("Array_Two", arr_two)
print("2nd element of 1st   dim: ", arr_two[0, 1])
print("3rd element on 2nd   dim: ", arr_two[1, 2])

# Negative   Indexing
print("last element of   first dimension", arr_two[0, -1])```
Output: Array: [10 15 25 30 5 20] Array Indexing: arr: 10 arr: 15 Adding first and third element of array: 35 Array_Two [[ 5 10 15] [20 25 30]] 2nd element of 1st dim: 10 3rd element on 2nd dim: 30 last element of first dimension 15

# NumPy Array Slicing

We can pass slice of an array instead of index i.e. [start:end] and define the step i.e [start:end:step] in NumPy.

```import numpy as np

# Array   Slicing
arr = np.array([10, 15, 25, 30, 5, 20])
print("Array:", arr)
print("Array Slicing:")
print("arr[1:5]:", arr[1:5])
print("arr[4: ]:", arr[4:])
print("arr[:4]", arr[:4])
print("arr[-3:-1]:", arr[-3:-1])
print("arr[1:5:2]:", arr[1:5:2])
print("arr[::2]", arr[::2])

# Slicing in 2-D Array
arr_two = np.array([[3, 4, 5, 6], [7, 2, 1, 9]])
print("2-D Array: ", arr_two)
print("arr[0:3,1:2] :   ", arr_two[0:3, 1:2])```
Output: Array: [10 15 25 30 5 20] Array Slicing: arr[1:5]: [15 25 30 5] arr[4: ]: [ 5 20] arr[:4] [10 15 25 30] arr[-3:-1]: [30 5] arr[1:5:2]: [15 30] arr[::2] [10 25 5] 2-D Array: [[3 4 5 6] [7 2 1 9]] arr[0:3,1:2] : [ ]

In [start:end], start index will be included and end index will be excluded in results.

# NumPy Data Types:

```import numpy as np

# Data   Type In Numpy: dtype
arr = np.array([1, 2, 3, 4])
print("Array: ", arr)
print("Data Type in Array:   ")
print(arr.dtype)

arr = np.array(['Apple', 'Banana', 'Cherry'])
print("Array: ", arr)
print("Data Type in Array:   ")
print(arr.dtype)

# Define   the Data Type in Array
arr = np.array([1, 2, 3, 4], dtype='S')
print("Array:", arr)
print("define the data   type in array:", arr.dtype)

arr = np.array([1, 2, 3, 4], dtype='i4')
print("Array:", arr)
print("define the data   type in array:", arr.dtype)

arr = np.array([1, 2, 3])
newarr = arr.astype(bool)
print("Array: ", arr)
print("New Array: ", newarr)
print("data dtype of   array: ", arr.dtype)
print("data type of New   Array:", newarr.dtype)```
Output: Array: [1 2 3 4] Data Type in Array: int32 Array: ['Apple' 'Banana' 'Cherry'] Data Type in Array: <U6 Array: [b'1' b'2' b'3' b'4'] define the data type in array: |S1 Array: [1 2 3 4] define the data type in array: int32 Array: [1 2 3] New Array: [ True True True] data dtype of array: int32 data type of New Array: bool

# NumPy Array Copy and View

Copy() : An copy of an original array and any changes made in original array elements doesn’t affect the copy array.

View(): Any changes in original array affects the new array with view function.

```import numpy as np

# Copy   in Array
arr = np.array([25, 40, 30, 45, 50])
print("Array:", arr)
newarr = arr.copy()
print("NewArray:", newarr)
arr = 35
print("arr:", arr)
print("Array:", arr)
print("New Array copy():   ", newarr)

# View   func in array.
arr = np.array([25, 40, 30, 45, 50])
print("Array:", arr)
x = arr.view()
print("X Array: ", x)
x = 35
print("x:", x)
print("Array: ", arr)
print("New Array after   view func: ", x)

# both copy and   view func in an array
arr = np.array([25, 40, 30, 45, 50])
print("Array: ", arr)
x = arr.copy()
y = arr.view()
print("Array Y with view   func: ", y)
print("Base of Array   Y", y.base)
print("Array X with   copy():", x)
print("Base of Array   X", x.base)```
Output: Array: [25 40 30 45 50] NewArray: [25 40 30 45 50] arr: 35 Array: [25 40 30 45 35] New Array copy(): [25 40 30 45 50] Array: [25 40 30 45 50] X Array: [25 40 30 45 50] x: 35 Array: [25 40 30 35 50] New Array after view func: [25 40 30 35 50] Array: [25 40 30 45 50] Array Y with view func: [25 40 30 45 50] Base of Array Y [25 40 30 45 50] Array X with copy(): [25 40 30 45 50] Base of Array X None

Note: In copy function, base attribute will return None. In view function, base attribute will return the original array.

# NumPy Array Shape and Reshape

Shape : NumPy has a shape attribute which tells how many elements in each dimensions of an array in tuple format.

```import numpy as np

# Shape
arr = np.array([[7, 2, 9, 4], [5, 3, 7, 6]])
print("Array:", arr)
print("Shape of   Array:", arr.shape)

# shape attribute with ndim
arr = np.array([7, 2, 9, 4], ndmin=4)
print("Define dimension in   array:", arr.ndim)
print("Array:", arr)
print("Shape of Array:   ", arr.shape)```
Output: Array: [[7 2 9 4] [5 3 7 6]] Shape of Array: (2, 4) Define dimension in array: 4 Array: [[[[7 2 9 4]]]] Shape of Array: (1, 1, 1, 4)

In above example of code, shape of array (2,4) , here 2 is dimensions of an array and 4 is number of elements in each array. We can define shape attribute with ndim arguments as well.

Reshape: In Reshape, we can convert the array dimension in other dimension array…in other meaning we can change array shape.

```import numpy as np

# Reshape change the dimensions of an array from 1-D to 2-D
arr = np.array([10, 15, 30, 45, 55, 20, 50, 60, 12, 19, 20, 22])
print("Array:", arr)
newarr = arr.reshape(4, 3)
print("Reshape   Array:", newarr)

# 1-D   array reshaped into 3-D array
newarr_b = arr.reshape(2, 3, 2)
print("1-d to 3-D Array:   ", newarr_b)

arr = np.array([10, 15, 30, 45, 55, 20, 50, 60])
print(arr.reshape(2, 4).base)

# Reshape in 2_d into 1-D array
arr = np.array([[1, 2, 3], [4, 5, 6]])
newarr_c = arr.reshape(-1)
print("2-D into 1-D Array:   ", newarr_c)```
Output: Array: [10 15 30 45 55 20 50 60 12 19 20 22] Reshape Array: [[10 15 30] [45 55 20] [50 60 12] [19 20 22]] 1-d to 3-D Array: [[[10 15] [30 45] [55 20]] [[50 60] [12 19] [20 22]]] [10 15 30 45 55 20 50 60] 2-D into 1-D Array: [1 2 3 4 5 6]

# NumPy Array Iterating

In Numpy, we can use for loop to iterate the each element in numpy array as well. Examples are as below in code:

```import numpy as np

#   1-D Array Iteration:
arr_one = np.array([17, 12, 10])
print("1-D Array: ", arr_one)
print("Array Iteration in   1-D Array:")
for x in arr_one:
print(x)

#   2-D Array Iteration:
arr_two = np.array([[17, 12, 10], [14, 15, 16]])
print("2-D Array:", arr_two)
print("Array Iteration in   2-D Array:")
for x in arr_two:
# print(x): prints each array
for y in x:
# print each element in 2D   array
print(y)

#   3-D Array Iteration
arr_three = np.array([[[17, 12, 13], [24, 25, 26]], [[47, 48, 49], [80, 11, 35]]])
print("3-D Array:", arr_three)
print("Array iteration in   3-D Array:")
for x in arr_three:
for y in x:
for z in y:
print(z)

# nditer() in numpy
arr = np.array([[[17, 12], [13, 24]], [[25, 26], [47, 48]]])
print("Array:", arr)
print("Numpy Iteration   with nditer(): ")
for x in np.nditer(arr):
print(x)

# Iteration   array with different data types: op_dtypes
arr = np.array([12, 50, 75])
print("Array: ", arr)
for x in np.nditer(arr, flags=['buffered'], op_dtypes=['S']):
print("Iteration with   different data types: ", x)

# Iteration with different step size
arr = np.array([[12, 50, 75, 20], [13, 10, 15, 30]])
print("Array: ", arr)
for x in np.nditer(arr[:: 3]):
print("Iteration with   different step size: ", x)

# ndenumerate() func
arr = np.array([12, 22, 32])
for idx, x in np.ndenumerate(arr):
print(idx, x)

arr = np.array([[12, 22, 32, 42], [53, 64, 75, 80]])
for idx, x in np.ndenumerate(arr):
print(idx, x)```
Output: 1-D Array: [17 12 10] Array Iteration in 1-D Array: 17 12 10 2-D Array: [[17 12 10] [14 15 16]] Array Iteration in 2-D Array: 17 12 10 14 15 16 3-D Array: [[[17 12 13] [24 25 26]] [[47 48 49] [80 11 35]]] Array iteration in 3-D Array: 17 12 13 24 25 26 47 48 49 80 11 35 Array: [[[17 12] [13 24]] [[25 26] [47 48]]] Numpy Iteration with nditer(): 17 12 13 24 25 26 47 48 Array: [12 50 75] Iteration with different data types: b'12' Iteration with different data types: b'50' Iteration with different data types: b'75' Array: [[12 50 75 20] [13 10 15 30]] Iteration with different step size: 12 Iteration with different step size: 50 Iteration with different step size: 75 Iteration with different step size: 20 (0,) 1 (1,) 2 (2,) 3 (0, 0) 1 (0, 1) 2 (0, 2) 3 (0, 3) 4 (1, 0) 5 (1, 1) 6 (1, 2) 7 (1, 3) 8

nditer() : As we use n for loops to iterate each elements of an array likewise we use nditer() function in Numpy. This function helps to solve the issue with high dimensionality from basic to advances iteration level.

op_dtypes: With this argument in Numpy, we can change the data types of an element during iteration. For this we need extra space as a buffer we use flag with nditer function.

ndenumerate(): With this function, we can iterate the each elements of an array with their index as well.

# NumPy Array Join

Numpy array join means to add the contents of two or more arrays into on single array by their axes.

For joining the array we use concatenate() function to pass sequence of arrays.

We can also use stack() function to joining the array along with the new axis. There are some other attribute of stack() function i.e. hstack(), vstack() and dstcak(). Examples of this function as follows:

```import numpy as np

# join two array
arr1 = np.array([31, 23, 43])
print("Array 1:", arr1)
arr2 = np.array([48, 15, 60])
print("Array 2:", arr2)
joinarr = np.concatenate((arr1, arr2))
print("two 1-D array joining into one array: ", joinarr)

arr3 = np.array([[11, 21], [13, 49]])
print("1st 2D Array :", arr3)
arr4 = np.array([[25, 46], [97, 88]])
print("2nd 2D Array :", arr4)
newarr = np.concatenate((arr3, arr4), axis=1)
print("Join Two 2D Array into one 2D Array:", newarr)

# Stack () in Joining Arrays
arr1 = np.array([31, 23, 43])
print("Array 1:", arr1)
arr2 = np.array([48, 15, 60])
print("Array 2:", arr2)
joinarr = np.stack((arr1, arr2))
print("two 1-D array joining into one array with stack function: ", joinarr)

hstack_arr = np.hstack((arr1, arr2))
print("Stacking with rows of an array:", hstack_arr)

vstack_arr = np.vstack((arr1, arr2))
print("Stacking with columns of an array:", vstack_arr)

dstack_arr = np.dstack((arr1, arr2))
print("Stacking with height of an array:", dstack_arr)```
Output: Array 1: [31 23 43] Array 2: [48 15 60] two 1-D array joining into one array: [31 23 43 48 15 60] 1st 2D Array : [[11 21] [13 49]] 2nd 2D Array : [[25 46] [97 88]] Join Two 2D Array into one 2D Array: [[11 21 25 46] [13 49 97 88]] Array 1: [31 23 43] Array 2: [48 15 60] two 1-D array joining into one array with stack function: [[31 23 43] [48 15 60]] Stacking with rows of an array: [31 23 43 48 15 60] Stacking with columns of an array: [[31 23 43] [48 15 60]] Stacking with height of an array: [[[31 48] [23 15] [43 60]]]

# Numpy Array Splitting:

In Joining we merges two array into one array, but in splitting we split the array into multiple arrays with array_split() function.

There are some other attribute of array_split() function i.e. hsplit(), vsplit() and dsplit(). Examples of this function as follows:

```import numpy as np

# spliting   array
arr1 = np.array([31, 23, 43, 25, 98, 76, 45, 13, 45])
print("Array 1:", arr1)
splitarr = np.array_split(arr1, 3)
print("splitting the one   array into 3 arrays: ", splitarr)

arr2 = np.array([31, 23, 43, 25, 98, 76, 3])
print("Array 2:", arr2)
splitarr = np.array_split(arr2, 6)
print("splitting the one   array into 6 arrays: ", splitarr)

# spliting   2D array:
arr = np.array([[21, 41], [51, 71], [34, 25], [12, 11]])
print("2D Array:", arr)
splitarr = np.array_split(arr, 2)
print("2d Array split into   two 2D array:", splitarr)
newarr = np.array_split(arr, 2, axis=1)
print("newarr", newarr)

# Accessing   the array an element after split:
arr1 = np.array([31, 23, 43, 25, 98, 76, 45, 13])
print("Array 1:", arr1)
splitarr = np.array_split(arr1, 4)
print("elements after splitting   the one array into 4 arrays: ")
print(splitarr)
print(splitarr)
print(splitarr)
print(splitarr)

arr = np.array([[31, 23, 43], [25, 98, 76], [45, 13, 45]])
print("Array :", arr)
hsplit_arr = np.hsplit(arr, 3)
print("Stacking with rows   of an array:", hsplit_arr)
vsplit_arr = np.vsplit(arr, 3)
print("Stacking with   columns of an array:", vsplit_arr)```
Output: Array 1: [31 23 43 25 98 76 45 13 45] splitting the one array into 3 arrays: [array([31, 23, 43]), array([25, 98, 76]), array([45, 13, 45])] Array 2: [31 23 43 25 98 76 3] splitting the one array into 6 arrays: [array([31, 23]), array(), array(), array(), array(), array()] 2D Array: [[21 41] [51 71] [34 25] [12 11]] 2d Array split into two 2D array: [array([[21, 41], [51, 71]]), array([[34, 25], [12, 11]])] newarr [array([, , , ]), array([, , , ])] Array 1: [31 23 43 25 98 76 45 13] elements after splitting the one array into 4 arrays: [31 23] [43 25] [98 76] [45 13] Array : [[31 23 43] [25 98 76] [45 13 45]] Stacking with rows of an array: [array([, , ]), array([, , ]), array([, , ])] Stacking with columns of an array: [array([[31, 23, 43]]), array([[25, 98, 76]]), array([[45, 13, 45]])]

# NumPy Array Search:

With where() method in NumPy, we can find indexes of certain value/element in array.

There is an another method searchsorted() which is used on sorted array . The where() function normally finds the indexes of an element in array from left side. We can use side = ‘right’ method to search the indexes from right side as well.

```import numpy as np

# Searching   the numeric array
arr = np.array([2, 4, 2, 9, 2])
print("Array: ", arr)
x = np.where(arr == 2)
print("Searching Array with certain element which gives indexes in result : ", x)
x = np.where(arr % 3 == 0)
print("Index of an element   in array where is divided by 3: ", x)
x = np.where(arr % 2 == 1)
print("Index of odd   element in array: ", x)

aa1 = np.array([12, 15, 19, 32])
print("New Array :", aa1)
x = np.searchsorted(aa1, 15)
print("Binary search in   array ", x)
x1 = np.searchsorted(aa1, 15, side='right')
print("Search from right   side of array:", x1)
x2 = np.searchsorted(aa1, [10, 13, 20])
print("sorting multiple   values in array: ", x2)```
Output: Array: [2 4 2 9 2] Searching Array with certain element which gives indexes in result : (array([0, 2, 4], dtype=int64),) Index of an element in array where is divided by 3: (array(, dtype=int64),) Index of odd element in array: (array(, dtype=int64),) New Array : [12 15 19 32] Binary search in array 1 Search from right side of array: 2 sorting multiple values in array: [0 1 3]

# NumPy Array Sort:

To arrange the elements of an array in sequence order ascending or descending, we use sort() function. It is used for any data type in NumPy array.

```import numpy as np

# Sorting   the numeric array
arr = np.array([8, 4, 2, 9, 15])
print("Array: ", arr)
print("Sorting in Array:   ")
print(np.sort(arr))

# Sorting   the strings: