Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在NumPy数组中查找索引和n个最近邻的值_Python_Arrays_Numpy - Fatal编程技术网

Python 在NumPy数组中查找索引和n个最近邻的值

Python 在NumPy数组中查找索引和n个最近邻的值,python,arrays,numpy,Python,Arrays,Numpy,我想知道是否有人能告诉我如何在NumPy数组中找到一个数字的索引和n个最近邻居的索引 例如,在这个数组中,我想找到值87的索引,以及它的四个最近邻居86,88,在左边和78,43,在右边 a = np.random.randint(1,101,15) array([79, 86, 88, 87, 78, 43, 57]) 如果要不时更改值,尽管这对于大型阵列来说会很昂贵,但应执行以下操作: a=np.array([79,86,88,87,78,43,57]) 数字=87 n_最近=4 inde

我想知道是否有人能告诉我如何在
NumPy
数组中找到一个数字的索引和n个最近邻居的索引

例如,在这个数组中,我想找到值
87
的索引,以及它的四个最近邻居
86
88
,在左边和
78
43
,在右边

a = np.random.randint(1,101,15)
array([79, 86, 88, 87, 78, 43, 57])

如果要不时更改值,尽管这对于大型阵列来说会很昂贵,但应执行以下操作:

a=np.array([79,86,88,87,78,43,57])
数字=87
n_最近=4
index=np.where(a==number)[0][0]#np.where返回(数组([3]),)
a=a[max(0,索引-n_最近//2):索引+n_最近//2+1]
nearests=np.delete(a,n_,nearests//2)
打印(最接近)
输出:
[86 88 78 43]

首先,找到从邻居处获得的值的索引(但可能无法处理重复的值)

您应该执行
max(0,索引-2)
,以防所需的值可能位于数组的开头(位置0或1)


然后,从结果中删除该数字。剩下的将是你想要的邻居。

我试过了,但要注意的是,我对python或numpy没有太多的经验,只有几个月的时间

(…所以我还想找其他人加入一个更干净/更简单/更好的方法!)

从functools导入reduce
进口经营者
a=np.数组([5,10,15,12,88,86,5,87,1,2,3,87,1,2,3])
寻找=87
#查找a==87的标记:
np.非零(a==查找)
#变为可重复的
np.nonzero(a==查找[0]
#将您想要的标记和值中的增量放入列表理解中
#从“范围”内的上方生成一个带索引增量的值范围:索引+增量,
#然后将其包装到列表中,以从范围迭代器生成列表:
δ=2
[np中i的列表(范围(i-delta,i+delta+1))。非零(a==87)[0]]
#上面给出了一个列表,需要将列表展平为一个列表
减少(运算符.concat,[list(范围(i-delta,i+delta+1))中的i。非零(a==87)[0]]
#只需要唯一的值,所以一种方法是将上面的值转换为一个集合
设置(np中i的reduce(operator.concat,[list(范围(i-delta,i+delta+1))为非零(a==87)[0]]))
#上面给出了一个包含所有标记的集合,只有唯一的值。
#一个发回的问题是,它仍然可能有<0或>a.size的值,因此
#现在,您可能希望将其全部打包到另一个列表中,以便将其删除
#任何小于0或>a的值

你能再解释一下吗?您希望如何准确定义邻居?在你的例子中,43被认为是87的近邻,而79不是。此外,添加您已经尝试过的内容也很有帮助,值得推荐。
from functools import reduce
import operator

a = np.array([5, 10, 15, 12, 88, 86, 5, 87, 1,2,3, 87,1,2,3])

look_for = 87

# find indicies where a == 87:
np.nonzero(a == look_for)

# get as interable
np.nonzero(a == look_for)[0]

# put into list comprehension with the delta in indicies you want and the values
# from above inside 'range' to generate a range of values b/w index-delta:index+delta,
# then wrap it into a list to generate the list from the range iterator:
delta = 2
[list(range(i-delta,i+delta+1)) for i in np.nonzero(a==87)[0]]

# above gives you a list of lists, need to flatten into a single list
reduce(operator.concat, [list(range(i-delta,i+delta+1)) for i in np.nonzero(a==87)[0]])

# want only unique values, so one way is to turn above into a set
set(reduce(operator.concat, [list(range(i-delta,i+delta+1)) for i in np.nonzero(a==87)[0]]))

# the above gives you a set with all the indicies, with only unique values.
# one remaning problem is it still could have values < 0 or > a.size, so
# you'd now want to wrap it all into another list comprehension to get rid of 
# any values < 0 or > a.size