Skip to content

Commit fe9cdbf

Browse files
Merge pull request #108 from tidymodels/fix-94
make sure ClusterR predict method also rearranges
2 parents ac2168e + c296c4d commit fe9cdbf

File tree

3 files changed

+10
-3
lines changed

3 files changed

+10
-3
lines changed

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# tidyclust (development version)
22

3+
* Fixed bug where `extract_cluster_assignment()` and `predict()` sometimes didn't have agreement of clusters. (#94)
4+
35
* `silhouette()` and `silhouette_avg()` now return NAs instead of erroring when applied to a clustering object with 1 cluster. (#104)
46

57
* Fixed bug where `extract_cluster_assignment()` doesn't work for `hier_clust()` models in workflows where `num_clusters` is specified in `extract_cluster_assignment()`.

R/extract_assignment.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ cluster_assignment_tibble <- function(clusters,
6262
n_clusters,
6363
...,
6464
prefix = "Cluster_") {
65-
reorder_clusts <- order(unique(clusters))
65+
reorder_clusts <- order(union(unique(clusters), seq_len(n_clusters)))
6666
names <- paste0(prefix, seq_len(n_clusters))
6767
res <- names[reorder_clusts][clusters]
6868

R/predict_helpers.R

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,13 @@ stats_kmeans_predict <- function(object, new_data, prefix = "Cluster_") {
77
}
88

99
clusterR_kmeans_predict <- function(object, new_data, prefix = "Cluster_") {
10-
res <- predict(object, new_data)
11-
res <- paste0(prefix, res)
10+
clusters <- predict(object, new_data)
11+
n_clusters <- length(object$obs_per_cluster)
12+
13+
reorder_clusts <- order(union(unique(clusters), seq_len(n_clusters)))
14+
names <- paste0(prefix, seq_len(n_clusters))
15+
res <- names[reorder_clusts][clusters]
16+
1217
factor(res)
1318
}
1419

0 commit comments

Comments
 (0)