I will write it because it is unexpectedly packed and there is no good summary.
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
data = [1, 1 ,1 ,0.5 ,2 ,3 ,-1]
value = 0.8
n = idx_of_the_nearest(data, value)
n
#0
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
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]
'''
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
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)]
'''
But don't say what "plural" means. That is still the case in mathematics (I have intentionally made such concrete examples).
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.
data = [1, 1 ,1 ,0.5 ,2 ,3 ,-1]
value = 0.8
indices_of_the_nearest(data, value)
'''
array([0, 1, 2])
'''
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.
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!
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.
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)
Taking out the subscript was unexpectedly difficult, but I hope it helps someone someday.
Recommended Posts