【机器学习】机器学习学习笔记 - 监督学习 - 多项式回归决策树回归 - 03

news/2024/9/23 17:23:44/

多项式回归

  • 解决线性回归的准备性不足问题(线性回归只能是直线,多项式回归引入多项式可以是曲线)
  • 通过对预测值进行多项式转换, 使得回归模型可以是非线性的
  • 多项式回归的优点是可以处理非线性的数据
  • 多项式回归的缺点是它对数据进行了多项式转换

pdf在线免费转word文档 https://orcc.online/pdf

# 曲线多项式的次数设置为2
polynomial = PolynomialFeatures(degree=2)# 多项式回归
poly_linear_model = linear_model.LinearRegression()
# 多项式参数
# 多项式转换结果
X_train_transformed = polynomial.fit_transform(X_train)
# 训练模型
poly_linear_model.fit(X_train_transformed, y_train)# 多项式转换测试数据
X_test_transformed = polynomial.fit_transform(X_test)
# 通过转换的测试数据预测数据
y_test_poly_pred = poly_linear_model.predict(X_test_transformed)
print(y_test_poly_pred)

决策树回归

  • 集成学习(Ensemble Learning) 旨在通过组合多个基本模型(弱学习器)的预测创建一个强学习器(强学习器)
  • Boosting(提升) 和 Bagging(袋装) 都是集成学习的思想
  • Bagging(袋装) 原始数据集随机采样, 然后将采样的数据集分别训练多个决策树模型, 最后将这些决策树模型的预测结果进行投票(投票:分类, 回归:平均), 得到最终的预测结果
  • Boosting(提升) 通过迭代的方式, 训练多个决策树模型, 最后将这些决策树模型的预测结果进行加权平均, 得到最终的预测结果
  • bagging: 适用于高方差、低偏差的数据集 (random forest 随机森林)
  • boosting: 适用于高偏差、低方差的数据集 (AdaBoost 自适应增强)
  • 偏差: 预测结果的准确性
  • 方差: 预测结果的离散程度
决策树回归 decision tree regression
from sklearn.tree import DecisionTreeRegressor# 训练模型
# 决策树回归
# max_depth 树的深度, 设定位4,防止任意深度过深,导致过拟合
dt_regressor = DecisionTreeRegressor(max_depth=4)
dt_regressor.fit(X_train, y_train)y_pred_dt = dt_regressor.predict(X_test)
mse = mean_squared_error(y_test, y_pred_dt)
evs = explained_variance_score(y_test, y_pred_dt)
print("#### Decision Tree performance ####")
print("Mean squared error (均方误差/平均平方误差 (越小越好)) = ", round(mse, 2))
print("Explained variance score (解释方差分 (0-1) 1 接近表示解释能力越好) =", round(evs, 2))# 决策树特征权重
def plot_feature_importances(feature_importances, title, feature_names):# 将重要性值标准化feature_importances = 100.0 * (feature_importances / max(feature_importances))# 将得分从高到低排序index_sorted = np.flipud(np.argsort(feature_importances))# 让X坐标轴上的标签居中显示pos = np.arange(index_sorted.shape[0]) + 0.5# 画条形图plt.figure()plt.bar(pos, feature_importances[index_sorted], align='center')print(pos, index_sorted, feature_importances)plt.xticks(pos, [feature_names[i] for i in index_sorted])plt.ylabel('Relative Importance')plt.title(title)plt.show()# print(dt_regressor.feature_importances_)
# print(dt_regressor.feature_importances_)
plot_feature_importances(dt_regressor.feature_importances_, 'Decision Tree regressor', data_columns)
自适应决策树回归 adaboost regressor
from sklearn.ensemble import AdaBoostRegressor# AdaBoost: adaptive boosting (自适应增强)
# n_estimators 基学习器的个数
ab_regressor = AdaBoostRegressor(DecisionTreeRegressor(max_depth=4), n_estimators=400, random_state=7)
ab_regressor.fit(X_train, y_train)y_pred_ab = ab_regressor.predict(X_test)
mse = mean_squared_error(y_test, y_pred_ab)
evs = explained_variance_score(y_test, y_pred_ab)
print("#### AdaBoost performance ####")
print("Mean squared error (均方误差/平均平方误差 (越小越好)) = ", round(mse, 2))
print("Explained variance score (解释方差分 (0-1) 1 接近表示解释能力越好) =", round(evs, 2))def plot_feature_importances(feature_importances, title, feature_names):# 将重要性值标准化feature_importances = 100.0 * (feature_importances / max(feature_importances))# 将得分从高到低排序index_sorted = np.flipud(np.argsort(feature_importances))# 让X坐标轴上的标签居中显示pos = np.arange(index_sorted.shape[0]) + 0.5# 画条形图plt.figure()plt.bar(pos, feature_importances[index_sorted], align='center')print(pos, index_sorted, feature_importances)plt.xticks(pos, [feature_names[i] for i in index_sorted])plt.ylabel('Relative Importance')plt.title(title)plt.show()plot_feature_importances(ab_regressor.feature_importances_, 'AdaBoost regressor', data_columns)
随机森林回归 random forest regressor
from sklearn.ensemble import RandomForestRegressor# 训练模型
# 随机森林回归
# 随机森林回归, n_estimators 决策树的个数, max_depth 决策树的深度, min_samples_split 决策树的最小样本数
rf_regressor = RandomForestRegressor(n_estimators=1000, max_depth=4, min_samples_split=2)
rf_regressor.fit(X_train, y_train)y_pred_rf = rf_regressor.predict(X_test)
mse = mean_squared_error(y_test, y_pred_rf)
evs = explained_variance_score(y_test, y_pred_rf)
print("#### Random Forest Regressor performance ####")
print("Mean squared error (均方误差/平均平方误差 (越小越好)) = ", round(mse, 2))
print("Explained variance score (解释方差分 (0-1) 1 接近表示解释能力越好) =", round(evs, 2))def plot_feature_importances(feature_importances, title, feature_names):# 将重要性值标准化feature_importances = 100.0 * (feature_importances / max(feature_importances))# 将得分从高到低排序index_sorted = np.flipud(np.argsort(feature_importances))# 让X坐标轴上的标签居中显示pos = np.arange(index_sorted.shape[0]) + 0.5# 画条形图plt.figure()plt.bar(pos, feature_importances[index_sorted], align='center')print(pos, index_sorted, feature_importances)plt.xticks(pos, [feature_names[i] for i in index_sorted])plt.ylabel('Relative Importance')plt.title(title)plt.show()# print(dt_regressor.feature_importances_)
# print(dt_regressor.feature_importances_)
plot_feature_importances(rf_regressor.feature_importances_, 'random forest regressor', data_columns)

IT免费在线工具网 https://orcc.online


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

相关文章

互联网大厂ssp面经,数据结构:part1

1. 数组和链表的区别是什么? a. 数组是一种线性数据结构,存储在连续的内存块中,元素可以通过索引直接访问。 b. 链表是由节点组成的数据结构,每个节点包含数据和指向下一个节点的指针。 2. 数组和链表的的优缺点是什么&#xff…

【Linux学习】初始冯诺漫体系结构

文章目录 认识冯诺依曼系统 认识冯诺依曼系统 什么是冯诺依曼体系结构? 冯诺依曼体系结构是一种将程序指令和数据以二进制形式存放在主存储器中,由中央处理器统一控制和执行的计算机系统结构。冯诺依曼体系结构实现了程序的可编程性和硬件与软件的分离&…

量子密钥分发系统设计与实现(一):系统基本架构讨论

经过一段时间讨论,我们了解到量子密钥分发设备是当前量子保密通信系统的基础。从本文开始,我将开启量子密钥分发系统设计与实现系列,详细讨论量子密钥分发设备如何从0到1的搭建。 1.QKD系统总体讨论 QKD系统的核心功能就是为通信双方提供理论…

LLM 编码的过程

句子 》句子预处理 (把太长的句子截短,在句子中添加首尾标识符)》分词 》编码(词典 vocabulary) tokenizer.encode 参与了 step 1、2、3 transformer 参与了 step 4、5、6、7 LLM 编码的过程 LLM(大型语言模型)编码是将输入文本转换为神经网络可以处理的数字表示的过…

安卓(Android)安装 Microsoft Authenticator 搞定 2FA 验证

现在越来越多网站强制 2FA 验证了,Microsoft Authenticator 是一个兼容性很强的 2FA 应用 app,如果能够安装它,可以解决绝大部分的 2FA 验证问题。 但是,在国内如果你用苹果手机还好,如果是用安卓(Android…

深度学习中几种常见函数介绍(SoftMax,ReLU,Sigmoid,Tanh)

在机器学习中,尤其是在深度学习模型中,使用各种不同的数学函数来实现网络的非线性、归一化或激活。这些函数各有特点和用途。下面我将介绍几种常见的函数,并解释它们在实践中的应用和区别。 尊贵无比的GPT4用户,非常开心为您服务,我是您的AI助手,我将竭诚为您服务! 在…

295. 数据流的中位数

295. 数据流的中位数 中位数是有序整数列表中的中间值。如果列表的大小是偶数,则没有中间值,中位数是两个中间值的平均值。例如 arr [2,3,4] 的中位数是 3 。 例如 arr [2,3] 的中位数是 (2 3) / 2 2.5 。 实现 MedianFinder 类:MedianFinder() 初始…

数字化校园引领未来

在当今科技日新月异的时代,数字化转型已成为各行各业的必然趋势,教育领域也不例外。随着信息技术的迅猛发展,数字化已经成为推动教育变革的重要力量,它不仅重塑了传统的教育模式,还为学生、教师以及整个教育生态系统带…