【发布时间】:2018-01-30 09:08:04
【问题描述】:
我需要定义一个函数,它读取一个 numpy 数组并为数组中与 p 数最近的 k 个点生成平均值。
例子:
array= np.array([1, 2, 3, 4, 5, 6, 7, 50, 24, 32, 9, 11, 12, 10])
p= 15 (**Note this is not a number in the array, I will need to find the
number closest to p or p number itself)
k = 3
In this case, I would need to generate the mean for ([11, 12, 10)]
as they are closest to p = 15
使用上述数字,我需要找到最接近 p 的 k 个点的平均值,并且 p 可以在数组中明确说明,也可能不明确说明。
我是新手,在这一点上非常困惑,觉得我已经用尽了我的资源。我觉得以前有人问过这个问题,但答案对于我所需要的来说太复杂了。
提前致谢。
【问题讨论】:
-
"Can Someone Help Me?" is not a valid SO question。这通常表明您需要的是与当地导师一起半小时或完成教程,而不是 Stack Overflow。
-
这是作业吗?你想用什么样的东西?在这个非常微不足道的情况下,您应该从一点一点分解问题开始。为
array中的每个值查找与p的距离,也许用这些值创建一个新数组,然后对该新数组进行排序,并取前三个值... -
欢迎来到 StackOverflow。请阅读并遵循帮助文档中的发布指南。 on topic 和 how to ask 在这里申请。
标签: python function numpy knn nearest-neighbor