如何制作逻辑回归(梯度十进制)代码



我是大一和初学者,我在制作逻辑回归算法时遇到了麻烦。我在教科书上附上了代码。我应该填写什么代码?4~5行以内就好了。非常感谢

from sklearn import datasets
import numpy as np
from sklearn.metrics import accuracy_score
X, y = datasets.make_classification(
    n_samples=200, n_features=2, random_state=333,
    n_informative=2, n_redundant=0, n_clusters_per_class=1)
def sigmoid(s):
    return 1 / (1 + np.exp(-s))
def loss(y, h):
    return (-y * np.log(h) - (1 - y) * np.log(1 - h)).mean()
def gradient(X, y, w):
    return -(y * X) / (1 + np.exp(-y * np.dot(X, w)))

X_bias = np.append(np.ones((X.shape[0], 1)), X, axis=1)
y = np.array([[1] if label == 0 else [0] for label in y])
w = np.array([[random.uniform(-1, 1)] for _ in range(X.shape[1]+1)])
max_iter = 100
learning_rate = 0.1
threshold = 0.5
for _ in range(max_iter):
# fill in the blanks

probabilities = sigmoid(np.dot(X_bias, w))
predictions = [[1] if p > threshold else [0] for p in probabilities]
print("loss: %.2f, accuracy: %.2f" %
(loss(y, probabilities), accuracy_score(y, predictions)))

填空

这基本上非常简单。

定义假设函数:

theta0 = 0
theta1 = 0
def hyp(x): return theta0 + theta1*x

定义成本函数:

def cost(hyp, x, y):
    total1 = 0
    total2 = 0
    for i in range(1, len(x)):
        total1 += hyp(x[i]) - y[i]
        total2 += (hyp(x[i]) - y[i]) * x[i]
return total1 / len(x), total2 / len(x)

调用函数:

for i in range(50):
    s1, s2 = cost(hyp, x, y)
    theta1 = theta1 - alpha * s2
    theta0 = theta0 - alpha * s1

学习参数将更新。

最新更新