从Python开始进行随机Logistic回归

时间:2020-10-10 03:20:16

标签: python numpy data-science logistic-regression

我在将逻辑回归梯度转换为numpy操作并获得预期结果方面遇到困难。当我打印我的theta时,它由一个数组组成。我认为这导致操作无法依次正确执行,因为我的系数基本上都等于0。

有人可以指出我的错误吗?

def sigmoid(z):
    return 1 / (1 + np.exp(-z))

k = 1
K = 1000
vert = (dim_train, 1)
hrza = (1, dim_train)
theta = np.random.normal(0, 1, size=(784,1))

f = np.array([])

while k <= K:
    for i in range(0, n_train):
        s = random.randint(0, n_train-1)
        y = y_train[s,0]
        x = x_train[s,:].reshape(784,1)
        xtheta = x.T@theta
        sig = sigmoid(xtheta)[0][0]
        w = y - sig
        grad = x*w
        theta = theta * 1*grad
    k += 1
    fun = 0.
#     for i in range(0, n_train):
#         x = x_train[i,:].reshape(hrza)
#         y = y_train[i,:][0]        
        
#         s1 = y_train[s,:]*np.log(np.exp(np.dot(x_train[s,:], theta))/(1+np.exp(np.dot(x_train[s,:], theta))))
#         s3 = 1 - np.exp(np.dot(x_train[s,:], theta))/(1+np.exp(np.dot(x_train[s,:], theta)))
#         s2 = (1-y_train[s,:])*np.log(s3)
#         fun = fun + s1 + s2
#     f = np.append(f, fun)
print(theta)
#print(f)

0 个答案:

没有答案