数据科学中的 R 语言
创作时间:
作者:
@小白创作中心
数据科学中的 R 语言
引用
1
来源
1.
https://bookdown.org/wangminjie/R4DS/tidymodels-intro.html
第 63 章 机器学习
Rstudio工厂的Max Kuhn大神正主持机器学习的开发,日臻成熟了,感觉很强大啊。
63.2 机器学习
split <- penguins %>%
mutate(species = fct_lump(species, 1)) %>%
initial_split()
split
training_data <- training(split)
training_data
testing_data <- testing(split)
testing_data
63.7 workflow
63.7.1 使用 recipes
参考tidy modeling in R, 被预测变量在分割前,应该先处理,比如标准化。 但这里的案例,我为了偷懒,被预测变量
bill_length_mm
,暂时保留不变。 预测变量做标准处理。
penguins_lm <-
#parsnip::set_engine("lm")
penguins_recipe <-
recipes::recipe(bill_length_mm ~ bill_depth_mm + sex, data = training_data) %>%
recipes::step_normalize(all_numeric(), -all_outcomes()) %>%
recipes::step_dummy(all_nominal())
broom::tidy(penguins_recipe)
## # A tibble: 2 × 6
## number operation type trained skip id
## <int> <chr> <chr> <lgl> <lgl> <chr>
## 1 1 step normalize FALSE FALSE normalize_zs0oP
## 2 2 step dummy FALSE FALSE dummy_Rh8f7
63.7.2 workflows的思路更清晰
workflows的思路让模型结构更清晰。 这样
prep()
,
bake()
, and
juice()
就可以省略了,只需要recipe和model,他们往往是成对出现的
wflow <-
workflows::add_recipe(penguins_recipe) %>%
workflows::add_model(penguins_lm)
wflow_fit <-
wflow %>%
parsnip::fit(data = training_data)
wflow_fit %>%
## # A tibble: 3 × 3
## term estimate std.error
## <chr> <dbl> <dbl>
## 1 (Intercept) 41.1 0.442
## 2 bill_depth_mm -2.33 0.297
## 3 sex_male 5.68 0.634
wflow_fit %>%
先提取模型,用在
predict()
是可以的,但这样太麻烦了
wflow_fit %>%
stats::predict(new_data = test_data) # note: test_data not testing_data
因为,
predict()
会自动的将recipes(对training_data的操作),应用到testing_data 这个不错,参考这里
penguins_pred <-
predict(
wflow_fit,
new_data = testing_data %>% dplyr::select(-bill_length_mm), # note: testing_data not test_data
type = "numeric"
) %>%
dplyr::bind_cols(testing_data %>% dplyr::select(bill_length_mm))
penguins_pred
## # A tibble: 84 × 2
## .pred bill_length_mm
## <dbl> <dbl>
## 1 40.2 38.9
## 2 42.5 42.5
## 3 45.6 37.2
## 4 41.2 36.4
## 5 43.3 38.8
## 6 39.4 42.2
## 7 44.4 39.8
## 8 40.0 36.5
## 9 39.4 36
## 10 43.7 44.1
## # ℹ 74 more rows
penguins_pred %>%
ggplot(aes(x = bill_length_mm, y = .pred)) +
labs(y = "Predicted ", x = "bill_length_mm")
augment()
具有
predict()
一样的功能和特性,还更简练的多
wflow_fit %>%
augment(new_data = testing_data) %>% # note: testing_data not test_data
ggplot(aes(x = bill_length_mm, y = .pred)) +
labs(y = "Predicted ", x = "bill_length_mm")
63.7.3 模型评估
参考https://www.tmwr.org/performance.html#regression-metrics
penguins_pred %>%
yardstick::rmse(truth = bill_length_mm, estimate = .pred)
## # A tibble: 1 × 3
## .metric .estimator .estimate
## <chr> <chr> <dbl>
## 1 rmse standard 4.94

自定义一个指标评价函数my_multi_metric,就是放一起,感觉不够tidyverse
my_multi_metric <- yardstick::metric_set(rmse, rsq, mae, ccc)
penguins_pred %>%
my_multi_metric(truth = bill_length_mm, estimate = .pred)
## # A tibble: 4 × 3
## .metric .estimator .estimate
## <chr> <chr> <dbl>
## 1 rmse standard 4.94

## 2 rsq standard 0.179
## 3 mae standard 4.10
## 4 ccc standard 0.335
热门推荐
棋类游戏:智力与策略的较量,探索围棋、国际象棋与象棋的魅力与技巧
康德:意志是否自由?灵魂是否不朽?上帝是否实存? | 纯粹纪念
如何利用AI绘画生成软件提升数字艺术创作的效率与质量
安康鱼:营养价值、功效、禁忌及食用方式
驱车两小时,从北京出发探索崇礼的夏日清凉之旅
10 次难忘的经历:在老挝可以做什么
《山海经》上古异兽:軨軨、兽身人面神、䍶䍶、居暨
颈椎做磁共振好还是ct好
2025高考报名的条件与要求: 哪些人不可以参加?
租房还是买房?一文详解两种居住方式的优劣与决策方法
养哈士奇须知:生活习性与饮食要求全攻略
趋势线的定义是什么?如何利用趋势线进行投资分析?
微服务架构中常用的多级缓存设计,建议收藏!
深夜,集体崩了!英伟达2万亿元市值灰飞烟灭,加密货币超32万人爆仓
浪涌保护器的应用场景与工作原理详解
WHO 发布新版慢性乙型肝炎防治指南,扩大治疗人群,简化管理是关键!
孩子人际关系不好怎么办?心理师教你,这样让孩子好人缘
易经智慧与团队管理
学英语,从“有点”开始:掌握基本技巧,打开语言学习新大门
思维方式决定情绪状态,做情绪的掌控者才是王者
卫青、霍去病死后,他们的子孙是何结局?多人死于非命
销售分析如何利用数据分析和指标评估提升销售业绩?
传统体育与电子竞技正相向而行,共创体育新篇章
浙江天一阁:藏书楼中的璀璨明珠
数据分析师的职业前景究竟有多广阔?揭开隐藏的机会与挑战!
海淀,有“秋”必应 | 一山如画万千秋色,是凤凰岭给秋天的应答
告别"废片":手机修图入门指南
古希腊德尔斐神庙:从神话到考古的历史传奇
电商风控指南 | 直播间里的藏匿的“羊毛党”,普通消费者看不到
南京再增5000多共享泊位,城郊地铁换乘区域停车难题不断破解