ML17:逻辑回归多分类

# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import numpy as np
import sklearn.linear_model as lm
import matplotlib.pyplot as mp
x = np.array([
    [4,     7],
    [3.5,   8],
    [3.1, 6.2],
    [0.5,   1],
    [1,     2],
    [1.2, 1.9],
    [6,     2],
    [5.7, 1.5],
    [5.4, 2.2]])
y = np.array([0, 0, 0, 1, 1, 1, 2, 2, 2])
# 创建逻辑分类器模型
# solver='liblinear':对特征做线性组合
# C=1:惩罚力度/正则强度,越小越强
model = lm.LogisticRegression(solver='liblinear',
                              C=100)
model.fit(x, y)
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 = model.predict(flat_x)
grid_y = flat_y.reshape(grid_x[0].shape)
mp.figure(num='Logistic Classification',
          facecolor='lightgray')
mp.title('Logistic 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='brg')
mp.scatter(x[:, 0], x[:, 1], s=80, c=y, cmap='RdYlBu')
mp.show()

猜你喜欢

转载自blog.csdn.net/weixin_38246633/article/details/80595802