python中带正则化的Logistic回归未能最小化

2024-10-01 09:26:50 发布

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

我正在基于Coursera文档实现logistic回归,包括python和Octave。 在Octave中,我设法做到了这一点,并获得了正确的训练精度,但是在python中,由于我无法访问fminunc,所以我无法找到解决方法。在

目前,这是我的代码:

df = pandas.DataFrame.from_csv('ex2data2.txt', header=None, index_col=None)
df.columns = ['x1', 'x2', 'y']
y = df[df.columns[-1]].as_matrix()
m = len(y)
y = y.reshape(m, 1)
X = df[df.columns[:-1]]
X = X.as_matrix()

from sklearn.preprocessing import PolynomialFeatures

feature_mapper = PolynomialFeatures(degree=6)
X = feature_mapper.fit_transform(X)

def sigmoid(z):
    return 1/(1+np.power(np.e, z))

def cost_function_reg(theta):
    _theta = theta.copy().reshape(-1, 1)
    shifted_theta = np.insert(_theta[1:], 0, 0)
    h = sigmoid(np.dot(X, _theta))
    reg = (_lambda / (2.0*m))* shifted_theta.T.dot(shifted_theta)
    J = ((1.0/m)*(-y.T.dot(np.log(h)) - (1 - y).T.dot(np.log(1-h)))) + reg
    return J

def gradient(theta):
    _theta = theta.copy().reshape(-1, 1)
    shifted_theta = np.insert(_theta[1:], 0, 0)
    h = sigmoid(np.dot(X, _theta))
    gradR = _lambda*shifted_theta
    gradR.shape = (gradR.shape[0], 1)
    grad = (1.0/m)*(X.T.dot(h-y)+gradR)
    return grad.flatten()

from scipy.optimize import *
theta = fmin_ncg(cost_f, initial_theta, fprime=gradient)
predictions = predict(theta, X)
accuracy = np.mean(np.double(predictions == y)) * 100
print 'Train Accuracy: %.2f' % accuracy

输出为:

^{pr2}$

在倍频程中,准确度为:83.05。在

感谢任何帮助。在


Tags: columnsfromnonedfreturndefnpreg
1条回答
网友
1楼 · 发布于 2024-10-01 09:26:50

在这方面存在两个问题:

第一种方法fmin_ncg不适合最小化。我在上一个练习中用过它,但是它没有找到θ和梯度函数,这是理想的倍频程。在

切换到

theta = fmin_bfgs(cost_function_reg, initial_theta)

修正了这个问题。在

第二个问题是计算错误。 一旦我用fmin_bfgs进行优化,并达到与倍频程结果(0.529)相匹配的成本,(predictions == y)部分具有不同的形状((118, 118)和{}),得到的矩阵是{},而不是向量。在

相关问题 更多 >