mlr-org / mlr3cluster

@@ -28,7 +28,7 @@
Loading
28 28
              } else if (test_int(x)) {
29 29
                assert_true(x >= 1L)
30 30
              } else {
31 -
                return("centers must be either integer or data.frame with initial cluster centers!")
31 +
                return("`centers`` must be either integer or data.frame with initial cluster centers")
32 32
              }
33 33
            }
34 34
          ),
@@ -58,13 +58,13 @@
Loading
58 58
    .train = function(task) {
59 59
      if ("nstart" %in% names(self$param_set$values)) {
60 60
        if (!test_int(self$param_set$values$centers)) {
61 -
          warning("warning: `nstart` parameter is only relevant when `centers` is integer")
61 +
          warning("`nstart` parameter is only relevant when `centers` is integer.")
62 62
        }
63 63
      }
64 64
65 65
      if (test_data_frame(self$param_set$values$centers)) {
66 66
        if (length(self$param_set$values$centers) != task$ncol) {
67 -
          stop("`centers` must have same number of columns as data!")
67 +
          stop("`centers` must have same number of columns as data.")
68 68
        }
69 69
      }
70 70

@@ -25,7 +25,7 @@
Loading
25 25
            if (test_numeric(x)) {
26 26
              return(TRUE)
27 27
            } else {
28 -
              stop("`weights` need to be a numeric vector!")
28 +
              stop("`weights` need to be a numeric vector")
29 29
            }
30 30
          }, tags = "train"),
31 31
          ParamFct$new(

@@ -38,6 +38,7 @@
Loading
38 38
  x$add("clust.agnes", LearnerClustAgnes)
39 39
  x$add("clust.diana", LearnerClustDiana)
40 40
  x$add("clust.fanny", LearnerClustFanny)
41 +
  x$add("clust.cmeans", LearnerClustCMeans)
41 42
  x$add("clust.dbscan", LearnerClustDBSCAN)
42 43
  x$add("clust.xmeans", LearnerClustXMeans)
43 44

@@ -0,0 +1,101 @@
Loading
1 +
#' @title Fuzzy C-Means Clustering Learner
2 +
#'
3 +
#' @name mlr_learners_clust.cmeans
4 +
#' @include LearnerClust.R
5 +
#'
6 +
#' @description
7 +
#' A [LearnerClust] for fuzzy clustering implemented in [e1071::cmeans()].
8 +
#' The default number of clusters has been initialized to 2.
9 +
#'
10 +
#' @templateVar id clust.cmeans
11 +
#' @template section_dictionary_learner
12 +
#'
13 +
#' @export
14 +
LearnerClustCMeans = R6Class("LearnerClustCMeans",
15 +
  inherit = LearnerClust,
16 +
  public = list(
17 +
    #' @description
18 +
    #' Creates a new instance of this [R6][R6::R6Class] class.
19 +
    initialize = function() {
20 +
      ps = ParamSet$new(
21 +
        params = list(
22 +
          ParamUty$new(
23 +
            id = "centers", tags = c("required", "train"), default = 2L,
24 +
            custom_check = function(x) {
25 +
              if (test_data_frame(x)) {
26 +
                return(TRUE)
27 +
              } else if (test_int(x)) {
28 +
                assert_true(x >= 1L)
29 +
              } else {
30 +
                return("`centers` must be either integer or data.frame with initial cluster centers")
31 +
              }
32 +
            }
33 +
          ),
34 +
          ParamInt$new(id = "iter.max", lower = 1L, default = 100L, tags = "train"),
35 +
          ParamLgl$new(id = "verbose", default = FALSE, tags = "train"),
36 +
          ParamFct$new(
37 +
            id = "dist", levels = c("euclidean", "manhattan"),
38 +
            default = "euclidean", tags = "train"),
39 +
          ParamFct$new(
40 +
            id = "method", levels = c("cmeans", "ufcl"),
41 +
            default = "cmeans", tags = "train"),
42 +
          ParamDbl$new(id = "m", lower = 1L, default = 2L, tags = "train"),
43 +
          ParamDbl$new(id = "rate.par", lower = 0L, upper = 1L, tags = "train"),
44 +
          ParamUty$new(
45 +
            id = "weights", default = 1L,
46 +
            custom_check = function(x) {
47 +
              if (test_numeric(x)) {
48 +
                if (sum(sign(x)) == length(x)) {
49 +
                  return(TRUE)
50 +
                } else {
51 +
                  return("`weights` must contain only positive numbers")
52 +
                }
53 +
              } else if (test_count(x)) {
54 +
                return(TRUE)
55 +
              } else {
56 +
                return("`weights` needs to be either a numeric vector with all positive values or a single positive number")
57 +
              }
58 +
            },
59 +
            tags = "train"),
60 +
          ParamUty$new(id = "control")
61 +
        )
62 +
      )
63 +
      # add deps
64 +
      ps$add_dep("rate.par", "method", CondEqual$new("ufcl"))
65 +
66 +
      ps$values = list(
67 +
        centers = 2L, iter.max = 100L, verbose = FALSE,
68 +
        dist = "euclidean", m = 2L, weights = 1L)
69 +
70 +
      super$initialize(
71 +
        id = "clust.cmeans",
72 +
        feature_types = c("logical", "integer", "numeric"),
73 +
        predict_types = c("partition", "prob"),
74 +
        param_set = ps,
75 +
        properties = c("partitional", "fuzzy", "complete"),
76 +
        packages = "cluster"
77 +
      )
78 +
    }
79 +
  ),
80 +
81 +
  private = list(
82 +
    .train = function(task) {
83 +
      if (test_data_frame(self$param_set$values$centers)) {
84 +
        if (length(self$param_set$values$centers) != task$ncol) {
85 +
          stop("`centers` must have same number of columns as data.")
86 +
        }
87 +
      }
88 +
89 +
      pv = self$param_set$get_values(tags = "train")
90 +
      invoke(e1071::cmeans, x = task$data(), .args = pv)
91 +
    },
92 +
93 +
    .predict = function(task) {
94 +
      partition = unclass(cl_predict(self$model, newdata = task$data(), type = "class_ids"))
95 +
      prob = unclass(cl_predict(self$model, newdata = task$data(), type = "memberships"))
96 +
      colnames(prob) = seq_len(ncol(prob))
97 +
98 +
      PredictionClust$new(task = task, partition = partition, prob = prob)
99 +
    }
100 +
  )
101 +
)
Files Coverage
R 53.68%
Project Totals (17 files) 53.68%

No yaml found.

Create your codecov.yml to customize your Codecov experience

Sunburst
The inner-most circle is the entire project, moving away from the center are folders then, finally, a single file. The size and color of each slice is representing the number of statements and the coverage, respectively.
Icicle
The top section represents the entire project. Proceeding with folders and finally individual files. The size and color of each slice is representing the number of statements and the coverage, respectively.
Grid
Each block represents a single file in the project. The size and color of each block is represented by the number of statements and the coverage, respectively.
Loading