x xx，模型参数为 w w w和 b b b，则其输出结果可以表示为：

y ^ = σ ( w T x + b ) \hat{y} = \sigma(w^Tx + b) y^​=σ(wTx+b)

σ ( z ) = 1 1 + e − z \sigma(z) = \frac{1}{1+e^{-z}} σ(z)=1+e−z1​

sigmoid函数的输出值在0到1的范围内，可以被解释为预测结果为正例的概率。

，使得模型的预测结果与真实标签之间的差异最小化。具体来说，MLE的目标是最大化所有训练样本上的似然函数：

L ( w , b ) = ∏ i = 1 n P ( y i ∣ x i ; w , b ) L(w,b) = \prod_{i=1}^n
P(y_i|x_i;w,b)L(w,b)=i=1∏n​P(yi​∣xi​;w,b)

b ) P(y_i|x_i;w,b)P(yi​∣xi​;w,b)表示为：

P ( y i ∣ x i ; w , b ) = y ^ i y i ( 1 − y ^ i ) 1 − y i P(y_i|x_i;w,b) =
\hat{y}_i^{y_i}(1-\hat{y}_i)^{1-y_i}P(yi​∣xi​;w,b)=y^​iyi​​(1−y^​i​)1−yi​

J ( w , b ) = − 1 n ∑ i = 1 n [ y i log ⁡ ( y ^ i ) + ( 1 − y i ) log ⁡ ( 1 −
y ^ i ) ] J(w,b) = -\frac{1}{n}\sum_{i=1}^n [y_i\log(\hat{y}_i) +
(1-y_i)\log(1-\hat{y}_i)]J(w,b)=−n1​i=1∑n​[yi​log(y^​i​)+(1−yi​)log(1−y^​i​)]

import numpy as np def sigmoid(x): return 1/(1+np.exp(-x)) if name == "main":
# -------------毛发长,腿长 dogs = np.array([[8.9,12],[9,11],[10,13],[9.9,11.2],[12.2,
10.1],[9.8,13],[8.8,11.2]],dtype = np.float32) # 0 cats = np.array([[3,4],[5,6],
[3.5,5.5],[4.5,5.1],[3.4,4.1],[4.1,5.2],[4.4,4.4]],dtype = np.float32) # 1
labels= np.array([0]*7 + [1]* 7,dtype = np.int32).reshape(-1,1) X = np.vstack((
dogs,cats)) k = np.random.normal(0,1,size=(2,1)) b = 0 epoch = 1000 lr = 0.05
for e in range(epoch): p = X @ k + b pre = sigmoid(p) loss = -np.sum(labels * np
.log(pre) + (1-labels) * np.log(1-pre)) G = pre - labels delta_k = X.T @ G
delta_b= np.sum(G) k = k - lr * delta_k b = b - lr * delta_b print(loss) while
True: f1 = float(input('请输入毛发长:')) f2 = float(input("请输入腿长:")) test_x = np.array
([f1,f2]).reshape(1,2) p = sigmoid(test_x @ k + b ) if p >0.5: print("类别: 猫")
else: print("类别: 狗")

GitHub

Gitee