我的逻辑回归代码有问题

时间:2021-07-16 15:19:02

标签: python machine-learning logistic-regression

def calc_log(values):
    temp1 = np.exp(-values)
    temp2 = 1+temp1
    return 1/temp2

def logisticregression(x, y, a):
    #a is the learning rate
    m = len(y)
    x = np.matrix(x)
    x = np.c_[np.ones((x.shape[0], 1)), x]
    # adds a row of ones at the start of the x matrix which represents x0 (which is multiplied by theta[0])
    theta = np.matrix(np.zeros(x.shape[1])).T
    #makes a list of 0s as starting theta values with the same number of features that the x matrix has
    y = np.matrix(y)
    while True:
        calculation = x * theta #multiplies x and theta
        hypo = calc_log(calculation) #f(z) = 1/(1+e^-z), plugs calculation into f(z)
        difference = hypo - y.T #calculates the difference between the predicted y values and the real y values
        vals = x.T * difference #multiplies the difference by each feature for every piece of training data 
        lasttheta = theta.copy() 
        theta -= (vals*(a/m)) #multiplies the matrix of theta values by a/m and negate it from the previous theta values
        array = lasttheta - theta 
        array = [j for j in array if abs(float(j)) > 0.001] #checks if the difference between each theta value and the previous theta value is more than 0.001. 
        if not array:
            break #Breaks from the loop if the difference between each theta value and its previous theta value is less than 0.001
    return theta

X_vals = np.array([[3, 5, 6], [4, 2, 4], [8, 6, 2]])
Y_vals = np.array([1, 0, 1])

value = logisticregression(X_vals, Y_vals, 0.1)

从逻辑回归函数返回的系数不正确,但我不确定错误是什么。有人可以帮忙吗?

逻辑回归的梯度下降公式: Logistic Regression Gradient Descent

1 个答案:

答案 0 :(得分:0)

问题似乎混淆了“i”和“j”。您可以通过设置m=1来检查它并重新阅读代码。

import numpy as np

def calc_log(values):
    temp1 = np.exp(-values)
    temp2 = 1+temp1
    return 1/temp2

def logisticregression(x, y, a):
    #a is the learning rate
    m = len(y)
    # x = np.matrix(x)  # x has been a 2d array
    x = np.c_[np.ones((x.shape[0], 1)), x]
    # adds a row of ones at the start of the x matrix which represents x0 (which is multiplied by theta[0])
    theta = np.zeros(x.shape[1])
    #makes a list of 0s as starting theta values with the same number of features that the x matrix has
    # y = np.matrix(y)  # y should be a vector here
    while True:
        calculation = theta @ x.T #multiplies x and theta, or you can use np.dot(theta, x.T) for python < 3.5
        hypo = calc_log(calculation) #f(z) = 1/(1+e^-z), plugs calculation into f(z)
        difference = hypo - y.T #calculates the difference between the predicted y values and the real y values
        vals = np.multiply(x, difference[:, np.newaxis]) #multiplies the difference by each feature for every piece of training data 
        lasttheta = theta.copy() 
        theta -= (vals.sum(0)*(a/m)) #multiplies the matrix of theta values by a/m and negate it from the previous theta values
        array = lasttheta - theta 
        array = [j for j in array if abs(float(j)) > 0.001] #checks if the difference between each theta value and the previous theta value is more than 0.001. 
        loss = np.abs(hypo-y.T).sum()
        print(f'loss: {loss}')
        if not array:
            break #Breaks from the loop if the difference between each theta value and its previous theta value is less than 0.001
    return theta

X_vals = np.array([[3, 5, 6], [4, 2, 4], [8, 6, 2]])
Y_vals = np.array([1, 0, 1])

value = logisticregression(X_vals, Y_vals, 0.1)
相关问题