机器学习之八大算法③——逻辑回归(分类问题)

逻辑回归代码及注释

import numpy as np
import matplotlib.pyplot as plt

# 中文、负号
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False

# 读取数据
train_data = np.loadtxt('train_car.txt',delimiter=',')
test_data = np.loadtxt('test_car.txt',delimiter=',')

# 查看维度
print(train_data.shape)
print(test_data.shape)

# 洗牌
np.random.seed(7)                   #种子
np.random.permutation(train_data)
np.random.permutation(test_data)

# 提取数据
train_x,train_y = train_data[:,:-1],train_data[:,-1]
test_x,test_y = test_data[:,:-1],test_data[:,-1]

# 数据预处理
def preProcess(x,y):
    # 特征缩放
    x -= np.mean(x,0)
    x /= np.std(x,0,ddof=1)

    # 数据初始化
    x = np.c_[np.ones(len(x)),x]
    y = np.c_[y]
    return x,y
# 调用
train_x,train_y = preProcess(train_x,train_y)
test_x,test_y = preProcess(test_x,test_y)

# 逻辑函数
def g(z):
    return 1/(1+np.exp(-z))

# 画逻辑函数图像
plt.title('逻辑函数图像')
x = np.linspace(-10,10,300)
y = g(x)
plt.plot(x,y)
plt.plot(0,0.5,c='r',marker='*')
plt.show()

# 定义模型
def model(x,theta):
    z = np.dot(x,theta)
    return g(z)

# 定义代价函数
def costFunc(h,y,r):
    j = (-1.0/len(y))*np.sum(y*np.log(h) + (1-y) * np.log(1-h)) + r
    return j

# 定义梯度下降函数
def gradDesc(x,y,alpha=0.1,max_iter=20000,lamda=0):
    m,n = x.shape
    theta = np.zeros((n,1))
    j_history = np.zeros(max_iter)

    for i in range(max_iter):
        h = model(x,theta)

        #正则化
        theta_r = theta.copy()
        theta_r[0] = 0
        r = (lamda/(2*m))*np.dot(theta_r.T,theta_r)

        j_history[i] = costFunc(h,y,r)
        deldatheta = (1.0/m)*(np.dot(x.T,h-y) + lamda*theta_r)
        theta -= alpha*deldatheta

    return j_history,theta
j_history0,theta0 = gradDesc(train_x,train_y,lamda=0)
j_history1,theta1 = gradDesc(train_x,train_y,lamda=1.6)

print(theta0)
print(theta1)

# 代价函数对比图像
plt.title('代价函数图像')
plt.plot(j_history0,label='lamda=0')
plt.plot(j_history1,label='lamda=1.6')
plt.legend()
plt.show()

# 获取预测值
train_h0 = model(train_x,theta0)
train_h1 = model(train_x,theta1)
test_h0 = model(test_x,theta0)
test_h1 = model(test_x,theta1)

# 准确率
def score(h,y):
    count = 0
    for i in range(len(y)):
        if np.where(h[i] >= 0.5,1,0) == y[i]:
            count += 1
    return count/len(y)

print('lamda=0时训练集准确率',score(train_h0,train_y))
print('lamda=1.6时训练集准确率',score(train_h1,train_y))
print('lamda=0时测试集准确率',score(test_h0,test_y))
print('lamda=1.6时测试集准确率',score(test_h1,test_y))

# 画图
def show(x,y,h,title):
    plt.title(title)

    # 把预测值转换为  0/1
    h = [1 if i >= 0.5 else 0 for i in h]
    h = np.c_[h]

    plt.scatter(x[y[:,0]==0,1],x[y[:,0]==0,2],label='真实值负样本',s=80,c='r')
    plt.scatter(x[y[:,0]==1,1],x[y[:,0]==1,2],label='真实值正样本',s=80,c='b')

    plt.scatter(x[h[:,0] == 0, 1], x[h[:,0] == 0, 2],label='预测值负样本')
    plt.scatter(x[h[:,0] == 1, 1], x[h[:,0] == 1, 2],label='预测值正样本')

    min_x1,max_x1 = np.min(x[:,1]),np.max(x[:,1])
    min_x2,max_x2 =  -(theta0[0] + theta0[1]*min_x1)/theta0[2]\
                    ,-(theta0[0] + theta0[1]*max_x1)/theta0[2]

    min_x21, max_x21 = -(theta1[0] + theta1[1] * min_x1) / theta1[2] \
        , -(theta1[0] + theta1[1] * max_x1) / theta1[2]

    plt.plot((min_x1,max_x1),(min_x2,max_x2), label='lamda=0是决策边界')
    plt.plot((min_x1,max_x1),(min_x2,max_x21), label='lamda=1.6是决策边界')

    plt.legend()
    plt.show()

show(train_x,train_y,train_h0,'训练集')
show(test_x,test_y,test_h0,'测试集')


实现效果

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述


算法实现要点

在这里插入图片描述


八大算法所用所有库

在这里插入图片描述

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值