Instagram
youtube
Facebook
Twitter

Operations with Numpy Arrays III

Joining Arrays in Numpy

  • In Numpy we can join two or more arrays in a single array.

  • Concatenation in Numpy arrays is based on the axis.

  • The joining of arrays is done via concatenate function which takes arrays as parameters and the axis by default axis is set to 0. 

Example:

import numpy as np

arr1 = np.array([11, 22, 33])

arr2 = np.array([44, 55, 66])

arr = np.concatenate((arr1, arr2))

print(arr) #prints [11, 22, 33, 44, 55, 66]

Now, we will see an example with an axis parameter

Example:

import numpy as np

arr1 = np.array([[11, 22], [33, 44]])

arr2 = np.array([[55, 66], [77, 88]])

arr = np.concatenate((arr1, arr2), axis=1)

print(arr) #prints [[1 2 5 6]
           #        [3 4 7 8]]

Example:

 

import numpy as np

arr1 = np.array([[11, 22], [33, 44]])

arr2 = np.array([[55, 66], [77, 88]])

arr = np.concatenate((arr1, arr2), axis=1)

print(arr) #prints [[1 2 5 6]
           #        [3 4 7 8]]

Splitting arrays in Numpy

  • In joining we merge multiple arrays into one and in splitting we divide one array into multiples.

  • Splitting is the reverse of joining.

  • The Numpy method used here is array_split, which takes the array and number of splits as the parameters.

Example:

Here, we are splitting an array into 4 parts

import numpy as np

arr = np.array([1, 2, 3, 4, 5, 6, 7, 8])

arr_split = np.array_split(arr, 4)

print(arr_split)

#prints [array([1, 2]), array([3, 4]), array([5, 6]), array([7, 8])]

arr_split2 = np.array_split(arr, 6)

print(arr_split2)

#prints [array([1, 2]), array([3, 4]), array([5]), array([6]), array([7]), array([8])]

We can access the splitter arrays like any element of an array like below:

import numpy as np

arr = np.array([1, 2, 3, 4, 5, 6])

arr1 = np.array_split(arr, 4)

print(arr1)

print(arr1[0])

print(arr1[1])

print(arr1[2])

print(arr[3])

Output:

[array([1, 2]), array([3, 4]), array([5]), array([6])]
[1 2]
[3 4]
[5]
4

Searching Arrays in Numpy

  • In Numpy we can search in arrays for a specific value or element and it returns the indexes of that particular value in the given array.

  • We use the where() method in Numpy for searching.

Example:

import numpy as np

arr = np.array([22, 43,76,0, -45, 45, 22, 32, 65, 22])

print(np.where(arr == 22)) #prints (array([0, 6, 9]),)

We can also use this method to check some mathematical expressions in an array like the below:

import numpy as np

arr = np.array([22, 43, 76, 0, -45, 45, 22, 32, 65, 22])

print(np.where(arr%2 == 0))

#prints (array([0, 2, 3, 6, 7, 9]),)

Sorting Arrays in Numpy

  • In Numpy, we can sort an array using the sort()  method.

  • Sorting an array means putting the elements of a particular array into an ordered sequence.

  • Sorting can be done based on ascending or descending, or alphabetically if the array has strings.

Example:

import numpy as np

arr = np.array([22, 43, 76, 0, -45, 45, 22, 32, 65, 22])

print(np.sort(arr)) #this method returns the copy of array which means actual array is unchanged

#prints [-45   0  22  22  22  32  43  45  65  76]