首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Tidymodel中的C5_rules()

Tidymodel中的C5_rules()
EN

Stack Overflow用户
提问于 2021-09-30 09:51:59
回答 1查看 167关注 0票数 1

我想使用tidymodels来拟合一个基于C5.0规则的分类模型。我已将模型指定如下

代码语言:javascript
复制
c5_spec <- 
  C5_rules() %>% 
  set_engine("C5.0") %>% 
  set_mode("classification")

在C5_rules()命令的文档中,我阅读了以下内容。

在对数据使用fit.model_spec()函数之前,不会对模型进行训练或拟合。

在此之后,我不太确定我需要如何处理这个欧洲狙击模型对象。每次我尝试拟合模型时,我都会得到以下错误

预处理器1/1,模型1/1 (预测):预测错误。C5.0(object= object$fit,newdata = new_data,type = "class"):必须提供树或规则

我遗漏了什么?

非常感谢!

EN

回答 1

Stack Overflow用户

发布于 2022-09-19 18:42:25

我尝试了Mark的reprex,得到了最后一个命令(conf_mat)的错误。

负载潮流模型及规则

代码语言:javascript
复制
library(tidymodels)
library(rules)
#> 
#> Attaching package: 'rules'
#> The following object is masked from 'package:dials':
#> 
#>     max_rules

# example training dataset
cars_train <- as_tibble(mtcars)

# change the number of cylinders to character for predicting as a class
cars_train <- 
  cars_train %>%
  mutate(cyl = as.character(cyl))

# training df
cars_train
#> # A tibble: 32 × 11
#>      mpg cyl    disp    hp  drat    wt  qsec    vs    am  gear  carb
#>    <dbl> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#>  1  21   6      160    110  3.9   2.62  16.5     0     1     4     4
#>  2  21   6      160    110  3.9   2.88  17.0     0     1     4     4
#>  3  22.8 4      108     93  3.85  2.32  18.6     1     1     4     1
#>  4  21.4 6      258    110  3.08  3.22  19.4     1     0     3     1
#>  5  18.7 8      360    175  3.15  3.44  17.0     0     0     3     2
#>  6  18.1 6      225    105  2.76  3.46  20.2     1     0     3     1
#>  7  14.3 8      360    245  3.21  3.57  15.8     0     0     3     4
#>  8  24.4 4      147.    62  3.69  3.19  20       1     0     4     2
#>  9  22.8 4      141.    95  3.92  3.15  22.9     1     0     4     2
#> 10  19.2 6      168.   123  3.92  3.44  18.3     1     0     4     4
#> # … with 22 more rows

# setup recipe with no preprocessing
cars_rec <-
  recipe(cyl ~ ., data = cars_train)

# specify c5 model; no need to set mode (can only be used for classification)
cars_spec <- 
  C5_rules() %>%
  set_engine("C5.0")

# create workflow
cars_wf <-
  workflow() %>%
  add_recipe(cars_rec) %>%
  add_model(cars_spec)

# fit workflow
cars_fit <- fit(cars_wf, data = cars_train)

# add predictions to df
cars_preds <- 
  predict(cars_fit, new_data = cars_train) %>%
  bind_cols(cars_train) %>%
  select(.pred_class, cyl)

cars_preds
#> # A tibble: 32 × 2
#>    .pred_class cyl  
#>    <fct>       <chr>
#>  1 6           6    
#>  2 6           6    
#>  3 4           4    
#>  4 6           6    
#>  5 8           8    
#>  6 6           6    
#>  7 8           8    
#>  8 4           4    
#>  9 4           4    
#> 10 6           6    
#> # … with 22 more rows


# confusion matrix
cars_preds %>%
  conf_mat(truth = cyl, 
           estimate = .pred_class)
#> Error in `yardstick_table()`:
#> ! `truth` must be a factor.
#> ℹ This is an internal error in the yardstick package, please report it to the package authors.

#> Backtrace:
#>     ▆
#>  1. ├─cars_preds %>% conf_mat(truth = cyl, estimate = .pred_class)
#>  2. ├─yardstick::conf_mat(., truth = cyl, estimate = .pred_class)
#>  3. └─yardstick:::conf_mat.data.frame(., truth = cyl, estimate = .pred_class)
#>  4.   └─yardstick:::yardstick_table(truth = truth, estimate = estimate, case_weights = case_weights)
#>  5.     └─rlang::abort("`truth` must be a factor.", .internal = TRUE)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69390070

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档