Introduction To Numpy- Part 6

We’ve covered many parts of this numpy library in python, and we are still complete with exploring its functions that will make using arrays with python so straightforward. This part will discuss using numpy to split the array and search inside the array. Enough talking. Let’s jump into the code.

1. Splitting The Arrays

We’ve learned in the previous tutorial how to split the arrays but with 1-dimensional arrays, and the article will show you how you can perform the same operations with the 2-dimensional arrays. Let’s see an example:

import numpy as np

# Create The Array
array = np.array([[10, 20], [30, 40], [50, 60], [70, 80], [90, 100], [200, 300]])

# Splitting The Array
array2 = np.array_split(array, 2)

# Print The New Array
print(array2)

The function .array_split() function splitter the original 2-dimensional array into two small 2-dimensional arrays. You first specify the array you want to split and then the number of small arrays you want to get from the original array. Let’s split the array along the rows:

import numpy as np

# Create The Array
array = np.array([[10, 20, 30], [40, 50, 60], [70, 80, 90], [100, 200, 300], [400, 500, 600], [700, 800, 900]])

# Split The Array
array2 = np.array_split(array, 2, axis=1)

# Print The New Array
print(array2)

The arrays will be splitter along the rows, as you see from the results after running the code, and they will be splitter into two arrays. Try experimenting with the split value to understand more!

2. Searching Inside Numpy Array

You have an extensive array and want to find a specific value in that array. It will be a daunting task to find its index manually. Numpy has a function to perform this action. Let’s see an example:

import numpy as np

# Creating The Array
array = np.array([4, 5, 6, 6, 3, 8, 6])

# Searching For The Value
y = np.where(array == 6)

# Printing The Value Index
print(y)

You will get an array with 3 numbers when you run the code, meaning the indexed value 6 inside that array. We’ve used the .where() function to search for the number 6 inside the array. 

Let’s suppose you want to find the odd numbers inside the array. It seems a daunting task, and you will maybe create a for loop with the if statement but forget all of this. You have the solution with a tiny command:

import numpy as np

# Creating The Array
array = np.array([4, 5, 6, 6, 3, 8, 6])

# Searching For The Value
y = np.where(array%2 == 1)

# Printing The Value Index
print(y)

We can use a small logic inside the .where() function to get all the odd values inside the array, and this “%” symbol is called the modulus operator, which will return the remainder of the division operator and if the remainder is equal to 1 as in the code above, that means we found an odd number.

3. Search Sorted In Numpy

You have a numpy array with many elements, and you want to sort them from small to a large value. You want to know the proper index of a specific value to have a sorted list. There is a function to perform this, and let’s see an example:

import numpy as np

# Create The Array
array = np.array([2, 5, 4, 3])

# Apply Search Sort Function
y = np.searchsorted(array, 4)

# Print The Result
print(y)

When you execute the code, you will get the value 1. That means the 4 value inside the array should be at index 1, which is the second element after the number 2 since counting starts from 0.

The function .searchsorted() needs to have the array you will apply this function on it. And the number you want to see its index, so the array will be sorted. By default, it will start the search from left to right, but we can change this to return the right most index. Let me show you an example:

import numpy as np

# Create The Array
array = np.array([2, 5, 4, 3])

# Apply Search Sort Function
y = np.searchsorted(array, 4, side="right")

# Print The Result
print(y)

It will return the number 4, which means the value four should be at the index 4 if we start counting from the right to the left. We’ve only added the side attribute to the .searchsorted() function and specified the “right” value.

4. Search With Multiple Values

We’ve done the sort search for only one value, but what if you want to apply it to multiple values? You can do this by specifying the values inside a closed square. Let me show you an example:

import numpy as np

# Create The Array
array = np.array([2, 5, 4, 3])

# Apply Search Sort Function
y = np.searchsorted(array, [3, 5])

# Print The Result
print(y)

The .searchsorted() function returns an array in the result, which shows you the index of every number you want to know its index if the array is sorted ascending. The first value should be at index 1, and the second value should be at index 4.

Conclusion

Thanks for reading! There are many other functions to use in numpy that we can’t cover even in more than 20 parts of this series, but we will try to cover more and how you can use numpy with other libraries to simulate how you can use it in real life.

The OS Module In Python- Part 1
Introduction To Numpy- Part 5
Scroll to Top