兄弟姐妹们,咱们今天讲讲机器学习。

         开始之前,需要回归前期内容的请按需查看:

R语言caret包:机器学习介绍(trainContral函数)-CSDN博客

R语言caret包:机器学习介绍(train函数)-CSDN博客

R语言caret包:机器学习示例(一)-CSDN博客

目录

4、模型训练  & 5、参数调优

6、模型评估

7、模型比较


上期内容,我们演示了数据预处理、数据分割、特征选择。下面我们介绍剩余内容。

4、模型训练  & 5、参数调优

        根据3、特征选择的结果,进行模型训练和参数调优。

## 模型训练 ##  ## 参数调优 ##
# 设置训练控制参数
control <- trainControl(
  method = "cv",          # 交叉验证
  number = 5,             # 5 折
  verboseIter = TRUE,     # 显示训练过程
  savePredictions = "final",  # 保存预测结果
  classProbs = F,      # 计算分类概率
  #summaryFunction = multiClassSummary  # 多分类评估
)
# 训练随机森林模型
set.seed(123)
rf_model1 <- train(   #train() 函数用于训练模型。
  BAI ~ AGE + CI + scPDSI + VPD, 
  data = train_processed, 
  method = "rf", 
  trControl = control,
  tuneLength = 3
)
# 查看结果
print(rf_model)
# 绘制调优结果
plot(rf_model)

        print(rf_model)

        plot(rf_model)

6、模型评估

        confusionMatrix()函数用于分类变量的预测;拟合变量我们可比较与真实值的差异及R2等指标。

## 模型评估 ##
# 根据最优模型生成预测值
predictions <- predict(rf_model,test_processed)
# 混淆矩阵
confusionMatrix(predictions, test_processed$lnBAI)

7、模型比较

## 模型比较 ##
# 训练多个模型
rf_model <- train(BAI ~ ., data = train_processed, method = "rf", trControl = control)
svm_model <- train(BAI ~ ., data = train_processed, method = "svmLinear", trControl = control)
knn_model <- train(BAI ~ ., data = train_processed, method = "knn", trControl = control)
# 比较模型性能
results <- resamples(list(RF = rf_model, SVM = svm_model, KNN = knn_model))
# 查看汇总结果
summary(results) 

# MAE平均绝对误差(误差的平均大小)
# RMSE均方根误差(误差的平方幅度)以上越小越好 
# Rsquared决定系数(模型解释了多少数据方差)越大越好

method常用方法有:

        1)极端随机树 exremely randomized trees (ERT):基于决策树的集成方法。与RF相比,ERT通过对树中的每个节点使用随机分裂在树构建过程中引入额外的随机性。RET对非线性问题和不平衡数据集具有较快的训练速度,对非线性问题和不平衡数据集具有良好的分类性能。

method = 'extraTrees'

Type: Regression, Classification

Tuning parameters:

  • mtry (# Randomly Selected Predictors)
  • numRandomCuts (# Random Cuts)

Required packages: extraTrees

        2)随机森林 random forest(RF)

 method = 'rf'

Type: Classification, Regression

Tuning parameters:

  • mtry (#Randomly Selected Predictors)

Required packages: randomForest

A model-specific variable importance metric is available.

         3)自适应增强Adaptive Boosting

method = 'gamboost'

Type: Regression, Classification

Tuning parameters:

  • mstop (# Boosting Iterations)
  • prune (AIC Prune?)

Required packages: mboostplyrimport

Notes: The prune option for this model enables the number of iterations to be determined by the optimal AIC value across all iterations. See the examples in ?mboost::mstop. If pruning is not used, the ensemble makes predictions using the exact value of the mstop tuning parameter value.

       4) 梯度增强Gradient Boosting

  method = 'gbm_h2o'

Type: Regression, Classification

Tuning parameters:

  • ntrees (# Boosting Iterations)
  • max_depth (Max Tree Depth)
  • min_rows (Min. Terminal Node Size)
  • learn_rate (Shrinkage)
  • col_sample_rate (#Randomly Selected Predictors)

Required packages: h2o

A model-specific variable importance metric is available.

        5)极端梯度增强eXtreme Gradient Boosting

 method = 'xgbDART'

Type: Regression, Classification

Tuning parameters:

  • nrounds (# Boosting Iterations)
  • max_depth (Max Tree Depth)
  • eta (Shrinkage)
  • gamma (Minimum Loss Reduction)
  • subsample (Subsample Percentage)
  • colsample_bytree (Subsample Ratio of Columns)
  • rate_drop (Fraction of Trees Dropped)
  • skip_drop (Prob. of Skipping Drop-out)
  • min_child_weight (Minimum Sum of Instance Weight)

Required packages: xgboostplyr

A model-specific variable importance metric is available.

 method = 'xgbLinear'

Type: Regression, Classification

Tuning parameters:

  • nrounds (# Boosting Iterations)
  • lambda (L2 Regularization)
  • alpha (L1 Regularization)
  • eta (Learning Rate)

Required packages: xgboost

A model-specific variable importance metric is available.

 method = 'xgbTree'

Type: Regression, Classification

Tuning parameters:

  • ntrees (# Boosting Iterations)
  • max_depth (Max Tree Depth)
  • min_rows (Min. Terminal Node Size)
  • learn_rate (Shrinkage)
  • col_sample_rate (#Randomly Selected Predictors)

Required packages: h2o

A model-specific variable importance metric is available.

        6)支持向量机 Support vector machine

method = 'svmLinear'

 Type: Regression, Classification

Tuning parameters:

  • C (Cost)

Required packages: kernlab

         7) 决策树 Decision Trees

method = 'rpart'

Type: Regression, Classification

Tuning parameters:

  • cp (Complexity Parameter)

Required packages: rpart

A model-specific variable importance metric is available.

       8) L2正则化 Logistic Regression with L2 regulation

method = 'plr'

Type: Classification

Tuning parameters:

  • lambda (L2 Penalty)
  • cp (Complexity Parameter)

Required packages: stepPlr

        9)多层感知 Multi-layer Perceptron

 method = 'mlpWeightDecay'

Type: Regression, Classification

Tuning parameters:

  • size (#Hidden Units)
  • decay (Weight Decay)

Required packages: RSNNS

        10)k近邻 K- Nearest Neighbors 

method = 'kknn'

Type: Regression, Classification

Tuning parameters:

  • kmax (Max. #Neighbors)
  • distance (Distance)
  • kernel (Kernel)

Required packages: kknn

 method = 'knn'

Type: Classification, Regression

Tuning parameters:

  • k (#Neighbors)

点点关注吧!!!

Logo

码道开发者社区,聚焦华为云码道 CodeArts 代码智能体,沉淀 Agent、Skill、鸿蒙开发实战内容,供开发者查阅资料、交流技术、分享工程实践

更多推荐