[英]how to convert lambda function into a separate function?
I am not sure how to pass argument after converting lambda function into separate function.我不确定如何在将 lambda 函数转换为单独的函数后传递参数。
Here is the original codes which works.这是有效的原始代码。
from sklearn.neighbors import BallTree
X = np.array([[-1, -1], [-2, -1], [-3, -2], [1, 1], [2, 1], [3, 2]])
aspect=2
tree = BallTree(X,metric="pyfunc",func=lambda p0, p1: math.sqrt((p1[0] - p0[0]) * (p1[0] - p0[0]) + (p1[1] - p0[1]) * (p1[1] - p0[1]) * aspect))
since the above lambda function is too long, i'd like define a separate function fromthis lambda function.由于上面的 lambda 函数太长,我想从这个 lambda 函数中定义一个单独的函数。
def elipse(p0,p1,aspect):
a=math.sqrt((p1[0] - p0[0]) * (p1[0] - p0[0]) + (p1[1] - p0[1]) * (p1[1] - p0[1]) * aspect)
return a
tree = BallTree(X,metric="pyfunc",func=elipse(X.iloc[:,0],X.iloc[:,1],aspect))
then i got an error of TypeError: 'float' object is not callable
然后我得到了一个
TypeError: 'float' object is not callable
错误TypeError: 'float' object is not callable
I am not sure how to pass p0 and p1 , could anyone help me?我不确定如何通过 p0 和 p1 ,有人可以帮助我吗? Thanks
谢谢
由于elipse()
接受 3 个参数,但BallTree()
仅提供 2 个参数,您仍然可以使用lambda
,但它更简单,因为长方程在函数中。
tree = BallTree(X,metric="pyfunc",func=lambda p0, p1: elipse(p0, p1, aspect))
Since you now have a named function, you just need to tell BallTree
what that name is:既然你现在有了一个命名函数,你只需要告诉
BallTree
这个名字是什么:
tree = BallTree(X,metric="pyfunc",func=elipse)
Also, you don't need to have aspect
as a parameter:此外,您不需要将
aspect
作为参数:
def elipse(p0, p1):
aspect = 2
return math.sqrt((p1[0] - p0[0]) * (p1[0] - p0[0]) + (p1[1] - p0[1]) * (p1[1] - p0[1]) * aspect)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.