简体   繁体   中英

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

Trying to implement Logistic Regression in Python:

Below is the Cost Function:

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

Below is the Gradient Function:

def gradientDesc(theta_tuple):
   theta_matrix = np.transpose(np.mat(theta_tuple))

   H_x = 1 / (1 + np.exp(-X_matrix * theta_matrix))
   G_theta0 = (sum(np.multiply(H_x - Y_matrix, X_matrix[:, 0])) / m)[0, 0]
   G_theta1 = (sum(np.multiply(H_x - Y_matrix, X_matrix[:, 1])) / m)[0, 0]
   G_theta2 = (sum(np.multiply(H_x - Y_matrix, X_matrix[:, 2])) / m)[0, 0]


   return np.array((G_theta0, G_theta1, G_theta2))

Then I run the optimize.fmin_bfgs function, as below:

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

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

Then I got the error below:

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

Could anyone tell me how to fix? :) Thanks!

For the args parameter you should specify a single-item tuple (also known as a singleton ) with a comma instead; otherwise the parentheses do nothing more than grouping the expression.

Change:

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

to:

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

Also, your gradientDesc should accept an additional parameter per the documentation .

Change:

def gradientDesc(theta_tuple):

to:

def gradientDesc(x, theta_tuple):

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM