Skip to main content

How to Search Arrays in Numpy

How to Search Arrays in Numpy
Hi guys welcome to another edition on this platform.  If you are new here, please do checkout our previous articles on NumPy and python. Do checkout our YouTube channel to get the full video tutorials.

We have different methods in searching numerical values in NumPy. 

In this article we have two main methods in searching:

  • np.where()
  • np.searchsorted()

np.where()

With this method, the compiler returns the indexes of elements in an input array where the given condition has a matched.

Syntax

Import numpy as np
numpy.where(condition[, x, y]) 

Condition: a given statement when if found true, the compiler yields x and yields y if false.

x,y: the values from which to choose x and y.

The following example explains how the np.where works

import numpy as np
arr = np.array(['a', 'c', 'b', 'e', 'g', 'b', 'b'])
x = np.where(arr == 'b')
print(x)

NumPy passes the command to the function which searches the given array. when the program is run, any letter below b and in the left of b is the letter to be indexed. The position of this letter is returned as the output.

OUTPUT

How to Search Arrays in Numpy

Let’s take another example

import numpy as np
arr = np.array([0, 1, 4, 6, 4, 3, 8, 3])
x = np.where(arr == 3)
print(x)

NumPy passes the command to the given function which searches the given array. when the program is run, any number which is greater than 3 and in the left of 3 is the value which will be indexed. And the output is the position of this number.

OUTPUT

How to Search Arrays in Numpy

np.searchsorted()

we use this function to find the indices in a sorted array, such that if we insert element before indexing, the order of the array would still be maintain. A binary search is always used to find the correct insertion space.

Syntax

import numpy as np
numpy.searchsorted(arr, num, side=’left’, sorter=None)

The following example will explain how this function works.

import numpy as np
arr = np.array([6, 7, 8, 9,8,8])
x = np.searchsorted(arr, 8)
print(x)

The command is passed on to the function which sorts out the given array. when the program is run/executed, the compiler follows the instructions of the function by searching for any number beside 8 from the left which is less than 8, and returns the position of this number as the output.

OUTPUT

How to Search Arrays in Numpy

As seen from the given syntax, this search method always starts from the left, but we can specify for the search to start from the right.

Example

import numpy as np
arr = np.array([6, 7, 8, 9,8,8])
x = np.searchsorted(arr, 8 , side='right')
print(x)

In this case, the search starts from the right, which in the case the compiler considers values which are greater than 8 and next to 8. The position of this values is returns as the result.

OUTPUT

How to Search Arrays in Numpy

We can also search for multiple arrays in an input of arrays. this array must contain the specified value.

Here’s an example to explain better 

import numpy as np
arr = np.array([6, 7, 8, 9,8,8])
x = np.searchsorted(arr, 8 , side='right')
print(x)

The search takes place simultaneously at the same time. By default, the search starts from the left. The compiler searches for letters less than the specified letters and returns the position of the letters as the output.

OUTPUT

How to Search Arrays in Numpy

There are other functions to use when searching data in NumPy. They help in arranging data according to the given requirements. Hope the articles are very interesting. Have a nice day coding.


Comments

Popular posts from this blog

Introduction to flask

Hello. Welcome to another session on this platform. If you are new here, please do checkout our previous articles on python programming language and stay excited on this session because we are entering into one of python’s web-based application called flask. In this article, we are going to see the following What is flask Prequistes Installation of flask in python Some of flask template engine. What is flask? Flask is one of python-based framework which is used to create web applications. Flight is a very light web framework. During installation, flask comes with pre-installed modules and functions which are used to backup your own web applications. Flask is very easy to understand and perfect go-to for beginners and with flask, a web server can run in less than 3 lines of code. Prequistes Before learning flask, we recommend the reader to have a basic mastery of python concepts. Installation of flask  Before installing flask, we have to checked if python has been installed or. If n...

How to generate random numbers using NumP1

Hello. Welcome to another edition on this platform. For more better understanding, do checkout our YouTube channel to get the video tutorial. In this article of today, we are going to see how to generate random numbers using any of the following methods: Generating a random number Generating a random float and integer Generating random number arrays Generating random number from an array What is a random number? This is a number which cannot be predicted before its occurrence. This number might not different every time. Programmatically, they are two categories of random numbers:     Pseudo-Random numbers       True Random numbers. Just as programs which are written by programmers are a set of instructions, we must follow an algorithm to generate random numbers. Random numbers which are generated using an algorithm are called Pseudo-Random numbers. To generate a true random number, it is important to get the data from sources such as the keyboards, mou...

Introduction to Django

Hello. Welcome to another session on this platform. If you are new here, please do checkout our previous articles on python programming language and stay excited on this session. we are entering into one of python’s application called Django. In this article, we are going to discuss the following: What is Django Why must we use Django  A brief history of Django Installation of Django Popularity of Django What is Django? Python has so many framework application and Django happen to be one of them. Being a python-based-framework, it is used to quickly create web applications.  When building websites, django provides similar ready-made components to handle user authentication, forms, a way to upload components. Why use django? Django is very fast. It takes applications from concept to applications very quickly Django has thousand available packages in it when it is installed. With django, we can launch web applications is a matter of hours. Django is a highly is secured and helps...