TypeError:gradientDesc()只接受1个参数(给定4个)

2024-03-29 11:08:42 发布

您现在位置:Python中文网/ 问答频道 /正文

尝试在Python中实现Logistic回归:

成本函数如下:

def costFunction(theta_array):
   m = len(X1)
   theta_matrix = np.transpose(np.mat(theta_array))

   H_x = 1 / (1 + np.exp(-X_matrix * theta_matrix))
   J_theta = ((sum(np.multiply((-Y_matrix), np.log(H_x)) - np.multiply((1 - Y_matrix), np.log(1 - H_x)))) / m )[0, 0]

   return J_theta

梯度函数如下:

^{pr2}$

然后我运行优化.fmin_bfgs功能如下:

initial_theta = np.zeros((3, 1))
theta_tuple = (0, 0, 0)

theta_optimize = op.fmin_bfgs(costFunction, initial_theta, gradientDesc, args = (theta_tuple))

然后我得到了以下错误:

**TypeError: gradientDesc() takes exactly 1 argument (4 given)**

谁能告诉我怎么修理吗?:)谢谢!在


Tags: 函数lognparraymatrixmultiplyinitial成本
1条回答
网友
1楼 · 发布于 2024-03-29 11:08:42

对于args参数,您应该使用逗号指定一个单项元组(也称为singleton),否则括号只会对表达式进行分组。在

更改:

theta_optimize = op.fmin_bfgs(costFunction, initial_theta, gradientDesc, args = (theta_tuple))

收件人:

^{pr2}$

另外,您的gradientDesc应该为documentation接受一个额外的参数。在

更改:

def gradientDesc(theta_tuple):

收件人:

^{4}$

相关问题 更多 >