import json from math import exp x = [-2, -1, 1, 2] y = [0, 0, 1, 1] rate = 1.0 # 截距和斜率从零开始,sigmoid(0)=0.5;交叉熵对线性分数的导数为 p-y。 errors = [0.5 - label for label in y] intercept = -rate * sum(errors) / len(x) slope = -rate * sum(value * error for value, error in zip(x, errors)) / len(x) # 用更新后的系数对留出的 x=1 计算正类概率。 probability = 1 / (1 + exp(-(intercept + slope))) assert intercept == 0 and slope == 0.75 and 0.67 < probability < 0.69 result = {"intercept": intercept, "slope": slope, "probability_at_one": probability} print("LAB_RESULT " + json.dumps(result, ensure_ascii=False))