我有一个函数:可选参数
cost(X, model, reg = 1e-3, sparse)
,我需要这个函数传递到另一个形式下:
f(X, model)
f(X, model, reg = reg)
我使用lambda来做到这一点:
f = lambda X, model: cost(X, model, sparse = np.random.rand(10,10))
和python抱怨lambda有一个意外的论据reg。我如何正确地做到这一点?
如果我做的其他方式:
f = lambda X, model, reg: cost(X, model, reg = reg, sparse = np.random.rand(10,10))
,则它不会在第一种情况下工作。
在函数调用中,关键字参数必须跟随位置参数。所以我认为你的“成本”函数应该类似于成本(X,model,reg = 1e-3,sparse ='somethingelse'或'cost(X,model,sparse,req = 1e-3)' – poros