将特定类别出现的概率与该类别出现时每一个特征值出现的概率取乘积,以此表示该组特征值被归属为该类别的概率。以此计算该组特征值被归属为每一个类别的概率,择其最大的概率所对应的类别作为预测结果。
x1 x2 -> y
3 1 0
2 5 1
1 8 1
6 4 0
5 2 0
3 5 1
4 7 1
4 1 0
3 9 ? -> 1
$x_1 > x_2: y = 0$
$x_1 < x_2: y = 1$
import numpy as np
import matplotlib.pyplot as mp
x = np.array([
[3, 1],
[2, 5],
[1, 8],
[6, 4],
[5, 2],
[3, 5],
[4, 7],
[4,-1]])
y = np.array([0, 1, 1, 0, 0, 1, 1, 0])
l, r, h = x[:, 0].min() - 1, x[:, 0].max() + 1, 0.005
b, t, v = x[:, 1].min() - 1, x[:, 1].max() + 1, 0.005
grid_x = np.meshgrid(np.arange(l, r, h),
np.arange(b, t, v))
flat_x = np.c_[grid_x[0].ravel(), grid_x[1].ravel()]
flat_y = np.zeros(len(flat_x), dtype=int)
flat_y[flat_x[:, 0] < flat_x[:, 1]] = 1 # 分类规则
grid_y = flat_y.reshape(grid_x[0].shape)
mp.figure('Simple Classification',
facecolor='lightgray')
mp.title('Simple Classification', fontsize=20)
mp.xlabel('x', fontsize=14)
mp.ylabel('y', fontsize=14)
mp.tick_params(labelsize=10)
mp.pcolormesh(grid_x[0], grid_x[1], grid_y, cmap='gray')
mp.scatter(x[:, 0], x[:, 1], c=y, cmap='brg', s=80)
mp.show()
$y = w0 + w1x$
$z = \frac{1}{1 + e^{-y}}$
x样本被归属为1类别的概率
梯度下降
model=LogisticRegression(solver='liblinear', C=正则强度)
$w0 + w1 \times 1 + w2 \times 2 = 0$
import numpy as np
import sklearn.linear_model as lm
import matplotlib.pyplot as mp
x = np.array([
[3, 1],
[2, 5],