记5(一元逻辑回归+线性分类器+多元逻辑回归

server/2025/2/3 19:44:44/

目录

  • 1、一元逻辑回归
  • 2、线性可分&线性不可分
  • 3、Iris数据集实现多元逻辑回归
  • 4、绘制分类图
  • 5、鸢尾花分类图
  • 6、多分类问题:(softmax回归)
    • 6.1、编码:自然顺序码、独热编码、独冷编码
    • 6.2、二/多分类问题:
    • 6.3、softmax回归:
    • 6.4、(非)互斥的多分类问题:

1、一元逻辑回归

import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
#x是商品房面积,y是对应的商品房类型
x=np.array([137.97,104.50,100.00,126.32,79.20,99.00,124.00,114.00,106.69,140.05,53.75,46.91,68.00,63.02,81.26,86.21])
y=np.array([1,1,0,1,0,1,1,0,0,1,0,0,0,0,0,0])
#plt.scatter(x,y)  #可以看到横坐标面积都是大于40的正数
x_train=x-np.mean(x)        #因为sigmoid函数是以0点为中心的,所以需要中心化
y_train=y
#plt.scatter(x,y)learn_rate=0.005        #超参数
iter=5
display_step=1          #显示间隔np.random.seed(612)
w=tf.Variable(np.random.randn())
b=tf.Variable(np.random.randn())
cross_train=[]           #存放训练集的交叉熵损失
acc_train=[]             #存放训练集的分类准确率plt.scatter(x_train,y_train)
x_=range(-80,80)
y_=1/(1+tf.exp(-(w*x_+b)))
plt.plot(x_,y_,color="red",linewidth=3) #绘制使用初始的w、b值时的sigmoid函数曲线,此时w<0,b<0for i in range(0,iter+1):with tf.GradientTape() as tape:#Sigmoid函数pred_train=1/(1+tf.exp(-(w*x_train+b)))#交叉熵损失函数Loss_train=-tf.reduce_mean(y_train*tf.math.log(pred_train)+(1-y_train)*tf.math.log(1-pred_train))#准确率Accuracy_train=tf.reduce_mean(tf.cast(tf.equal(tf.where(pred_train<0.5,0,1),y_train),tf.float32))cross_train.append(Loss_train)acc_train.append(Accuracy_train)dL_dw,dL_db=tape.gradient(Loss_train, [w,b])w.assign_sub(learn_rate*dL_dw)b.assign_sub(learn_rate*dL_db)if i%display_step==0:print("i:%i,Train Loss:%f, Accuracy:%f" %(i,Loss_train,Accuracy_train))y_=1/(1+tf.exp(-(w*x_+b)))plt.plot(x_,y_)     #绘制每次间隔后,当前权值的sigmoid曲线

在这里插入图片描述
训练测试数据(不是测试集,因为是数据无标签)及其可视化:

x_test=[128.15,45.00,141.43,106.27,99.00,53.84,85.36,70.00,162.00,114.60]
pred_test=1/(1+tf.exp(-(w*(x_test-np.mean(x))+b)))
y_test=tf.where(pred_test<0.5,0,1)
for i in range(len(x_test)):print(x_test[i],"\t",pred_test[i].numpy(),"\t",y_test[i].numpy(),"\t")
#测试集数据的可视化
plt.scatter(x_test,y_test)
y_=1/(1+tf.exp(-(w*x_+b)))
plt.plot(x_+np.mean(x),y_)
plt.show()
#输出:
128.15 	 0.8610252 	 1 	
45.0 	 0.0029561974 	 0 	
141.43 	 0.9545566 	 1 	
106.27 	 0.45318928 	 0 	
99.0 	 0.2981362 	 0 	
53.84 	 0.00663888 	 0 	
85.36 	 0.108105935 	 0 	
70.0 	 0.028681064 	 0 	
162.0 	 0.9928677 	 1 	
114.6 	 0.6406205 	 1 

在这里插入图片描述

2、线性可分&线性不可分

  • 线性可分:通过一条直线分开
    与或非都是线性可分,异或线性不可分:
    在这里插入图片描述
  • 线性分类器
    在这里插入图片描述
  • 线性不可分:无法通过一条直线分开(但可以是2条直线或1条曲线)

3、Iris数据集实现多元逻辑回归

属性选取花萼长度、宽度;标签选山鸢尾(Setosa)、变色鸢尾(Virginica),

线性分类器:

import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl
import pandas as pd
plt.rcParams["font.family"] = "SimHei"
plt.rcParams['axes.unicode_minus']=False    #用来正常显示负号
#读取文件,详见Python笔记10
train_path=tf.keras.utils.get_file("iris.csv", origin=None)   #获取文件的绝对路径
df_iris=pd.read_csv(train_path,header=0)        #结果是panda的二维数据表
iris=np.array(df_iris)        #将二维数据表类型转化为二维数组类型,shape=(150,6),与视频中不一样,索引号为0的是序号
x=iris[:,1:3]         #索引号1、2列属性:花瓣长度和宽度,x.shape=(150, 2)
y=iris[:,5]          #train_y.shape=(150,)x_sv=np.concatenate((np.stack(x[y=='setosa']),  #选取2种花,共100条数据,以及其前2种属性np.stack(x[y=='versicolor'])),axis=0)
y_sv=np.concatenate((np.zeros(np.where(y=='setosa')[0].size),   #元组只有一个元素(数组)np.ones(np.where(y=='versicolor')[0].size)),axis=0)np.random.seed(612)
iris_rand=np.concatenate((x_sv,np.expand_dims(y_sv,axis=1)),axis=1)
np.random.shuffle(iris_rand)        #打乱数组,并选前面78条数据为训练集,后面22条做测试集
x_train=iris_rand[:78,0:2]
y_train=iris_rand[:78,2]
x_test=iris_rand[78:,0:2]
y_test=iris_rand[78:,2]#训练前的可视化
cm_pt=mpl.colors.ListedColormap(["blue","red"])     #自定义颜色集合(蓝色,红色)
#plt.scatter(x_train[:,0],x_train[:,1],c=y_train,cmap=cm_pt) 
#plt.show()  #横坐标花萼长度,纵坐标花萼宽度,蓝色'setosa',红色'versicolor',尺度相同不用归一化x_train=x_train-np.mean(x_train,axis=0)     #中心化
x_test=x_test-np.mean(x_test,axis=0)        #中心化
# plt.scatter(x_train[:,0],x_train[:,1],c=y_train,cmap=cm_pt) 
# plt.show()x0_train=np.ones(len(x_train)).reshape(-1,1)    #见机器学习笔记4——多元线性回归,一样的
X_train=tf.cast(tf.concat([x0_train,x_train],axis=1),tf.float32)  #X.shape=TensorShape([78, 3])
Y_train=tf.constant(y_train.reshape(-1,1),tf.float32)             #Y.shape=TensorShape([78, 1])x0_test=np.ones(len(x_test)).reshape(-1,1)    #见上面训练集,一样的
X_test=tf.cast(tf.concat([x0_test,x_test],axis=1),tf.float32)  #X.shape=TensorShape([22, 3])
Y_test=tf.constant(y_test.reshape(-1,1),tf.float32)             #Y.shape=TensorShape([22, 1])learn_rate=0.2                                  #超参数——学习率
iter=120                                         #迭代次数
display_step=30                                 #设置每迭代10次输出结果,方便查看
np.random.seed(615)
W=tf.Variable(np.random.randn(3,1),dtype=tf.float32)    #W列向量,3行1列
ce_train=[]       #保存交叉熵损失
ce_test=[]
acc_train=[]      #保存准确率
acc_test=[]
#训练模型
for i in range(0,iter+1):with tf.GradientTape() as tape:#Sigmoid函数,PRED是列向量,是每个样品的预测概率PRED_train=1/(1+tf.exp(-tf.matmul(X_train,W)))PRED_test=1/(1+tf.exp(-tf.matmul(X_test,W)))#交叉熵损失函数Loss_train=-tf.reduce_mean(Y_train*tf.math.log(PRED_train)+(1-Y_train)*tf.math.log(1-PRED_train))Loss_test=-tf.reduce_mean(Y_test*tf.math.log(PRED_test)+(1-Y_test)*tf.math.log(1-PRED_test))#准确率,训练集:将预测值PRED_train二值化,并与真实值Y_train比较Accuracy_train=tf.reduce_mean(tf.cast(tf.equal(tf.where(PRED_train.numpy()<0.5,0.,1.),Y_train),tf.float32))Accuracy_test=tf.reduce_mean(tf.cast(tf.equal(tf.where(PRED_test.numpy()<0.5,0.,1.),Y_test),tf.float32))    ce_train.append(Loss_train)ce_test.append(Loss_test)acc_train.append(Accuracy_train)acc_test.append(Accuracy_test)#只使用训练集来更新参数dL_dW=tape.gradient(Loss_train, W)W.assign_sub(learn_rate*dL_dW)if i%display_step==0:print("i:%i,\tTrainAcc:%f,TrainLoss:%f\tTestAcc:%f,TestLoss:%f" %(i,Accuracy_train,Loss_train,Accuracy_test,Loss_test))#可视化,
plt.figure(figsize=(8,8))
#绘制损失和准确率的变化曲线
plt.subplot(221)
plt.plot(ce_train,color="blue",label="Train Loss")
plt.plot(acc_train,color="red",label="Train Acc")
plt.legend()
plt.subplot(223)
plt.plot(ce_test,color="blue",label="Test Loss")
plt.plot(acc_test,color="red",label="Test Acc")
plt.legend()
#绘制散点图、决策边界
plt.subplot(222)
plt.scatter(x_train[:,0],x_train[:,1],c=y_train,cmap=cm_pt)
x_=[-1.5,1.5]
y_=-(W[1]*x_+W[0])/W[2]
plt.plot(x_,y_,color='g')
plt.subplot(224)
plt.scatter(x_test[:,0],x_test[:,1],c=y_test,cmap=cm_pt)
plt.plot(x_,y_,color='g')
plt.suptitle("训练集(上)&测试集(下)")
plt.show()

输出:

i:0,	TrainAcc:0.474359,TrainLoss:0.887229	TestAcc:0.409091,TestLoss:0.854671
i:30,	TrainAcc:0.846154,TrainLoss:0.464031	TestAcc:0.818182,TestLoss:0.448182
i:60,	TrainAcc:0.961538,TrainLoss:0.317919	TestAcc:0.909091,TestLoss:0.318348
i:90,	TrainAcc:0.987179,TrainLoss:0.244545	TestAcc:0.909091,TestLoss:0.256466
i:120,	TrainAcc:1.000000,TrainLoss:0.200362	TestAcc:0.909091,TestLoss:0.220343

在这里插入图片描述

4、绘制分类图

生成网格坐标矩阵:np.meshgrid()
填充网格:plt.pcolomesh()

import numpy as np
import matplotlib.pyplot as plt
n=10
x=np.linspace(-10,10,n)    #生成等差数列
y=np.linspace(-10,10,n)
X,Y=np.meshgrid(x,y)
Z=X+Y
plt.pcolormesh(X,Y,Z,cmap="rainbow")    #X是横坐标,Y是纵坐标,Z来控制颜色,cmap颜色方案
plt.show()

在这里插入图片描述
也可以自定义颜色序列

  • 绘制轮廓线:plt.contour()、plt.contourf()
  • 自定义颜色方案:cm=mpl.colors.ListedColormap([“#FFA0A0”,“red”])
import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl
n=200
x=np.linspace(-10,10,n)         #生成等差数列
y=np.linspace(-10,10,n)
X,Y=np.meshgrid(x,y)
Z=X+Y
#Z=tf.where(Z<0,0,1)   #也可以用这个函数来设置阈值划分颜色
plt.figure(figsize=(6,6))
#2种颜色
plt.subplot(221)
cm_bg1=mpl.colors.ListedColormap(["#FFA0A0","#A0FFA2"])#注意有中括号[]
plt.pcolormesh(X,Y,Z,cmap=cm_bg1)    #用Z来控制颜色
#3种颜色
plt.subplot(222)
cm_bg2=mpl.colors.ListedColormap(["#FFA0A0","#A0FFA2","#AA5555"])
plt.pcolormesh(X,Y,Z,cmap=cm_bg2)    #用Z来控制颜色,均分为3种颜色
#绘制轮廓图
plt.subplot(223)
Z=X**2+Y**2
plt.contour(X,Y,Z,cmap="rainbow")    #用Z来控制颜色,每一条线上的取值相同,理解为等高线
#绘制轮廓并填充
plt.subplot(224)
plt.contourf(X,Y,Z,cmap="rainbow")
plt.show()

在这里插入图片描述

5、鸢尾花分类图

x_train的数值见上面“Iris数据集实现多元逻辑回归”代码

#绘制分类图
M=300
x1_min,x2_min=x_train.min(axis=0)   #算出训练集中花萼长度x1、花萼宽度x2的最大值最小值
x1_max,x2_max=x_train.max(axis=0)
m1,m2=np.meshgrid(np.linspace(x1_min,x1_max,M),     #绘制网格,x1横坐标,x2纵坐标np.linspace(x2_min,x2_max,M))     #linspace创建等差数列M是元素个数
X_mesh=tf.cast(np.stack((np.ones(M*M),m1.reshape(-1),m2.reshape(-1)),axis=1),dtype=tf.float32)   #生成多元线性回归需要的矩阵,shape=(90000, 3),并转化为浮点数类型
Y_mesh=tf.cast(1/(1+tf.exp(-tf.matmul(X_mesh,W))),dtype=tf.float32)
Y_mesh=tf.where(Y_mesh<0.5,0,1)     #0,1作为背景颜色填充的依据
n=tf.reshape(Y_mesh,m1.shape)       #维度变换,使Y_mesh与m1有相同形状
cm_pt=mpl.colors.ListedColormap(["blue","red"])     #散点图颜色方案
cm_bg=mpl.colors.ListedColormap(["#FFA022","#A0F111"])#背景颜色方案
plt.pcolormesh(m1,m2,n,cmap=cm_bg)
plt.scatter(x_train[:,0],x_train[:,1],c=y_train,cmap=cm_pt)

在这里插入图片描述

6、多分类问题:(softmax回归)

6.1、编码:自然顺序码、独热编码、独冷编码

  • 自然顺序码转化为独热编码:tf.one_hot(indices,depth) indices是一个整数,depth是深度
  • 依次是用一个数字、一个向量(独热是1,独冷是0)表示一个类别
    在这里插入图片描述

6.2、二/多分类问题:

  • 二分类问题:输入的x1、x2是样品的2个特征,令x0=1,因此,可以看成是输入3个特征,w0表示偏置项,通过sigmoid函数转化为一个0~1之间的概率值,逻辑回归
  • 多分类问题:样品的标记常常表示为独热编码的形式,如下图(0 0 1)^T,其中红色框里面的向量是对应标记的概率,softmax回归
    在这里插入图片描述
  • 交叉熵损失函数:
    二分类问题:二元交叉熵损失函数(BCE);多分类问题:多元交叉熵损失函数(CCE):
    在这里插入图片描述
    如下模型中A、B的准确率一样,但是A中那个预测错的比较离谱,计算知道A交叉熵损失较大:

在这里插入图片描述

6.3、softmax回归:

  • tf.nn.softmax()
  • 如下图,使得输入序列中较大的数在输出中的概率更大。这也是广义线性回归的一种,用来完成分类任务
    在这里插入图片描述
  • 例如鸢尾花数据集有4个属性,加上偏置项x0,输出有3个标签,so,模型参数矩阵W是5行3列
    在这里插入图片描述

6.4、(非)互斥的多分类问题:

  • 互斥的多分类问题:手写数字识别、鸢尾花识别、…
  • 非互斥的多分类问题:包含人物的图片与包含汽车的图片识别、…

http://www.ppmy.cn/server/164691.html

相关文章

基于微信小程序的电子竞技信息交流平台设计与实现(LW+源码+讲解)

专注于大学生项目实战开发,讲解,毕业答疑辅导&#xff0c;欢迎高校老师/同行前辈交流合作✌。 技术范围&#xff1a;SpringBoot、Vue、SSM、HLMT、小程序、Jsp、PHP、Nodejs、Python、爬虫、数据可视化、安卓app、大数据、物联网、机器学习等设计与开发。 主要内容&#xff1a;…

如何本地部署DeepSeek

第一步&#xff1a;安装ollama https://ollama.com/download 打开官网&#xff0c;选择对应版本 第二步&#xff1a;选择合适的模型 https://ollama.com/ 模型名称中的 1.5B、7B、8B 等数字代表模型的参数量&#xff08;Parameters&#xff09;&#xff0c;其中 B 是英文 B…

lstm代码解析1.1

这段代码使用了 MinMaxScaler&#xff0c;它是 scikit-learn 库中的一种数据预处理工具&#xff0c;用于对数据进行归一化处理。归一化是一种常见的数据预处理方法&#xff0c;目的是将数据缩放到指定的范围&#xff08;通常是 [0, 1]&#xff09;&#xff0c;以便不同特征的数…

爱快 IK-X9 吸顶AP 简单开箱评测和拆解,三频WiFi7,BE5000,2.5G网口

爱快&#xff08;iKuai&#xff09; IK-X9 三频高密度吸顶AP 简单开箱评测和拆解&#xff0c;三频Wi-Fi7&#xff08;2.4GHz&#xff1a;688Mbps、5.1GHz&#xff1a;2882Mbps、5.8GHz&#xff1a;1441Mbps&#xff09;&#xff0c;BE5000&#xff0c;2.5G网口 用的爱快系统做…

CSS Display属性完全指南

CSS Display属性完全指南 引言核心概念常用display值详解1. block&#xff08;块级元素&#xff09;2. inline&#xff08;行内元素&#xff09;3. inline-block&#xff08;行内块级元素&#xff09;4. flex&#xff08;弹性布局&#xff09;5. grid&#xff08;网格布局&…

构建一个数据分析Agent:提升分析效率的实践

在上一篇文章中,我们讨论了如何构建一个智能客服Agent。今天,我想分享另一个实际项目:如何构建一个数据分析Agent。这个项目源于我们一个金融客户的真实需求 - 提升数据分析效率,加快决策速度。 从分析师的痛点说起 记得和分析师团队交流时的场景&#xff1a; 小张&#xff…

本地Deepseek添加个人知识库

本贴在谷歌插件Page Assist里部署了Deepseek的基础上进行的&#xff0c;部署方法见上一篇帖子win10部署本地deepseek-r1&#xff0c;chatbox&#xff0c;deepseek联网&#xff08;谷歌网页插件&#xff09;。 想要获得自己的私人助手&#xff0c;需要喂相关的知识&#xff08;…

性能测试丨分布式性能监控系统 SkyWalking

软件测试领域&#xff0c;分布式系统的复杂性不断增加&#xff0c;如何保证应用程序的高可用性与高性能&#xff0c;这是每一个软件测试工程师所面临的重大挑战。幸运的是&#xff0c;现在有了一些强大的工具来帮助我们应对这些挑战&#xff0c;其中之一便是Apache SkyWalking。…