{"id":45023,"date":"2023-02-27T09:43:09","date_gmt":"2023-02-27T09:43:09","guid":{"rendered":"https:\/\/www.askpython.com\/?p=45023"},"modified":"2023-02-27T15:52:34","modified_gmt":"2023-02-27T15:52:34","slug":"nearest-value-in-numpy-array","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python-modules\/numpy\/nearest-value-in-numpy-array","title":{"rendered":"How To Find Nearest Value In Numpy Array?"},"content":{"rendered":"\n<p>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&#8217;ll see the details of a numpy array and how to find the nearest value in a numpy array. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"numpy-array\">Numpy array<\/h2>\n\n\n\n<p>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&#8217;s see how to create a numpy array in python.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"example-1-how-to-create-a-numpy-array-in-python\">Example 1: How to create a numpy array in python<\/h3>\n\n\n\n<p>For creating a numpy array in python first, we need to import the <strong>numpy<\/strong> library. The second step includes the use of the array() function\/method. We can pass lists, arrays, and tuples as a<em> <\/em><strong>ndarray<\/strong>. <\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport numpy as np\nArray = np.array(&#x5B;1,3,5,7,9])\nprint (Array)\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\" id=\"output\">Output:<\/h3>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"285\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/02\/creating_numpy_array-1024x285.png\" alt=\"Creating Numpy Array\" class=\"wp-image-45548\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/02\/creating_numpy_array-1024x285.png 1024w, https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/02\/creating_numpy_array-300x84.png 300w, https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/02\/creating_numpy_array-768x214.png 768w, https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/02\/creating_numpy_array.png 1202w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption class=\"wp-element-caption\">Creating Numpy Array<\/figcaption><\/figure>\n\n\n\n<p>We can also create multi-dimensional arrays using this numpy library. you can find more details related to the numpy array <a href=\"https:\/\/www.askpython.com\/python-modules\/numpy\/python-numpy-arrays\" data-type=\"post\" data-id=\"1070\">here<\/a>. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"nearest-value-in-numpy-array\">Nearest value in numpy array<\/h2>\n\n\n\n<p>Finding the nearest value in the numpy array is very simple and easy using the two functions. these functions are <strong>numpy.abs()<\/strong> and <strong>numpy.argmin().<\/strong> <\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"numpy-abs-function\">numpy.abs() function<\/h3>\n\n\n\n<p>The <strong>numpy. abs()<\/strong> function is also called <strong>numpy.absolute()<\/strong> 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.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"example-2-working-of-numpy-abs-function\">Example 2: working of numpy.abs() function<\/h3>\n\n\n\n<p>Now let&#8217;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. <\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport numpy as np \narray1 = &#x5B;2,-4,7,15,23,8,24,-5,-10]\nprint (&quot;Absolute Value of array1 : \\n&quot;, np.absolute(array1))\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\" id=\"output-1\">Output:<\/h3>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"251\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/02\/numpy.abs_function-1024x251.png\" alt=\"Numpy Abs Function\" class=\"wp-image-45591\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/02\/numpy.abs_function-1024x251.png 1024w, https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/02\/numpy.abs_function-300x74.png 300w, https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/02\/numpy.abs_function-768x189.png 768w, https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/02\/numpy.abs_function.png 1128w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption class=\"wp-element-caption\">Numpy.abs() Function<\/figcaption><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"numpy-argmin-function\">numpy.argmin() function<\/h3>\n\n\n\n<p>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.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"example-3-working-of-numpy-argmin-function\">Example 3: Working of numpy.argmin() function<\/h3>\n\n\n\n<p>In example 3 we will see the basic implementation of numpy.argmin function in python. <\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport numpy as np\nArray =np.array(&#x5B;1,2,3,6,9,0])\nA = np.argmin(Array)\nprint(A)\n<\/pre><\/div>\n\n\n<p>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. <\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"output-2\">Output:<\/h3>\n\n\n\n<figure class=\"wp-block-image alignwide size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"261\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/02\/numpy.argmin_function-1024x261.png\" alt=\"Numpy Argmin Function\" class=\"wp-image-45822\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/02\/numpy.argmin_function-1024x261.png 1024w, https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/02\/numpy.argmin_function-300x76.png 300w, https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/02\/numpy.argmin_function-768x195.png 768w, https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/02\/numpy.argmin_function.png 1242w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption class=\"wp-element-caption\">Numpy.argmin() Function<\/figcaption><\/figure>\n\n\n\n<p>Now let&#8217;s combine this two function to find out the nearest value in a numpy array.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"example-4-how-to-find-nearest-value-in-numpy-array\">Example 4: How to find nearest value in numpy array?<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport numpy as np\n\nArray = np.array(&#x5B;12,13,8,10,11,45])\n\nx =7 # value to which nearest element is to be found\nprint(&quot;value is: &quot;,x)\n \ndif_Array = np.absolute(Array-x) # use of absolute() function to find the difference \n \nindex = dif_Array.argmin() # find the index of minimum difference element\nprint(&quot;Nearest element to the given values is : &quot;, Array&#x5B;index])\nprint(&quot;Index of nearest value is : &quot;, index)\n<\/pre><\/div>\n\n\n<p>In this example 4, we have simply used the combination of both <strong>numpy.absolute()<\/strong> function and <strong>numpy.argmin()<\/strong> function to solve the problem. First, we imported the numpy library and initialized the numpy array using the <strong>array()<\/strong> 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 <strong>dif_Array<\/strong>. Next, we used the <strong>argmin()<\/strong> 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.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"output-3\">Output:<\/h3>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"252\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/02\/nearest_value_using_numpy_library-1024x252.png\" alt=\"Nearest Value Using Numpy Library\" class=\"wp-image-45832\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/02\/nearest_value_using_numpy_library-1024x252.png 1024w, https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/02\/nearest_value_using_numpy_library-300x74.png 300w, https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/02\/nearest_value_using_numpy_library-768x189.png 768w, https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/02\/nearest_value_using_numpy_library.png 1354w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption class=\"wp-element-caption\">Nearest Value Using Numpy Library in Python<\/figcaption><\/figure>\n\n\n\n<p>In this way, we can also find out the nearest value in a <a href=\"https:\/\/www.askpython.com\/python\/array\/multidimensional-arrays\" data-type=\"post\" data-id=\"41390\">multidimensional<\/a> numpy array. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"conclusion\">Conclusion<\/h2>\n\n\n\n<p>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. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"references\">References<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Here&#8217;s a <a href=\"https:\/\/numpy.org\/doc\/stable\/reference\/generated\/numpy.argmin.html\" target=\"_blank\" rel=\"noopener\">numpy documentation <\/a>for the numpy.argmin function, which finds the indices of the minimum values in an array.<\/li>\n\n\n\n<li>Do check out <a href=\"https:\/\/numpy.org\/doc\/stable\/reference\/generated\/numpy.absolute.html\" target=\"_blank\" rel=\"noopener\">this documentation<\/a> for the NumPy absolute function, which returns the absolute value of a given number or array.<\/li>\n<\/ul>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 [&hellip;]<\/p>\n","protected":false},"author":63,"featured_media":45840,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[93],"tags":[],"class_list":["post-45023","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-numpy"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/45023","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/users\/63"}],"replies":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/comments?post=45023"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/45023\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media\/45840"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=45023"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=45023"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=45023"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}