Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Weights reworked #87

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/dev-cmd-check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ jobs:
matrix:
config:
- {os: ubuntu-latest, r: 'release', dev-package: 'mlr-org/paradox'}
- {os: ubuntu-latest, r: 'release', dev-package: 'mlr-org/mlr3@weights_reworked'}

steps:
- uses: actions/checkout@v4
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
# Created by https://www.toptal.com/developers/gitignore/api/visualstudiocode,linux,r
# Edit at https://www.toptal.com/developers/gitignore?templates=visualstudiocode,linux,r

tests/testthat/Rplots.pdf

### Linux ###
*~

Expand Down
2 changes: 1 addition & 1 deletion R/LearnerClustAffinityPropagation.R
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ LearnerClustAP = R6Class("LearnerClustAP",
},

.predict = function(task) {
pv = self$param_set$get_values()
pv = self$param_set$get_values(tags = "train")
sim_func = pv$s
exemplar_data = attributes(self$model)$exemplar_data

Expand Down
4 changes: 2 additions & 2 deletions R/LearnerClustAgnes.R
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ LearnerClustAgnes = R6Class("LearnerClustAgnes",
tags = "train"
),
trace.lev = p_int(0L, default = 0L, tags = "train"),
k = p_int(1L, default = 2L, tags = "predict"),
k = p_int(1L, default = 2L, tags = c("train", "predict")),
par.method = p_uty(
tags = "train",
depends = quote(method %in% c("flexible", "gaverage")),
Expand Down Expand Up @@ -65,7 +65,7 @@ LearnerClustAgnes = R6Class("LearnerClustAgnes",
),
private = list(
.train = function(task) {
pv = self$param_set$get_values()
pv = self$param_set$get_values(tags = "train")
m = invoke(cluster::agnes,
x = task$data(),
diss = FALSE,
Expand Down
15 changes: 7 additions & 8 deletions R/LearnerClustCMeans.R
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,7 @@ LearnerClustCMeans = R6Class("LearnerClustCMeans",
method = p_fct(levels = c("cmeans", "ufcl"), default = "cmeans", tags = "train"),
m = p_dbl(1, default = 2, tags = "train"),
rate.par = p_dbl(0, 1, tags = "train", depends = quote(method == "ufcl")),
weights = p_uty(default = 1L, tags = "train", custom_check = crate(function(x) {
if (test_numeric(x) && all(x > 0) || check_count(x, positive = TRUE)) {
TRUE
} else {
"`weights` must be positive numeric vector or a single positive number"
}
})),
use_weights = p_lgl(default = FALSE, tags = "train"),
control = p_uty(tags = "train")
)

Expand All @@ -51,7 +45,7 @@ LearnerClustCMeans = R6Class("LearnerClustCMeans",
feature_types = c("logical", "integer", "numeric"),
predict_types = c("partition", "prob"),
param_set = param_set,
properties = c("partitional", "fuzzy", "complete"),
properties = c("partitional", "fuzzy", "complete", "weights"),
packages = "e1071",
man = "mlr3cluster::mlr_learners_clust.cmeans",
label = "Fuzzy C-Means Clustering Learner"
Expand All @@ -63,6 +57,11 @@ LearnerClustCMeans = R6Class("LearnerClustCMeans",
pv = self$param_set$get_values(tags = "train")
assert_centers_param(pv$centers, task, test_data_frame, "centers")

if (isTRUE(pv$use_weights)) {
pv$weights = task$weights_learner$weight
}
pv$use_weights = NULL

m = invoke(e1071::cmeans, x = task$data(), .args = pv, .opts = allow_partial_matching)
if (self$save_assignments) {
self$assignments = m$cluster
Expand Down
16 changes: 12 additions & 4 deletions R/LearnerClustDBSCAN.R
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ LearnerClustDBSCAN = R6Class("LearnerClustDBSCAN",
eps = p_dbl(0, tags = c("required", "train")),
minPts = p_int(0L, default = 5L, tags = "train"),
borderPoints = p_lgl(default = TRUE, tags = "train"),
weights = p_uty(tags = "train", custom_check = check_numeric),
use_weights = p_lgl(default = FALSE, tags = "train"),
search = p_fct(levels = c("kdtree", "linear", "dist"), default = "kdtree", tags = "train"),
bucketSize = p_int(1L, default = 10L, tags = "train", depends = quote(search == "kdtree")),
splitRule = p_fct(
Expand All @@ -42,7 +42,7 @@ LearnerClustDBSCAN = R6Class("LearnerClustDBSCAN",
feature_types = c("logical", "integer", "numeric"),
predict_types = "partition",
param_set = param_set,
properties = c("density", "exclusive", "complete"),
properties = c("density", "exclusive", "complete", "weights"),
packages = "dbscan",
man = "mlr3cluster::mlr_learners_clust.dbscan",
label = "Density-Based Clustering"
Expand All @@ -52,11 +52,19 @@ LearnerClustDBSCAN = R6Class("LearnerClustDBSCAN",
private = list(
.train = function(task) {
pv = self$param_set$get_values(tags = "train")
m = invoke(dbscan::dbscan, x = task$data(), .args = pv)
m = insert_named(m, list(data = task$data()))
data = task$data()

if (isTRUE(pv$use_weights)) {
pv$weights = task$weights_learner$weight
}
pv$use_weights = NULL

m = invoke(dbscan::dbscan, x = data, .args = pv)
m = insert_named(m, list(data = data))
if (self$save_assignments) {
self$assignments = m$cluster
}

m
},

Expand Down
5 changes: 3 additions & 2 deletions R/LearnerClustDBSCANfpc.R
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,9 @@ LearnerClustDBSCANfpc = R6Class("LearnerClustDBSCANfpc",
private = list(
.train = function(task) {
pv = self$param_set$get_values(tags = "train")
m = invoke(fpc::dbscan, data = task$data(), .args = pv)
m = insert_named(m, list(data = task$data()))
data = task$data()
m = invoke(fpc::dbscan, data = data, .args = pv)
m = insert_named(m, list(data = data))
if (self$save_assignments) {
self$assignments = m$cluster
}
Expand Down
4 changes: 2 additions & 2 deletions R/LearnerClustDiana.R
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ LearnerClustDiana = R6Class("LearnerClustDiana",
metric = p_fct(default = "euclidean", levels = c("euclidean", "manhattan"), tags = "train"),
stand = p_lgl(default = FALSE, tags = "train"),
trace.lev = p_int(0L, default = 0L, tags = "train"),
k = p_int(1L, default = 2L, tags = "predict")
k = p_int(1L, default = 2L, tags = c("train", "predict"))
)

param_set$set_values(k = 2L)
Expand All @@ -46,7 +46,7 @@ LearnerClustDiana = R6Class("LearnerClustDiana",
),
private = list(
.train = function(task) {
pv = self$param_set$get_values()
pv = self$param_set$get_values(tags = "train")
m = invoke(cluster::diana,
x = task$data(),
diss = FALSE,
Expand Down
5 changes: 3 additions & 2 deletions R/LearnerClustHDBSCAN.R
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,9 @@ LearnerClustHDBSCAN = R6Class("LearnerClustHDBSCAN",
private = list(
.train = function(task) {
pv = self$param_set$get_values(tags = "train")
m = invoke(dbscan::hdbscan, x = task$data(), .args = pv)
m = insert_named(m, list(data = task$data()))
data = task$data()
m = invoke(dbscan::hdbscan, x = data, .args = pv)
m = insert_named(m, list(data = data))

if (self$save_assignments) {
self$assignments = m$cluster
Expand Down
4 changes: 2 additions & 2 deletions R/LearnerClustHclust.R
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ LearnerClustHclust = R6Class("LearnerClustHclust",
diag = p_lgl(default = FALSE, tags = c("train", "dist")),
upper = p_lgl(default = FALSE, tags = c("train", "dist")),
p = p_dbl(default = 2, tags = c("train", "dist"), depends = quote(distmethod == "minkowski")),
k = p_int(1L, default = 2L, tags = "predict")
k = p_int(1L, default = 2L, tags = c("train", "predict"))
)

param_set$set_values(k = 2L, distmethod = "euclidean")
Expand All @@ -54,7 +54,7 @@ LearnerClustHclust = R6Class("LearnerClustHclust",
),
private = list(
.train = function(task) {
pv = self$param_set$get_values()
pv = self$param_set$get_values(tags = "train")
dist = invoke(stats::dist,
x = task$data(),
method = pv$d %??% "euclidean",
Expand Down
5 changes: 3 additions & 2 deletions R/LearnerClustKKMeans.R
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,13 @@ LearnerClustKKMeans = R6Class("LearnerClustKKMeans",

c = kernlab::centers(self$model)
K = kernlab::kernelf(self$model)
data = task$data()

# kernel product between each new datapoint and the centers
d_xc = matrix(kernlab::kernelMatrix(K, as.matrix(task$data()), c), ncol = nrow(c))
d_xc = matrix(kernlab::kernelMatrix(K, as.matrix(data), c), ncol = nrow(c))
# kernel product between each new datapoint and itself: rows are identical
d_xx = matrix(
rep(diag(kernlab::kernelMatrix(K, as.matrix(task$data()))), each = ncol(d_xc)),
rep(diag(kernlab::kernelMatrix(K, as.matrix(data))), each = ncol(d_xc)),
ncol = ncol(d_xc), byrow = TRUE
)
# kernel product between each center and itself: columns are identical
Expand Down
10 changes: 6 additions & 4 deletions R/LearnerClustMiniBatchKMeans.R
Original file line number Diff line number Diff line change
Expand Up @@ -66,18 +66,20 @@ LearnerClustMiniBatchKMeans = R6Class("LearnerClustMiniBatchKMeans",
stopf("`CENTROIDS` must have same number of rows as `clusters`")
}

m = invoke(ClusterR::MiniBatchKmeans, data = task$data(), .args = pv)
data = task$data()
m = invoke(ClusterR::MiniBatchKmeans, data = data, .args = pv)
if (self$save_assignments) {
self$assignments = as.integer(invoke(predict, m, newdata = task$data()))
self$assignments = as.integer(invoke(predict, m, newdata = data))
}
m
},

.predict = function(task) {
partition = as.integer(invoke(predict, self$model, newdata = task$data()))
data = task$data()
partition = as.integer(invoke(predict, self$model, newdata = data))
prob = NULL
if (self$predict_type == "prob") {
prob = invoke(predict, self$model, newdata = task$data(), fuzzy = TRUE)
prob = invoke(predict, self$model, newdata = data, fuzzy = TRUE)
colnames(prob) = seq_len(ncol(prob))
}
PredictionClust$new(task = task, partition = partition, prob = prob)
Expand Down
5 changes: 3 additions & 2 deletions R/LearnerClustOPTICS.R
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,9 @@ LearnerClustOPTICS = R6Class("LearnerClustOPTICS",
private = list(
.train = function(task) {
pv = self$param_set$get_values(tags = "train")
m = invoke(dbscan::optics, x = task$data(), .args = remove_named(pv, "eps_cl"))
m = insert_named(m, list(data = task$data()))
data = task$data()
m = invoke(dbscan::optics, x = data, .args = remove_named(pv, "eps_cl"))
m = insert_named(m, list(data = data))
m = invoke(dbscan::extractDBSCAN, object = m, eps_cl = pv$eps_cl)

if (self$save_assignments) {
Expand Down
2 changes: 1 addition & 1 deletion R/zzz.R
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ register_mlr3 = function() {
x$task_col_roles$clust = x$task_col_roles$regr
x$task_properties$clust = x$task_properties$regr
x$learner_properties$clust = c(
"missings", "partitional", "hierarchical", "exclusive", "overlapping", "fuzzy", "complete", "partial", "density"
"weights", "missings", "partitional", "hierarchical", "exclusive", "overlapping", "fuzzy", "complete", "partial", "density"
)

# measure
Expand Down
3 changes: 2 additions & 1 deletion man/PredictionClust.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/mlr_learners_clust.cmeans.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/mlr_learners_clust.dbscan.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file removed tests/testthat/Rplots.pdf
Binary file not shown.