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

多项式回归

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

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

python 复制代码
# 曲线多项式的次数设置为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
python 复制代码
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
python 复制代码
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
python 复制代码
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

相关推荐
BHXDML1 小时前
第九章:EM 算法
人工智能·算法·机器学习
q_35488851531 小时前
AI大模型:python新能源汽车推荐系统 协同过滤推荐算法 Echarts可视化 Django框架 大数据毕业设计(源码+文档)✅
大数据·人工智能·python·机器学习·信息可视化·汽车·推荐算法
悟道|养家2 小时前
Cursor IDE 入门到高阶使用指南
ai编程
brent4233 小时前
DAY54 CBAM注意力
人工智能·深度学习·机器学习
Hcoco_me3 小时前
大模型面试题90:half2,float4这种优化 与 pack优化的底层原理是什么?
人工智能·算法·机器学习·langchain·vllm
程序员哈基耄4 小时前
浏览器文件格式转换工具:简单、安全、高效的文本与数据处理助手
python·安全·数据挖掘
卡尔AI工坊5 小时前
Andrej Karpathy:过去一年大模型的六个关键转折
人工智能·经验分享·深度学习·机器学习·ai编程
jay神6 小时前
指纹识别考勤打卡系统 - 完整源码项目
人工智能·深度学习·机器学习·计算机视觉·毕业设计
2501_944934736 小时前
数据分析:汽车销售转型的职场跳板
数据挖掘·数据分析·汽车
高洁016 小时前
数字孪生与数字样机的技术基础:建模与仿真
python·算法·机器学习·transformer·知识图谱