[英]How to rewrite a given nearest-neighbor function with only using numpy and without loops?
I have a homework on nearest neighbor algorithm using python. 我有一个使用python的最近邻算法的作业。 I have a given code in pure python that contains a loop.
我在纯python中有一个包含循环的给定代码。 I have to rewrite and configure the function only using numpy and without loops.
我必须使用numpy和without循环重写和配置该函数。
I have an unlabeled point u, that needs to be classified, a distance function and a training set (X, Y). 我有一个未标记的点u,需要分类,距离函数和训练集(X,Y)。 The function that I have to write should return the label of the point that has the smallest distance to u.
我必须编写的函数应该返回与u具有最小距离的点的标签。
Here is the function written in pure python that I have to rewrite: 这是用纯python编写的函数,我必须重写:
def pynearest(u, X, Y, distance=pydistance):
xbest = None
ybest = None
dbest = float('inf')
for x, y in zip(X, Y):
d = distance(u, x)
if d < dbest:
ybest = y
xbest = x
dbest = d
return ybest
The process of rewriting for loops in Python is called vectorization. 在Python中重写循环的过程称为矢量化。 To solve this, you generally have to use indexes to retrieve the values in your matrix.
要解决此问题,通常必须使用索引来检索矩阵中的值。 I would suggest you to look at this questions: Vectorizing for loops NumPy vectorizing a for loop in numpy/scipy?
我建议你看看这个问题: 向量化 循环NumPy 在numpy / scipy中向量化循环吗? vectorizing a for loop in python
矢量化python中的for循环
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.