tidymodels / tidyclust

A tidy unified interface to clustering models
https://tidyclust.tidymodels.org/
Other
108 stars 14 forks source link

make sure that prefix argument is passed through extraction functions #145

Closed EmilHvitfeldt closed 1 year ago

EmilHvitfeldt commented 1 year ago

These functions now follow the documented behavior.

library(tidyclust)

spec <- k_means(num_clusters = 4) |>
  fit(~., data = mtcars)

extract_centroids(spec)
#> # A tibble: 4 × 12
#>   .cluster    mpg   cyl  disp    hp  drat    wt  qsec    vs    am  gear  carb
#>   <fct>     <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 Cluster_1  15.1     8 353.  209.   3.23  4.00  16.8 0     0.143  3.29  3.5 
#> 2 Cluster_2  19.7     6 183.  122.   3.59  3.12  18.0 0.571 0.429  3.86  3.43
#> 3 Cluster_3  24.2     4 122.   94.3  3.92  2.51  19.1 0.857 0.571  4.14  1.71
#> 4 Cluster_4  31       4  76.1  62.2  4.33  1.90  19.2 1     1      4     1.25

extract_centroids(spec, prefix = "c_")
#> # A tibble: 4 × 12
#>   .cluster   mpg   cyl  disp    hp  drat    wt  qsec    vs    am  gear  carb
#>   <fct>    <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 c_1       15.1     8 353.  209.   3.23  4.00  16.8 0     0.143  3.29  3.5 
#> 2 c_2       19.7     6 183.  122.   3.59  3.12  18.0 0.571 0.429  3.86  3.43
#> 3 c_3       24.2     4 122.   94.3  3.92  2.51  19.1 0.857 0.571  4.14  1.71
#> 4 c_4       31       4  76.1  62.2  4.33  1.90  19.2 1     1      4     1.25

extract_cluster_assignment(spec)
#> # A tibble: 32 × 1
#>    .cluster 
#>    <fct>    
#>  1 Cluster_1
#>  2 Cluster_1
#>  3 Cluster_2
#>  4 Cluster_1
#>  5 Cluster_3
#>  6 Cluster_1
#>  7 Cluster_3
#>  8 Cluster_2
#>  9 Cluster_2
#> 10 Cluster_1
#> # ℹ 22 more rows

extract_cluster_assignment(spec, prefix = "c_")
#> # A tibble: 32 × 1
#>    .cluster
#>    <fct>   
#>  1 c_1     
#>  2 c_1     
#>  3 c_2     
#>  4 c_1     
#>  5 c_3     
#>  6 c_1     
#>  7 c_3     
#>  8 c_2     
#>  9 c_2     
#> 10 c_1     
#> # ℹ 22 more rows

predict(spec, mtcars)
#> # A tibble: 32 × 1
#>    .pred_cluster
#>    <fct>        
#>  1 Cluster_1    
#>  2 Cluster_1    
#>  3 Cluster_2    
#>  4 Cluster_1    
#>  5 Cluster_3    
#>  6 Cluster_1    
#>  7 Cluster_3    
#>  8 Cluster_2    
#>  9 Cluster_2    
#> 10 Cluster_1    
#> # ℹ 22 more rows

predict(spec, mtcars, prefix = "c_")
#> # A tibble: 32 × 1
#>    .pred_cluster
#>    <fct>        
#>  1 c_1          
#>  2 c_1          
#>  3 c_2          
#>  4 c_1          
#>  5 c_3          
#>  6 c_1          
#>  7 c_3          
#>  8 c_2          
#>  9 c_2          
#> 10 c_1          
#> # ℹ 22 more rows

Created on 2023-08-11 with reprex v2.0.2

github-actions[bot] commented 1 year ago

This pull request has been automatically locked. If you believe you have found a related problem, please file a new issue (with a reprex: https://reprex.tidyverse.org) and link to this issue.