机器学习课堂4线性回归模型+特征缩放

server/2025/3/28 11:14:49/

一、实验2-2,线性回归模型,计算模型在训练数据集和测试数据集上的均方根误差

代码:

#  2-2线性回归模型
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
#  参数设置
iterations=3000  # 迭代次数
learning_rate=0.0001  # 学习率
m_train=3000  # 训练样本的数量
flag_plot_lines=False  # 是否画出拟合直线
plot_feature=1  # 使用哪个输入特征画拟合直线
plot_skip=4  # 每间隔几条拟合直线画出一条拟合直线
# 读入气温数据集
df = pd.read_csv('temperature_dataset.csv')
data=np.array(df)
m_all = np.shape(data)[0]  # 样本总数
d =1 if flag_plot_lines else np.shape(data)[1] - 1  # 输入特征的维数
m_test = m_all - m_train  # 测试数据集样本数量
# 划分数据集
X_train = data[0:m_train, plot_feature].reshape((1, -1)) if flag_plot_lines else data[0:m_train, 1:].T  # 根据是否画1拟合直线来决定输入特征的维度
X_test = data[m_train:, plot_feature].reshape((1, -1)) if flag_plot_lines else data[m_train:, 1:].T  # 根据是否画拟合直线来决定输入特征的维度
Y_train = data[0:m_train, 0].reshape((1, -1))
Y_test = data[m_train:, 0].reshape((1, -1))
# 初始化
w = np.zeros((d, 1)).reshape((-1, 1))  # 权重
b = 0  # 偏差(标量)
v = np.ones((1, m_train))  # 1向量
costs_saved = []
# 用来保存拟合直线的权重与偏差
w_saved=np.zeros(iterations+1)
b_saved=np.zeros(iterations+1)
# 迭代循环
for i in range(iterations):  # 更新权重与偏差Y_hat = np.dot(w.T, X_train) + b * ve = Y_hat - Y_train  # 计算误差b = b - 2. * learning_rate * np.dot(v, e.T) / m_train  # 更新偏差w = w - 2. * learning_rate * np.dot(X_train, e.T) / m_train  # 更新权重# 保存代价函数的值costs=np.dot(e, e.T)/m_traincosts_saved.append(costs.item(0))# 保存每次迭代的权重与偏差w_saved[i + 1]=w[0]b_saved[i + 1] = b
# 打印最新的权重与偏差
print('Weights=', np.array2string(np.squeeze(w, axis=1), precision=3))
print(f'Bias={b.item(0):.3f}')
# 画代价函数的值
plt.plot(range(1,np.size(costs_saved)+1),costs_saved,'r-o',linewidth=2,markersize=5)
plt.ylabel('Costs')
plt.xlabel('Iterations')
plt.title('Learning rate='+str(learning_rate))
plt.show()
# 计算训练数据集上的均方根误差
y_hat=np.dot(w.T, X_train)+b*v  # 计算训练样本标注的预测值
e=y_hat-Y_train  # 计算标注预测值与标注之间的误差
mse=np.dot(e, e.T)/m_train  # 计算均方误差
rmse=np.sqrt(mse)  # 计算均方根误差
print(f'Trainset RMSE={rmse.item(0):.3f}')  # 打印均方根误差
#  计算测试数据集上的均方根误差
y_hat_test=np.dot(w.T, X_test)+b  # 计算测试样本标注的预测值(此处使用了广播操作)
e_test=y_hat_test-Y_test  # 计算标注预测值与标注之间的误差
mse_test=np.dot(e_test,e_test.T)/m_test   # 计算均方误差
rmse_test=np.sqrt(mse_test)  # 计算均方根误差
print(f'Testset RMSE={rmse_test.item(0):.3f}')  # 打印均方根误差
# 画拟合直线
if flag_plot_lines:plot_x_min=np.min(X_train)  # 训练样本一维输入特征的最小值plot_x_max = np.max(X_train)  # 训练样本一维输入特征的最大值plot_x=np.array([plot_x_min, plot_x_max])  # 将其组成一个一维数组plt.figure()  # 新建一个图形plt.plot(X_train[0, 0::10], Y_train[0,0::10], 'xm')  # 画训练样本(每10个训练样本画出一个)for i in range(0, iterations+1, plot_skip+1):  # 每plot_skip+1条拟合直线画出一条plot_y=w_saved[i]*plot_x+b_saved[i]  # 计算输入特征最大值与最小值对应的标注预测值plt.plot(plot_x, plot_y, '--')  # 用虚线画出这条拟合直线plot_y=w_saved[i]*plot_x+b_saved[i]  # 计算输入特征最大值与最小值对应的标注预测值(最后一条拟合直线)plt.plot(plot_x, plot_y, 'b', linewith=3)  # 用实线画出最后一条拟合直线plt.xlabel('x')plt.ylabel('y')plt.show()

运行结果

二、实验2-5对输入特征做特征缩放(标准化、最小最大归一化、均值归一化)+训练评估线性回归模型

代码

#  2-5特征缩放
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
#  参数设置
iterations=3000  # 迭代次数
learning_rate=0.1  # 学习率
m_train=3000  # 训练样本的数量
flag_fs='std'  # 特征缩放法:’std‘为标准化、’norm_minmax‘为最小最大化归一化、’norm_mean‘为均值归一化,’none‘为不使用特征缩放
# 读入气温数据集
df = pd.read_csv('temperature_dataset.csv')
data=np.array(df)
m_all = np.shape(data)[0]  # 样本总数
d =np.shape(data)[1] - 1  # 输入特征的维数
m_test = m_all - m_train  # 测试数据集样本数量
# 特征缩放
if flag_fs=='std':  # 如果进行标准化mean=np.mean(data[0:m_train, 1:], axis=0)  # 计算训练样本输入特征的均值std=np.std(data[0:m_train, 1:], axis=0, ddof=1)  # 计算训练样本输入特诊的标准差data[:,1:]=(data[:,1:]-mean)/std  # 标准化所有样本的输入特征
elif flag_fs=='norm_minmax':  # 最小最大归一化xmin=np.amin(data[0:m_train, 1:], axis=0)  # 返回训练样本输入特征最小值xmax = np.amax(data[0:m_train, 1:], axis=0)  # 返回训练样本输入特征最大值data[:, 1:]=(data[:, 1:]-xmin)/(xmax-xmin)  # 最小最大归一化所有样本的输入特征
elif flag_fs=='norm_mean':xmin=np.amin(data[0:m_train, 1:], axis=0)  # 返回训练样本输入特征的最小值xmax=np.amax(data[0:m_train, 1:], axis=0)  # 返回训练样本输入特征的最大值mean=np.mean(data[0:m_train, 1:], axis=0)  # 计算训练样本输入特征的均值data[:, 1:]=(data[:, 1:]-mean)/(xmax-xmin)  # 均值归一所有样本的输入特征# 划分数据集
X_train = data[0:m_train, 1:].T  # 训练集输入特征
X_test = data[m_train:, 1:].T  # 测试集输入特征
Y_train = data[0:m_train, 0].reshape((1, -1))  # 训练集目标值
Y_test = data[m_train:, 0].reshape((1, -1))  # 测试集目标值# 初始化
w = np.zeros((d, 1))  # 权重
b = 0  # 偏差(标量)
v = np.ones((1, m_train))  # 1向量
costs_saved = []  # 用于保存代价函数的值
# 用来保存拟合直线的权重与偏差
w_saved = np.zeros(iterations + 1)
b_saved = np.zeros(iterations + 1)# 迭代循环
for i in range(iterations):  # 更新权重与偏差Y_hat = np.dot(w.T, X_train) + b * ve = Y_hat - Y_train  # 计算误差b = b - 2. * learning_rate * np.dot(v, e.T) / m_train  # 更新偏差w = w - 2. * learning_rate * np.dot(X_train, e.T) / m_train  # 更新权重# 保存代价函数的值costs = np.dot(e, e.T) / m_traincosts_saved.append(costs.item(0))# 保存每次迭代的权重与偏差w_saved[i + 1] = w[0]b_saved[i + 1] = b# 打印最新的权重与偏差
print('Weights=', np.array2string(np.squeeze(w, axis=1), precision=3))
print(f'Bias={b.item(0):.3f}')# 计算训练数据集上的均方根误差
y_hat = np.dot(w.T, X_train) + b * v  # 计算训练样本标注的预测值
e = y_hat - Y_train  # 计算标注预测值与标注之间的误差
mse = np.dot(e, e.T) / m_train  # 计算均方误差
rmse = np.sqrt(mse)  # 计算均方根误差
print(f'Trainset RMSE={rmse.item(0):.3f}')  # 打印均方根误差# 计算测试数据集上的均方根误差
y_hat_test = np.dot(w.T, X_test) + b  # 计算测试样本标注的预测值(此处使用了广播操作)
e_test = y_hat_test - Y_test  # 计算标注预测值与标注之间的误差
mse_test = np.dot(e_test, e_test.T) / m_test  # 计算均方误差
rmse_test = np.sqrt(mse_test)  # 计算均方根误差
print(f'Testset RMSE={rmse_test.item(0):.3f}')  # 打印均方根误差# 画拟合直线
plot_feature = 0  # 假设使用第一个特征画拟合直线
plot_skip = 4  # 每间隔几条拟合直线画出一条拟合直线# 如果进行了特征缩放,则需要将特征值还原到原始范围
if flag_fs == 'std':X_train_plot = X_train[plot_feature, :] * std[plot_feature] + mean[plot_feature]X_test_plot = X_test[plot_feature, :] * std[plot_feature] + mean[plot_feature]
elif flag_fs == 'norm_minmax':X_train_plot = X_train[plot_feature, :] * (xmax[plot_feature] - xmin[plot_feature]) + xmin[plot_feature]X_test_plot = X_test[plot_feature, :] * (xmax[plot_feature] - xmin[plot_feature]) + xmin[plot_feature]
elif flag_fs == 'norm_mean':X_train_plot = X_train[plot_feature, :] * (xmax[plot_feature] - xmin[plot_feature]) + mean[plot_feature]X_test_plot = X_test[plot_feature, :] * (xmax[plot_feature] - xmin[plot_feature]) + mean[plot_feature]
else:X_train_plot = X_train[plot_feature, :]X_test_plot = X_test[plot_feature, :]plot_x_min = np.min(X_train_plot)  # 训练样本一维输入特征的最小值
plot_x_max = np.max(X_train_plot)  # 训练样本一维输入特征的最大值
plot_x = np.array([plot_x_min, plot_x_max])  # 将其组成一个一维数组
plt.figure()  # 新建一个图形
plt.plot(X_train_plot[0::10], Y_train[0, 0::10], 'xm')  # 画训练样本(每10个训练样本画出一个)
for i in range(0, iterations + 1, plot_skip + 1):  # 每plot_skip+1条拟合直线画出一条plot_y = w_saved[i] * plot_x + b_saved[i]  # 计算输入特征最大值与最小值对应的标注预测值plt.plot(plot_x, plot_y, '--')  # 用虚线画出这条拟合直线
plot_y = w_saved[i] * plot_x + b_saved[i]  # 计算输入特征最大值与最小值对应的标注预测值(最后一条拟合直线)
plt.plot(plot_x, plot_y, 'b', linewidth=3)  # 用实线画出最后一条拟合直线
plt.xlabel('x')
plt.ylabel('y')
plt.title('Fitting Line with Feature Scaling=' + flag_fs)
plt.show()

运行结果


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

相关文章

C语言每日一练——day_12(最后一天)

引言 针对初学者,每日练习几个题,快速上手C语言。第十二天。(最后一天,完结散花啦) 采用在线OJ的形式 什么是在线OJ? 在线判题系统(英语:Online Judge,缩写OJ&#xff0…

Web开发-JS应用原生代码前端数据加密CryptoJS库jsencrypt库代码混淆

知识点: 1、安全开发-原生JS-数据加密&代码混淆 2、安全开发-原生JS-数据解密安全案例 一、演示案例-WEB开发-原生JS&第三方库-数据加密 前端技术JS实现: 1、非加密数据大致流程: 客户端发送->明文数据传输-服务端接受数据->…

2025.3.20总结

阅读:《时间贫穷》第二章,里面讲到,运动,多行善事,体验自然,都会增强自我效能感,是对抗时间焦虑的强有力的方式。 花时间运动是值得的,公司每周三都是运动周,把运动视作…

51单片机和STM32 入门分析

51单片机和STM32是嵌入式开发中两种主流的微控制器,它们在架构、性能、应用场景等方面存在显著差异。以下是两者的对比分析及选择建议: 1. 51单片机与STM32的定义与特点 51单片机 定义:基于Intel 8051内核的8位微控制器,结构简单…

代码随想录Day20

Day20 回溯算法part02 LeetCode 39. 组合总和 题目描述 给你一个 无重复元素 的整数数组 candidates 和一个目标整数 target ,找出 candidates 中可以使数字和为目标数 target 的 所有 不同组合 ,并以列表形式返回。你可以按 任意顺序 返回这些组合。…

Docker --shm-size参数含义

文章目录 解释 解释 --shm-size 是一个用于 Docker 的参数,用来设置容器的共享内存大小。 在 Docker 中,/dev/shm 是一个临时文件系统(tmpfs),用于存储共享内存。默认情况下,Docker 容器的共享内存大小是…

NO.51十六届蓝桥杯备战|堆算法题|第k小|除2|最小函数值|序列合并|舞蹈课(C++)

P3378 【模板】堆 - 洛谷 #include <bits/stdc.h> using namespace std;const int N 1e6 10; int n; int heap[N];void up(int child) {int parent child / 2;while (parent > 1 && heap[child] < heap[parent]){swap(heap[child], heap[parent]);chil…

「实战指南 」Swift 并发中的任务取消机制

网罗开发 &#xff08;小红书、快手、视频号同名&#xff09; 大家好&#xff0c;我是 展菲&#xff0c;目前在上市企业从事人工智能项目研发管理工作&#xff0c;平时热衷于分享各种编程领域的软硬技能知识以及前沿技术&#xff0c;包括iOS、前端、Harmony OS、Java、Python等…