【发布时间】:2021-09-25 09:42:26
【问题描述】:
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)
从逻辑回归函数返回的系数不正确,但我不确定错误是什么。有人可以帮忙吗?
【问题讨论】:
标签: python machine-learning logistic-regression