最近有小伙伴反映收不到推送,因为公众号改了推送算法,现在需要加星标,多点赞、点在看,才能准时收到推送哦。
导语:多模型机器学习并非指一个单一的技巧,而是一套工作流程:你同时训练多个不同类型的模型(如随机森林、支持向量机、梯度提升树等),然后通过交叉验证等方法客观地评估它们的性能,最终选择表现最好的模型,或甚至将多个模型组合起来(集成学习)以获得更好的性能。
北京墨因生物已经与国内50+知名医院的老师或名牌大学实验室合作(协和、哈工大、同济、哈医大等)。欢迎有生信分析需求的老师垂询,公共数据库数据挖掘或自测数据分析均可。

现代R语言进行机器学习,强烈推荐使用 tidymodels 元包。它是一套集成的、语法统一的包集合,其设计理念与著名的 tidyverse 一致,非常注重代码的清晰度和可读性。install.packages("tidymodels") install.packages("ranger") install.packages("glmnet") install.packages("kernlab") install.packages("xgboost")
第1步:加载必要的库并准备数据library(tidymodels) library(ranger) library(kernlab)
data(iris)glimpse(iris)table(iris$Species)
set.seed(123)
第2步:数据拆分iris_split iris_train iris_test
第3步:数据预处理(配方,Recipe)iris_recipe recipe(Species ~ ., data = iris_train) %>% step_normalize(all_numeric_predictors())
iris_recipe_preped prep(iris_recipe) bake(iris_recipe_preped, new_data = NULL)
第4步:定义多个模型rf_model mtry = tune(), trees = 500, min_n = tune() ) %>% set_engine("ranger", importance = "impurity") %>% set_mode("classification")
svm_model cost = tune(), rbf_sigma = tune() ) %>% set_engine("kernlab") %>% set_mode("classification")
knn_model neighbors = tune() ) %>% set_engine("kknn") %>% set_mode("classification")
第5步:创建工作流(Workflow)rf_workflow % add_recipe(iris_recipe) %>% add_model(rf_model)
svm_workflow % add_recipe(iris_recipe) %>% add_model(svm_model)
knn_workflow % add_recipe(iris_recipe) %>% add_model(knn_model)
第6步:超参数调优(Tuning)iris_folds 5)
rf_params % update(mtry = mtry(range = c(1L, 4L)))
svm_params knn_params
rf_tuned % tune_grid( resamples = iris_folds, grid = 10, metrics = metric_set(accuracy, roc_auc) )
svm_tuned % tune_grid( resamples = iris_folds, grid = 10, metrics = metric_set(accuracy, roc_auc) )
knn_tuned % tune_grid( resamples = iris_folds, grid = 10, metrics = metric_set(accuracy, roc_auc) )
第7步:评估和比较模型show_best(rf_tuned, metric = "accuracy")show_best(svm_tuned, metric = "accuracy")show_best(knn_tuned, metric = "accuracy")
rf_best "accuracy")svm_best "accuracy")knn_best "accuracy")
rf_final_workflow % finalize_workflow(rf_best)
svm_final_workflow % finalize_workflow(svm_best)knn_final_workflow % finalize_workflow(knn_best)
第8步:在测试集上进行最终评估rf_final_fit % last_fit(iris_split)svm_final_fit % last_fit(iris_split)knn_final_fit % last_fit(iris_split)
collect_metrics(rf_final_fit)collect_metrics(svm_final_fit)collect_metrics(knn_final_fit)
rf_accuracy % filter(.metric == "accuracy") %>% pull(.estimate)svm_accuracy % filter(.metric == "accuracy") %>% pull(.estimate)knn_accuracy % filter(.metric == "accuracy") %>% pull(.estimate)
cat(sprintf("随机森林测试集准确率: %.3f\n", rf_accuracy))cat(sprintf("支持向量机测试集准确率: %.3f\n", svm_accuracy))cat(sprintf("K近邻测试集准确率: %.3f\n", knn_accuracy))
第9步:拟合最终模型并用于预测best_model_fit % fit(iris)
new_flowers Sepal.Length = c(5.1, 6.7), Sepal.Width = c(3.5, 3.0), Petal.Length = c(1.4, 5.2), Petal.Width = c(0.2, 2.3))
predict(best_model_fit, new_data = new_flowers, type = "class")predict(best_model_fit, new_data = new_flowers, type = "prob")
码字不易,欢迎读者分享或转发到朋友圈,任何公众号或其他媒体未经许可不得私自转载或抄袭。由于微信平台算法改版,公众号内容将不再以时间排序展示,建议设置“作图丫”公众号为星标,防止丢失。星标具体步骤为:(2)点击右上角的小点点,在弹出界面选择“设为星标”即可。