[Python] Summary of functions that return the index that takes the closest value in the array

I will write it because it is unexpectedly packed and there is no good summary.

1 When not considering the duplication of the closest value

1.1 Simplest version

When there is one value you want to find and one index you want to return. This is the easiest. The data to be searched is assumed to be a one-dimensional array.

import numpy as np

def idx_of_the_nearest(data, value):
    idx = np.argmin(np.abs(np.array(data) - value))
    return idx

1.2 Specific example

data = [1, 1 ,1 ,0.5 ,2 ,3 ,-1]
value =  0.8
n = idx_of_the_nearest(data, value)
n
#0

1.3 When there are multiple values ​​you want to find

I want to be able to specify the value I want to find in a list. The function for that is as follows.

import numpy as np

def idx_of_the_nearest(data, value):
    print('value:', type(value))
    if type(value) == float:
        idx = np.argmin(np.abs(np.array(data) - value))
        #print(np.abs(np.array(data) - value))
        return idx
    if type(value) == list:
        idx = [None]*len(value)
        for i in range(len(value)):
            idx[i] = np.argmin(np.abs(np.array(data) - value[i]))
            #idx[i] = [value[i], np.argmin(np.abs(np.array(data) - value[i]))] #May be
            #print(np.abs(np.array(data) - value[i]))
        return idx

1.4 Specific example

data = [1, 1 ,1 ,0.5 ,2 ,3 ,-1]
value =  [0.8,0.7]
n = idx_of_the_nearest(data, value)
n
'''Result
value: <class 'list'>
[0, 3]
'''

1.5 Multidimensional extension

Click here if you want to return the index of the multidimensional array when the data to be searched is a multidimensional array. The output is a list of tuples. Use something called np.unravel_index ().

import numpy as np

def idx_of_the_nearest(data, value):
    print('value:', type(value))
    if type(value) == float:
        idx = np.argmin(np.abs(np.array(data) - value))
        #print(np.abs(np.array(data) - value))
        return idx
    if type(value) == list:
        idx = [None]*len(value)
        for i in range(len(value)):
            idx[i] = np.unravel_index(np.argmin(np.abs(np.array(data) - value[i])) , np.array(data).shape)
            #print(np.abs(np.array(data) - value[i]))
        return idx

1.6 Specific example

data = [[1, 1 ,1 ,0.5] ,[2 ,3 ,-1,0]]
value = [0.8, 0.7, 2]
idx_of_the_nearest(data, value)
'''
value: <class 'list'>
[(0, 0), (0, 3), (1, 0)]
'''

2 When there are multiple closest values

But don't say what "plural" means. That is still the case in mathematics (I have intentionally made such concrete examples).

2.1 Simple version

Click here if you want to return multiple subscripts in the simplest way. When there is only one value you want to find in a one-dimensional array.

import numpy as np

def indices_of_the_nearest(data, value):
    distance = np.abs(np.array(data) - value)
    indices = np.where(distance == np.min(distance))[0]
    return indices

This np.where () is a musician, and it is unexpectedly difficult to use.

However, in the simple version, this is the optimal solution, as you can see in the concrete example. By adding [0], array is returned.

2.2 Specific example

data = [1, 1 ,1 ,0.5 ,2 ,3 ,-1]
value = 0.8
indices_of_the_nearest(data, value)
'''
array([0, 1, 2])
'''

2.3 When there are multiple values ​​to be searched for (can be used in multiple dimensions, but there is room for improvement)

Click here for "greedy" people who want to search for multiple values. However, there is room for improvement.

import numpy as np

def indices_of_the_nearest(data, value):
    print('value:', type(value))
    if type(value) == float:
        distance = np.abs(np.array(data) - value)
        indices = np.where(distance == np.min(distance))
        #print(np.abs(np.array(data) - value))
        return indices
    if type(value) == list:
        indices = [None]*len(value)
        for i in range(len(value)):
            distance = np.abs(np.array(data) - value[i])
            indices[i] = np.where(distance == np.min(distance))
            #print(np.abs(np.array(data) - value[i]))
        return indices

I just applied section 1.3.

2.4 Specific example

data = [[1, 1 ,1 ,0.5] ,[2 ,3 ,-1,0]]
value = [0.8,0.7]
indices_of_the_nearest(data, value)
'''
value: <class 'list'>
[(array([0, 0, 0]), array([0, 1, 2])), (array([0]), array([3]))]
'''

The row number and column number are output separately and are difficult to understand (the meaning of the first element is the closest value to [0] [0], [0] [1], and [0] [2]. Meaning).

If you really want to use it, you can use it, but it doesn't make much sense for this purpose. There was no problem when the data was one-dimensional, but it became more difficult to see when it became three-dimensional.

data = [[[1, 1] ,[1 ,0.5]] ,[[2 ,3] ,[-1,0]]]
value = [0.8,0.7]
indices_of_the_nearest(data, value)
'''
value: <class 'list'>
[(array([0, 0, 0]), array([0, 0, 1]), array([0, 1, 0])),
 (array([0]), array([1]), array([1]))]
'''

The first tuple in the list has the closest value to [0] [0] [0], [0] [0] [1], [0] [1] [0] meaning.

This is not reading the inside of array as it is, but the i (i = 0, 1, 2, 3) th and array ([0, 0, 1]) of array ([0, 0, 0]) . ]) I read by connecting the i-th of and the i-th ofarray ([0, 1, 0])(the meaning can be understood by looking at the second tuple in the list together).

Hard to see!

2.5 Expanded to multiple dimensions (improved version)

So let's improve it.

import numpy as np

def indices_of_the_nearest(data, value):
    print('value:', type(value))
    if type(value) == float:
        distance = np.abs(np.array(data) - value)
        indices = np.where(distance == np.min(distance))
        #print(np.abs(np.array(data) - value))
        return indices
    if type(value) == list:
        indices = [None]*len(value)
        for i in range(len(value)):
            distance = np.abs(np.array(data) - value[i])
            indices[i] = np.array((np.where(distance == np.min(distance)))).T #Transpose
            #print(np.abs(np.array(data) - value[i]))
        return indices

What I'm doing is indices[i] = np.array((np.where(distance == np.min(distance)))).T I just rewrote it.

2.6 Specific example

data = [[1, 1 ,1 ,0.5] ,[2 ,3 ,-1,0]]
value = [0.8,0.7]
indices_of_the_nearest(data, value)
'''
value: <class 'list'>
[array([[0, 0],
        [0, 1],
        [0, 2]]),
 array([[0, 3]])]
'''
data = [[[1, 1] ,[1 ,0.5]] ,[[2 ,3] ,[-1,0]]]
value = [0.8,0.7]
indices_of_the_nearest(data, value)
'''
value: <class 'list'>
[array([[0, 0, 0],
        [0, 0, 1],
        [0, 1, 0]]),
 array([[0, 1, 1]])]
'''

Easy to see! (If you want to extract an element from the output, you can do it by turning the for statement in the comprehension notation)

3 Summary

Taking out the subscript was unexpectedly difficult, but I hope it helps someone someday.

Recommended Posts

[Python] Summary of functions that return the index that takes the closest value in the array
Find the divisor of the value entered in python
[Python numpy] Dynamically specify the index of the array
Output in the form of a python array
[Golang] Specify an array in the value of map
The story that the return value of tape.gradient () was None
[Introduction to Python] Summary of functions and methods that frequently appear in Python [Problem format]
Code that sets the default value in case of AttributeError
[Memo] The mystery of cumulative assignment statements in Python functions
Find the index of the maximum value (minimum value) of a multidimensional array
Get index of nth largest / smallest value in list in Python
Get the index of each element of the confusion matrix in Python
Get index of nth largest / smallest value in list in Python
Get the value of a specific key up to the specified index in the dictionary list in Python
About the return value of pthread_mutex_init ()
About the return value of the histogram.
A function that measures the processing time of a method in python
A simple reason why the return value of round (2.675,2) is 2.67 in python (it should be 2.68 in reality ...)
Check the behavior of destructor in Python
[Fundamental Information Technology Engineer Examination] I wrote an algorithm for the maximum value of an array in Python.
Summary of various for statements in Python
[Python] Summary of array generation (initialization) time! !! !!
The result of installing python in Anaconda
The basics of running NoxPlayer in Python
Summary of built-in methods in Python list
In search of the fastest FizzBuzz in Python
Get the return value of an external shell script (ls) with python3
[Python] Combine all the elements in the array
Find the index of items that match the conditions in the pandas data frame / series
Compare the sum of each element in two lists with the specified value in Python
How to find the coefficient of the trendline that passes through the vertices in Python
Get the last element of the array by splitting the string in Python and PHP
Output the number of CPU cores in Python
Correspondence summary of array operation of ruby and python
[Python] Sort the list of pathlib.Path in natural sort
Summary of the differences between PHP and Python
Summary of how to import files in Python 3
Get the caller of a function in Python
Match the distribution of each group in Python
View the result of geometry processing in Python
Watch out for the return value of __len__
Make a copy of the list in Python
Summary of how to use MNIST in Python
Testing methods that return random values in Python
Summary of evaluation functions used in machine learning
Find the solution of the nth-order equation in python
Pre-process the index in Python using Solr's ScriptUpdateProcessor
The story of reading HSPICE data in Python
[Note] About the role of underscore "_" in Python
About the behavior of Model.get_or_create () of peewee in Python
Solving the equation of motion in Python (odeint)
Search by the value of the instance in the list
The one that displays the progress bar in Python
Summary of Excel operations using OpenPyXL in Python
Lambda expression (correction) that creates an index (dictionary with members as keys) of the members of the object being collected in python
About the matter that the contents of Python print are not visible in docker logs
Summary of statistical data analysis methods using Python that can be used in business
Get the value of a specific key in a list from the dictionary type in the list with Python
Return value of quit ()-Is there anything returned by the "function that ends everything"?
[Python] A program that finds the shortest number of steps in a game that crosses clouds
A memo that implements the job of loading a GCS file into BigQuery in Python