实验9 分类问题

news/2025/2/22 16:06:54/

1. 实验目的

①掌握逻辑回归的基本原理,实现分类器,完成多分类任务;
②掌握逻辑回归中的平方损失函数、交叉熵损失函数以及平均交叉熵损失函数。

2. 实验内容

①能够使用TensorFlow计算Sigmoid函数、准确率、交叉熵损失函数等,并在此基础上建立逻辑回归模型,完成分类任务;
②能够使用MatPlotlib绘制分类图。

3. 实验过程

题目一:

观察6.5.3小节中给出的鸢尾花数据集可视化结果(如图1所示),编写代码实现下述功能:(15分)
在这里插入图片描述

图1 鸢尾花数据集

要求:
⑴选择恰当的属性或属性组合,训练逻辑回归模型,区分山鸢尾和维吉尼亚鸢尾,并测试模型性能,以可视化的形式展现训练和测试的过程及结果。
⑵比较选择不同属性或属性组合时的学习率、迭代次数,以及在训练集和测试集上的交叉熵损失和准确率,以表格或合适的图表形式展示。
⑶分析和总结:
区分山鸢尾和维吉尼亚鸢尾,至少需要几种属性?说明选择某种属性或属性组合的依据;通过以上结果,可以得到什么结论,或对你有什么启发。
① 代码

import math
import randomimport tensorflow as tf
import pandas as pd
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt#下载鸢尾花训练数据集和测试数据集
TRAIN_URL = "http://download.tensorflow.org/data/iris_training.csv"
train_path = tf.keras.utils.get_file(TRAIN_URL.split('/')[-1],TRAIN_URL)TEST_URL = "http://download.tensorflow.org/data/iris_test.csv"
test_path = tf.keras.utils.get_file(TEST_URL.split("/")[-1],TEST_URL)#读取数据集并转换为numpy数组
df_iris_train = pd.read_csv(train_path, header=0)
df_iris_test = pd.read_csv(test_path,header=0)
iris_train = np.array(df_iris_train)
iris_test = np.array(df_iris_test)#提取分量,花萼的长度和宽度来训练模型
train_x = iris_train[:,(0,1)]
train_y = iris_train[:,4]
test_x = iris_test[:,(0,1)]
test_y = iris_test[:,4]#有三种鸢尾花的品种,采取以下方法提取出山鸢尾和维吉尼亚鸢尾
x_train = train_x[train_y != 1]
y_train = train_y[train_y != 1]
x_test = test_x[test_y != 1]
y_test = test_y[test_y != 1]#将维吉尼亚鸢尾的值赋为1
y_train[y_train == 2] = 1
y_test[y_test == 2] = 1num_train = len(x_train)
num_test = len(x_test)plt.figure(figsize=(10,3))
cm_pt = mpl.colors.ListedColormap(["blue","green"])plt.subplot(121)
plt.scatter(x_train[:,0],x_train[:,1],c = y_train,cmap=cm_pt)plt.subplot(122)
plt.scatter(x_test[:,0],x_test[:,1],c = y_test,cmap=cm_pt)
plt.show()#按列中心化
x_train = x_train - np.mean(x_train,axis=0)
x_test = x_test - np.mean(x_test,axis=0)"""
plt.figure(figsize=(10,3))
cm_pt = mpl.colors.ListedColormap(["blue","green"])plt.subplot(121)
plt.scatter(x_train[:,0],x_train[:,1],c = y_train,cmap=cm_pt)plt.subplot(122)
plt.scatter(x_test[:,0],x_test[:,1],c = y_test,cmap=cm_pt)
plt.show()
"""x0_train = np.ones(num_train).reshape(-1,1)
X_train = tf.cast(tf.concat((x0_train,x_train),axis=1),tf.float32)
Y_train = tf.cast(y_train.reshape(-1,1),tf.float32)x0_test = np.ones(num_test).reshape(-1,1)
X_test = tf.cast(tf.concat((x0_test,x_test),axis=1),tf.float32)
Y_test = tf.cast(y_test.reshape(-1,1),tf.float32)#设置超参数
learn_rate = 0.2
iter = 200
display_step = 50np,random.seed(612)
W = tf.Variable(np.random.randn(3,1),dtype=tf.float32)ce_train = []
ce_test = []
acc_train = []
acc_test = []for i in range(0,iter + 1):with tf.GradientTape() as tape:PRED_train = 1 / (1 + tf.exp(-tf.matmul(X_train,W)))Loss_train = -tf.reduce_mean(Y_train * tf.math.log(PRED_train) + (1 - Y_train) * tf.math.log(1 - PRED_train))PRED_test = 1 / (1 + tf.exp(- tf.matmul(X_test,W)))Loss_test = -tf.reduce_mean(Y_test * tf.math.log(PRED_test) + (1 - Y_test) * tf.math.log(1 - PRED_test))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)acc_train.append(accuracy_train)ce_test.append(Loss_test)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:%d,TrainAcc:%f,TrainLoss:%f,TestAcc:%f,TestLoss:%f"%(i,accuracy_train,Loss_train,accuracy_test,Loss_test))plt.figure(figsize=(10,3))plt.subplot(121)
plt.plot(ce_train,color="b",label = "train")
plt.plot(ce_test,color="r",label = "test")
plt.ylabel("Loss")
plt.legend()plt.subplot(122)
plt.plot(acc_train,color="b",label = "train")
plt.plot(acc_test,color="r",label = "test")
plt.ylabel("Accuarcy")
plt.legend()
plt.show()

② 结果记录
在这里插入图片描述
在这里插入图片描述

③ 实验总结

在这里插入图片描述

题目二:

在Iris数据集中,分别选择2种、3种和4种属性,编写程序,区分三种鸢尾花。记录和分析实验结果,并给出总结。(20分)
⑴确定属性选择方案。
⑵编写代码建立、训练并测试模型。
⑶参考11.6小节例程,对分类结果进行可视化。
⑷分析结果:
比较选择不同属性组合时的学习率、迭代次数、以及在训练集和测试集上的交叉熵损失和准确率,以表格或合适的图表形式展示。
(3)总结:
通过以上分析和实验结果,对你有什么启发。

① 代码

import tensorflow as tf
import numpy as np
import pandas as pd
import matplotlib as mpl
import matplotlib.pyplot as pltTRAIN_URL = "http://download.tensorflow.org/data/iris_training.csv"
train_path = tf.keras.utils.get_file(TRAIN_URL.split("/")[-1], TRAIN_URL)df_iris_train = pd.read_csv(train_path,header=0)
iris_train = np.array(df_iris_train)
x_train = iris_train[:,2:4]
y_train = iris_train[:,4]
num_train = len(x_train)x0_train = np.ones(num_train).reshape(-1,1)
X_train = tf.cast(tf.concat([x0_train,x_train],axis=1),tf.float32)
Y_train = tf.one_hot(tf.constant(y_train,dtype=tf.int32),3)learn_rate = 0.2
iter = 700
display = 100np.random.seed(612)
W = tf.Variable(np.random.randn(3,3),dtype=tf.float32)acc = []
cce = []for i in range(iter + 1):with tf.GradientTape() as tape:PRED_train = tf.nn.softmax(tf.matmul(X_train,W))Loss_train = -tf.reduce_sum(Y_train * tf.math.log(PRED_train)) / num_trainaccuracy_train = tf.reduce_mean(tf.cast(tf.equal(tf.argmax(PRED_train.numpy(),axis=1),y_train),tf.float32))acc.append(accuracy_train)cce.append(Loss_train)dL_dW = tape.gradient(Loss_train,W)W.assign_sub(learn_rate * dL_dW)if i % display == 0:print("i:%d,Acc:%f,Loss:%f"%(i,accuracy_train,Loss_train))M = 500
x1_min,x2_min = x_train.min(axis = 0)
x1_max,x2_max = x_train.max(axis = 0)
t1 = np.linspace(x1_min,x1_max,M)
t2 = np.linspace(x2_min,x2_max,M)
m1,m2 = np.meshgrid(t1,t2)m0 = np.ones(M * M)
X_ = tf.cast(np.stack((m0,m1.reshape(-1),m2.reshape(-1)),axis=1),tf.float32)
Y_ = tf.nn.softmax(tf.matmul(X_,W))Y_ = tf.argmax(Y_.numpy(),axis=1)
n = tf.reshape(Y_,m1.shape)plt.figure(figsize=(8,6))
cm_bg = mpl.colors.ListedColormap(['#A0FFA0','#FFA0A0','#A0A0FF'])plt.pcolormesh(m1,m2,n,cmap=cm_bg)
plt.scatter(x_train[:,0],x_train[:,1],c = y_train,cmap="brg")plt.show()

② 结果记录
在这里插入图片描述

③ 实验总结
在这里插入图片描述

4. 实验小结&讨论题

①实现分类问题的一般步骤是什么?实现二分类和多分类问题时有什么不同之处?哪些因素会对分类结果产生影响?
答:1.问题的提出2.神经网络模型的搭建和训3.结果展示。
多分类:
每个样本只能有一个标签,比如ImageNet图像分类任务,或者MNIST手写数字识别数据集,每张图片只能有一个固定的标签。
对单个样本,假设真实分布为,网络输出分布为,总的类别数为,则在这种情况下,交叉熵损失函数的计算方法如下所示,我们可以看出,实际上也就是计算了标签类别为1的交叉熵的值,使得对应的信息量越来越小,相应的概率也就越来越大了。
二分类:
对于二分类,既可以选择多分类的方式,也可以选择多标签分类的方式进行计算,结果差别也不会太大
②将数据集划分为训练集和测试集时,应该注意哪些问题?改变训练集和测试集所占比例,对分类结果会有什么影响?
答:同样的迭代次数,和学习率下,随着训练集的比例逐渐变大,训练集交叉熵损失大致变小准确率变高的趋势,测试集交叉熵损失大致变大准确率变高的趋势。

③当数据集中存在缺失值时,有哪些处理的方法?查阅资料并结合自己的思考,说明不同处理方法的特点和对分类结果的影响。
答:
(1)删除,直接去除含有缺失值的记录,适用于数据量较大(记录较多)且缺失比较较小的情形,去掉后对总体影响不大。
(2)常量填充,变量的含义、获取方式、计算逻辑,以便知道该变量为什么会出现缺失值、缺失值代表什么含义。
(3)插值填充,采用某种插入模式进行填充,比如取缺失值前后值的均值进行填充。
(4)KNN填充
(5)随机森林填充,随机森林算法填充的思想和knn填充是类似的,即利用已有数据拟合模型,对缺失变量进行预测。


http://www.ppmy.cn/news/69441.html

相关文章

智能安防系统-视频监控系统

一、智能安防系统 1、智能安防系统介绍 安全防范系统成为了智慧城市与物联网行业应用中的一个非常重要的子系统。 安防系统主要包括&#xff1a;视频监控系统、入侵报警系统、出入口控制系统、电子巡查系统以及智能停车场管理系统等5个子系统。 AI人工智能安防系统功能&#xf…

CNN文本分类实现文本分类案例解析(附实例源码)

前言 实现步骤1.导入所需的库和模块&#xff1a;2.设置随机种子&#xff1a;3.定义模型超参数&#xff1a;4.加载数据集&#xff1a;5.对文本进行填充和截断&#xff1a;6.构建模型&#xff1a;7.编译模型&#xff1a;8.训练模型&#xff1a;9.评估模型&#xff1a; 完整代码 C…

低代码信创开发核心技术(一):基于Vue.js的描述依赖渲染DDR实现模型驱动的组件

前言 随着数字化转型的不断发展&#xff0c;低代码开发平台已成为企业快速建立自己的应用程序的首选方案。然而&#xff0c;实现这样一个平台需要具备高效、灵活和可定制化的能力。这正是基于描述依赖渲染&#xff08;Description dependency rendering&#xff09;所实现的。…

计算image1和image2之间的LPIPS指标的python代码

计算image1和image2之间的LPIPS指标的python代码 import cv2 import lpips import torchloss_fn_vgg lpips.LPIPS(netalex).to("cuda:0")image1_path F:\YXL\project\Restormer-mainV364_v1\YXL_dir\photo\photo_otput_result.png image2_path F:\YXL\project\Re…

3.编写油猴脚本之-helloword

3.编写油猴脚本之-helloword Start 通过上一篇文章的学习&#xff0c;我们安装完毕了油猴插件。今天我们来编写一个helloword的脚步&#xff0c;体验一下油猴。 1. 开始 点击油猴插件>添加新脚本 默认生成的脚本 // UserScript // name New Userscript // name…

Java构造方法

Java构造方法 Java构造方法是啥&#xff0c;有什么作用构造方法如何定义&#xff1f;构造方法该如何调用&#xff1f;案例(利用构造方法完成一个时间打印)构造方法必须与类名相同构造方法可以重载吗&#xff1f;啥是缺省构造器&#xff1f; Java构造方法是啥&#xff0c;有什么…

美团数据指标体系搭建实战

在美团商家版中&#xff0c;美团为商家搭建的数据指标体系&#xff0c;很好的指导了商家的经营发展方向以及提供经营状况概览。​ 本文通过体验美团商家版经营数据子功能&#xff0c;对美团商家版数据指标体系搭建的情况做出一个概述。 美团商家版的店铺子功能下&#xff0c;…

【Spark编程基础】第7章 Structured Streaming

系列文章目录 文章目录 系列文章目录前言第7章 Structured Streaming7.1 概述7.1.1 基本概念7.1.2 两种处理模型7.1.3 Structured Streaming 和 Spark SQL、Spark Streaming 关系 7.2 编写Structured Streaming程序的基本步骤7.3 输入源7.3.1 File源7.3.2 Kafka源7.3.3 Socket源…