top of page
hand-businesswoman-touching-hand-artificial-intelligence-meaning-technology-connection-go-

Python NumPy


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([42])
   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: [42] 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[0]:", arr_one[0])
print("arr[1]:", arr_one[1])
print("Adding first and   third element of array: ", arr_one[0] + arr_one[2])

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[0]: 10 arr[1]: 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] : [[4] [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[4] = 35
print("arr[4]:", arr[4])
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[3] = 35
print("x[3]:", x[3])
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[4]: 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[3]: 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[0])
print(splitarr[1])
print(splitarr[2])
print(splitarr[3])

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([43]), array([25]), array([98]), array([76]), array([3])] 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([[21], [51], [34], [12]]), array([[41], [71], [25], [11]])] 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([[31], [25], [45]]), array([[23], [98], [13]]), array([[43], [76], [45]])] 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([3], dtype=int64),) Index of odd element in array: (array([3], 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:
arr = np.array(['Ram', 'Adam', 'Cherry'])
print("Array: ", arr)
print("Sorting in Array:   ")
print(np.sort(arr))

# Sorting   the boolean array
arr = np.array([True, False, True, False])
print("Array: ", arr)
print("Sorting of array:   ")
print(np.sort(arr))

# Sorting   the 2D array
arr = np.array([[8, 4, 2, 9, 15], [13, 23, 45, 65, 24]])
print("Array: ", arr)
print("Sorting in 2D   Array: ")
print(np.sort(arr))
Output: Array: [ 8 4 2 9 15] Sorting in Array: [ 2 4 8 9 15] Array: ['Ram' 'Adam' 'Cherry'] Sorting in Array: ['Adam' 'Cherry' 'Ram'] Array: [ True False True False] Sorting of array: [False False True True] Array: [[ 8 4 2 9 15] [13 23 45 65 24]] Sorting in 2D Array: [[ 2 4 8 9 15] [13 23 24 45 65]]


NumPy Array Filter:


We can select some elements from original array and make a new array. This is called filtration in array. For filtration, we use TRUE as Boolean value to choose the element and FALSE for Exclude the element in new array.

We can also filter the original array in conditions or in iteration of an array as well.


import numpy as np

# Filter   the array
arr = np.array([2, 4, 2, 9, 2])
print("Array: ", arr)
x = [False, False, True, False, True]
newarr = arr[x]
print("Filtered new array   : ", newarr)

# Filtration   of array in for and if command as well.
arr = np.array([100, 110, 120, 150, 180])
filterarr = []
for e in arr:
    if e < 150:
        filterarr.append(True)
    else:
        filterarr.append(False)
newarr = arr[filterarr]
print("Filter Array:   ", filterarr)
print("New Array: ", newarr)
Output: Array: [2 4 2 9 2] Filtered new array : [2 2] Filter Array: [True, True, True, False, False] New Array: [100 110 120]Array: [ 8 4 2 9 15]


Conclusion:


Hopefully, this blog helps you to understand the NumPy basic functions. You can also access the code given here, on my GitHub.

174 views0 comments

Recent Posts

See All

Beginner Friendly Java String Interview Questions

Hello Everyone! Welcome to the second section of the Java Strings blog. Here are some interesting coding questions that has been solved with different methods and approaches. “Better late than never!”

bottom of page