我想使用计算的逻辑回归,所以我使用的是fmin_bfgs。 但是在尝试使用渐变时,会出现如下错误:
ValueError: operands could not be broadcast together with shapes (5,25) (5,)
虽然没有使用fmin_bfgs,但它可以很好地计算渐变。
以下是我的代码部分:
theta, J = fmin_bfgs(costfunction, \
initial_theta , fprime = gradient, args = (X,y==c,lmd))
all_theta[:,c] = theta
以下是我计算渐变的方法:
def gradient(theta,X,y,lmd):
m = len(y)
n = len(theta)
z = np.dot((np.transpose(X)),theta)
h = sigmoid(z)
y = np.reshape( y , (-1,1))
h.reshape(m,1)
grad = np.dot( X , (h-y) )/m
#print('grad shape %d'%(grad.shape))
print('grad %d'%(grad))
temp = theta
temp[0] = 0
grad = grad + (lmd/m)*temp
return grad
答案 0 :(得分:0)
感谢您的回复。我可以找出问题所在。我不得不从'渐变'功能中删除'重塑'线。
def gradient(theta,X,y,lmd):
m = len(y)
n = len(theta)
z = np.dot(X , theta)
h = sigmoid(z)
#y = np.reshape( y , (-1,1)) <-- removed this
#h.reshape(m,1) <-- removed this
grad = np.dot( np.transpose(X) , (h-y) )/m
temp = theta
temp[0] = 0
grad = grad + (lmd/m)*temp
return grad
谢谢大家!