我是一名新生和初学者,遇到编写逻辑回归算法的困难。我附上了教科书中的代码。请问我应该如何填写空白处?最好在4到5行内完成。非常感谢您。
from sklearn import datasetsimport numpy as npfrom sklearn.metrics import accuracy_scoreX, 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 = 100learning_rate = 0.1threshold = 0.5for _ in range(max_iter):# fill in the blanksprobabilities = 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 = 0theta1 = 0def 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
学习参数将会更新。