Hello readers! There are numerous libraries available to do different functions in Python. The Numpy library is one of them and is used for working with arrays in Python. In many examples like sorting you need to find out the nearest value in an array. In this manner, you can sort the array using this method in ascending and descending order. This technique of finding the nearest value is nothing but the searching method. The searching method is used in handling the database. So, in simple words, we can say that finding the nearest value in a numpy array is a minor part of solving complex problems. In this article, we’ll see the details of a numpy array and how to find the nearest value in a numpy array.
The numpy array contains the same type of values. The indexing of the array is in the positive integer format. The object in the array is called ndarray. array() function from the numpy library is used to create the arrays in python. Now let’s see how to create a numpy array in python.
Example 1: How to create a numpy array in python
For creating a numpy array in python first, we need to import the numpy library. The second step includes the use of the array() function/method. We can pass lists, arrays, and tuples as a ndarray.
import numpy as np Array = np.array([1,3,5,7,9]) print (Array)
We can also create multi-dimensional arrays using this numpy library. you can find more details related to the numpy array here.
Nearest value in numpy array
Finding the nearest value in the numpy array is very simple and easy using the two functions. these functions are numpy.abs() and numpy.argmin().
The numpy. abs() function is also called numpy.absolute() function. This function helps to calculate the absolute values between the elements of the numpy array. In simple words, we can say that it returns the positive value for each negative value or removes the negative sign of the number.
Example 2: working of numpy.abs() function
Now let’s see the working of numpy.abs() function in python. In example 2 we will just pass one simple array to numpy.abs() function. The output will be the absolute value of all the elements of a numpy array.
import numpy as np array1 = [2,-4,7,15,23,8,24,-5,-10] print ("Absolute Value of array1 : \n", np.absolute(array1))
The numpy. argmin() function always returns an index number of the minimum number present in the numpy array. The numpy minimum function works similarly but it returns the minimum value/ number from the numpy array. Numpy. argmin() function always returns the index of the minimum number. Output is always in the form of an index number.
Example 3: Working of numpy.argmin() function
In example 3 we will see the basic implementation of numpy.argmin function in python.
import numpy as np Array =np.array([1,2,3,6,9,0]) A = np.argmin(Array) print(A)
According to example 3, the numpy array contains six values. The 0 is the smallest/ minimum value among them so, numpy.argmin() function should return the index number of 0 i.e. 5.
Now let’s combine this two function to find out the nearest value in a numpy array.
Example 4: How to find nearest value in numpy array?
import numpy as np Array = np.array([12,13,8,10,11,45]) x =7 # value to which nearest element is to be found print("value is: ",x) dif_Array = np.absolute(Array-x) # use of absolute() function to find the difference index = dif_Array.argmin() # find the index of minimum difference element print("Nearest element to the given values is : ", Array[index]) print("Index of nearest value is : ", index)
In this example 4, we have simply used the combination of both numpy.absolute() function and numpy.argmin() function to solve the problem. First, we imported the numpy library and initialized the numpy array using the array() function. Then set the value to which you want to find the nearest element. In the further steps, we have used the absolute function to find out the difference between each element of the numpy array and our selected value x. These differences are stored in a new array dif_Array. Next, we used the argmin() function to find the index of the minimum number in the dif_Array. So, in this manner, we can find the nearest value in a numpy array.
In this way, we can also find out the nearest value in a multidimensional numpy array.
In this article, we learn about the basics of the numpy library, creating an array using an array() function, and some more functions like absolute(), and argmin() to find the nearest value in a numpy array. I hope you will understand and enjoy this article.