{"action":"created","comment":{"url":"https://api.github.com/repos/mlr-org/mlr/pulls/comments/60199380","id":60199380,"diff_hunk":"@@ -0,0 +1,88 @@\n+#' @title Use dependent binary relevance method (DBR) to create a multilabel learner.\n+#'\n+#' @description\n+#' Every learner which is implemented in mlr and which supports binary\n+#' classification can be converted to a wrapped DBR multilabel learner.\n+#' The multilabel classification problem is converted into simple binary classifications\n+#' for each label/target on which the binary learner is applied. \n+#' For each target, actual information of all binary labels (except the target variable) is used as additional features.\n+#' During prediction these labels need are obtained by the binary relevance method using the same binary learner.\n+#'\n+#' Models can easily be accessed via \\code{\\link{getLearnerModel}}.\n+#'\n+#' @template arg_learner\n+#' @template ret_learner\n+#' @references\n+#' Montanes, E. et al. (2013)\n+#' \\emph{Dependent binary relevance models for multi-label classification}\n+#' Artificial Intelligence Center, University of Oviedo at Gijon, Spain.\n+#' @family wrapper\n+#' @family multilabel\n+#' @export\n+#' @examples\n+#' d = getTaskData(yeast.task)\n+#' # drop some labels so example runs faster\n+#' d = d[, c(1:3, 15:117)]\n+#' task = makeMultilabelTask(data = d, target = c(\"label1\", \"label2\", \"label3\"))\n+#' lrn = makeMultilabelDBRWrapper(\"classif.rpart\")\n+#' lrn = setPredictType(lrn, \"prob\")\n+#' # train, predict and evaluate\n+#' mod = train(lrn, task)\n+#' pred = predict(mod, task)\n+#' p = performance(pred)\n+#' performance(pred, measure = hamloss)\n+#' getMultilabelBinaryPerformances(pred, measures = list(mmce, auc))\n+#' # above works also with predictions from resample!\n+makeMultilabelDBRWrapper = function(learner) {\n+  learner = checkLearner(learner, type = \"classif\")\n+  hasLearnerProperties(learner, \"twoclass\")\n+  id = paste(\"multilabel\", learner$id, sep = \".\")\n+  packs = learner$package\n+  x = makeHomogeneousEnsemble(id, learner$type, learner, packs,\n+    learner.subclass = \"MultilabelDBRWrapper\",\n+    model.subclass = \"MultilabelDBRModel\")\n+  x$type = \"multilabel\"\n+  return(x)\n+}\n+\n+#' @export\n+trainLearner.MultilabelDBRWrapper = function(.learner, .task, .subset, .weights = NULL, ...) {\n+  targets = getTaskTargetNames(.task)\n+  .task = subsetTask(.task, subset = .subset)\n+  data = getTaskData(.task)\n+  # train level 1 learners (binary relevance)\n+  modelsLvl1 = getLearnerModel(train(makeMultilabelBinaryRelevanceWrapper(.learner$next.learner), .task, weights = .weights))\n+  # train meta level learners\n+  modelsMeta = namedList(targets)\n+  for (tn in targets) {\n+    data2Meta = dropNamed(data, setdiff(targets, tn))\n+    trueLabelData = sapply(data.frame(data[, setdiff(targets, tn)]), as.numeric)\n+    colnames(trueLabelData) = setdiff(targets, tn)\n+    data2Meta = data.frame(data2Meta, trueLabelData)\n+    ctask = makeClassifTask(id = tn, data = data2Meta, target = tn)\n+    modelsMeta[[tn]] = train(.learner$next.learner, ctask, weights = .weights)\n+  }\n+  makeHomChainModel(.learner, c(modelsLvl1, modelsMeta))\n+}\n+\n+#' @export\n+predictLearner.MultilabelDBRWrapper = function(.learner, .model, .newdata, ...) {\n+  models = getLearnerModel(.model, more.unwrap = FALSE)\n+  # Level 1 prediction (binary relevance)\n+  modelsLvl1 = models[1:length(.model$task.desc$target)]\n+  f = if (.learner$predict.type == \"response\") {\n+    function(m) as.logical(predict(m, newdata = .newdata, ...)$data$response)\n+  } else {\n+    function(m) getPredictionProbabilities(predict(m, newdata = .newdata, ...), cl = \"TRUE\")\n+  }\n+  predLvl1 = sapply(data.frame(asMatrixCols(lapply(modelsLvl1, f))), as.numeric)\n+  # Meta level prediction\n+  modelsMeta = models[(length(.model$task.desc$target) + 1):(2 * length(.model$task.desc$target))]\n+  newdataMeta = data.frame(.newdata, predLvl1)\n+  f = if (.learner$predict.type == \"response\") {\n+    function(m, tn) as.logical(getPredictionResponse(predict(m, newdata = newdataMeta[,which(colnames(newdataMeta) != tn)], ...)))","path":"R/MultilabelDBRWrapper.R","position":83,"original_position":83,"commit_id":"becb59347ea3e4b0860b61cbd50d08b62dd540e6","original_commit_id":"becb59347ea3e4b0860b61cbd50d08b62dd540e6","user":{"login":"PhilippPro","id":11573242,"avatar_url":"https://avatars.githubusercontent.com/u/11573242?v=3","gravatar_id":"","url":"https://api.github.com/users/PhilippPro","html_url":"https://github.com/PhilippPro","followers_url":"https://api.github.com/users/PhilippPro/followers","following_url":"https://api.github.com/users/PhilippPro/following{/other_user}","gists_url":"https://api.github.com/users/PhilippPro/gists{/gist_id}","starred_url":"https://api.github.com/users/PhilippPro/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/PhilippPro/subscriptions","organizations_url":"https://api.github.com/users/PhilippPro/orgs","repos_url":"https://api.github.com/users/PhilippPro/repos","events_url":"https://api.github.com/users/PhilippPro/events{/privacy}","received_events_url":"https://api.github.com/users/PhilippPro/received_events","type":"User","site_admin":false},"body":"Leerzeichen bei  `newdataMeata[, which` ;)","created_at":"2016-04-19T09:29:57Z","updated_at":"2016-04-19T09:29:57Z","html_url":"https://github.com/mlr-org/mlr/pull/833#discussion_r60199380","pull_request_url":"https://api.github.com/repos/mlr-org/mlr/pulls/833","_links":{"self":{"href":"https://api.github.com/repos/mlr-org/mlr/pulls/comments/60199380"},"html":{"href":"https://github.com/mlr-org/mlr/pull/833#discussion_r60199380"},"pull_request":{"href":"https://api.github.com/repos/mlr-org/mlr/pulls/833"}}},"pull_request":{"url":"https://api.github.com/repos/mlr-org/mlr/pulls/833","id":66355084,"html_url":"https://github.com/mlr-org/mlr/pull/833","diff_url":"https://github.com/mlr-org/mlr/pull/833.diff","patch_url":"https://github.com/mlr-org/mlr/pull/833.patch","issue_url":"https://api.github.com/repos/mlr-org/mlr/issues/833","number":833,"state":"open","locked":false,"title":"Multilabel wrappers all","user":{"login":"QuayAu","id":15329438,"avatar_url":"https://avatars.githubusercontent.com/u/15329438?v=3","gravatar_id":"","url":"https://api.github.com/users/QuayAu","html_url":"https://github.com/QuayAu","followers_url":"https://api.github.com/users/QuayAu/followers","following_url":"https://api.github.com/users/QuayAu/following{/other_user}","gists_url":"https://api.github.com/users/QuayAu/gists{/gist_id}","starred_url":"https://api.github.com/users/QuayAu/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/QuayAu/subscriptions","organizations_url":"https://api.github.com/users/QuayAu/orgs","repos_url":"https://api.github.com/users/QuayAu/repos","events_url":"https://api.github.com/users/QuayAu/events{/privacy}","received_events_url":"https://api.github.com/users/QuayAu/received_events","type":"User","site_admin":false},"body":"I made one PR for all wrappers (if desired, i can update the 4 PRs).\r\nI incorporated the latest comments into the files. The following changes were made:\r\n\r\n- used \"hasLearnerProperties\" for check if \"twoclass\" is available\r\n- there is no option for a different level 1 learner any more (for DBR and STA)\r\n- used \"makeMultilabelBinaryRelevanceWrapper\" for level 1 learners (for DBR and STA)\r\n- small corrections (deleted unnecessary \"newdata = .newdata\" or \"paste\", added \"...\" in predict,  small style changes)\r\n- unit tests were pretty much copy and paste. I wrote a function, for a clearer and shorter code.","created_at":"2016-04-13T16:53:06Z","updated_at":"2016-04-19T09:29:57Z","closed_at":null,"merged_at":null,"merge_commit_sha":"0e438a85ce757ae745cde00e0110885b86c95c83","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/mlr-org/mlr/pulls/833/commits","review_comments_url":"https://api.github.com/repos/mlr-org/mlr/pulls/833/comments","review_comment_url":"https://api.github.com/repos/mlr-org/mlr/pulls/comments{/number}","comments_url":"https://api.github.com/repos/mlr-org/mlr/issues/833/comments","statuses_url":"https://api.github.com/repos/mlr-org/mlr/statuses/becb59347ea3e4b0860b61cbd50d08b62dd540e6","head":{"label":"QuayAu:multilabel_wrappers_all","ref":"multilabel_wrappers_all","sha":"becb59347ea3e4b0860b61cbd50d08b62dd540e6","user":{"login":"QuayAu","id":15329438,"avatar_url":"https://avatars.githubusercontent.com/u/15329438?v=3","gravatar_id":"","url":"https://api.github.com/users/QuayAu","html_url":"https://github.com/QuayAu","followers_url":"https://api.github.com/users/QuayAu/followers","following_url":"https://api.github.com/users/QuayAu/following{/other_user}","gists_url":"https://api.github.com/users/QuayAu/gists{/gist_id}","starred_url":"https://api.github.com/users/QuayAu/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/QuayAu/subscriptions","organizations_url":"https://api.github.com/users/QuayAu/orgs","repos_url":"https://api.github.com/users/QuayAu/repos","events_url":"https://api.github.com/users/QuayAu/events{/privacy}","received_events_url":"https://api.github.com/users/QuayAu/received_events","type":"User","site_admin":false},"repo":{"id":49447687,"name":"mlr","full_name":"QuayAu/mlr","owner":{"login":"QuayAu","id":15329438,"avatar_url":"https://avatars.githubusercontent.com/u/15329438?v=3","gravatar_id":"","url":"https://api.github.com/users/QuayAu","html_url":"https://github.com/QuayAu","followers_url":"https://api.github.com/users/QuayAu/followers","following_url":"https://api.github.com/users/QuayAu/following{/other_user}","gists_url":"https://api.github.com/users/QuayAu/gists{/gist_id}","starred_url":"https://api.github.com/users/QuayAu/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/QuayAu/subscriptions","organizations_url":"https://api.github.com/users/QuayAu/orgs","repos_url":"https://api.github.com/users/QuayAu/repos","events_url":"https://api.github.com/users/QuayAu/events{/privacy}","received_events_url":"https://api.github.com/users/QuayAu/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/QuayAu/mlr","description":"mlr: Machine Learning in R ","fork":true,"url":"https://api.github.com/repos/QuayAu/mlr","forks_url":"https://api.github.com/repos/QuayAu/mlr/forks","keys_url":"https://api.github.com/repos/QuayAu/mlr/keys{/key_id}","collaborators_url":"https://api.github.com/repos/QuayAu/mlr/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/QuayAu/mlr/teams","hooks_url":"https://api.github.com/repos/QuayAu/mlr/hooks","issue_events_url":"https://api.github.com/repos/QuayAu/mlr/issues/events{/number}","events_url":"https://api.github.com/repos/QuayAu/mlr/events","assignees_url":"https://api.github.com/repos/QuayAu/mlr/assignees{/user}","branches_url":"https://api.github.com/repos/QuayAu/mlr/branches{/branch}","tags_url":"https://api.github.com/repos/QuayAu/mlr/tags","blobs_url":"https://api.github.com/repos/QuayAu/mlr/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/QuayAu/mlr/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/QuayAu/mlr/git/refs{/sha}","trees_url":"https://api.github.com/repos/QuayAu/mlr/git/trees{/sha}","statuses_url":"https://api.github.com/repos/QuayAu/mlr/statuses/{sha}","languages_url":"https://api.github.com/repos/QuayAu/mlr/languages","stargazers_url":"https://api.github.com/repos/QuayAu/mlr/stargazers","contributors_url":"https://api.github.com/repos/QuayAu/mlr/contributors","subscribers_url":"https://api.github.com/repos/QuayAu/mlr/subscribers","subscription_url":"https://api.github.com/repos/QuayAu/mlr/subscription","commits_url":"https://api.github.com/repos/QuayAu/mlr/commits{/sha}","git_commits_url":"https://api.github.com/repos/QuayAu/mlr/git/commits{/sha}","comments_url":"https://api.github.com/repos/QuayAu/mlr/comments{/number}","issue_comment_url":"https://api.github.com/repos/QuayAu/mlr/issues/comments{/number}","contents_url":"https://api.github.com/repos/QuayAu/mlr/contents/{+path}","compare_url":"https://api.github.com/repos/QuayAu/mlr/compare/{base}...{head}","merges_url":"https://api.github.com/repos/QuayAu/mlr/merges","archive_url":"https://api.github.com/repos/QuayAu/mlr/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/QuayAu/mlr/downloads","issues_url":"https://api.github.com/repos/QuayAu/mlr/issues{/number}","pulls_url":"https://api.github.com/repos/QuayAu/mlr/pulls{/number}","milestones_url":"https://api.github.com/repos/QuayAu/mlr/milestones{/number}","notifications_url":"https://api.github.com/repos/QuayAu/mlr/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/QuayAu/mlr/labels{/name}","releases_url":"https://api.github.com/repos/QuayAu/mlr/releases{/id}","deployments_url":"https://api.github.com/repos/QuayAu/mlr/deployments","created_at":"2016-01-11T19:07:29Z","updated_at":"2016-01-11T19:07:31Z","pushed_at":"2016-04-13T16:35:31Z","git_url":"git://github.com/QuayAu/mlr.git","ssh_url":"git@github.com:QuayAu/mlr.git","clone_url":"https://github.com/QuayAu/mlr.git","svn_url":"https://github.com/QuayAu/mlr","homepage":null,"size":85816,"stargazers_count":0,"watchers_count":0,"language":"R","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"mlr-org:master","ref":"master","sha":"19c3bf9b6133ca408dcafce0839a3e634a55085f","user":{"login":"mlr-org","id":12941794,"avatar_url":"https://avatars.githubusercontent.com/u/12941794?v=3","gravatar_id":"","url":"https://api.github.com/users/mlr-org","html_url":"https://github.com/mlr-org","followers_url":"https://api.github.com/users/mlr-org/followers","following_url":"https://api.github.com/users/mlr-org/following{/other_user}","gists_url":"https://api.github.com/users/mlr-org/gists{/gist_id}","starred_url":"https://api.github.com/users/mlr-org/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mlr-org/subscriptions","organizations_url":"https://api.github.com/users/mlr-org/orgs","repos_url":"https://api.github.com/users/mlr-org/repos","events_url":"https://api.github.com/users/mlr-org/events{/privacy}","received_events_url":"https://api.github.com/users/mlr-org/received_events","type":"Organization","site_admin":false},"repo":{"id":12465340,"name":"mlr","full_name":"mlr-org/mlr","owner":{"login":"mlr-org","id":12941794,"avatar_url":"https://avatars.githubusercontent.com/u/12941794?v=3","gravatar_id":"","url":"https://api.github.com/users/mlr-org","html_url":"https://github.com/mlr-org","followers_url":"https://api.github.com/users/mlr-org/followers","following_url":"https://api.github.com/users/mlr-org/following{/other_user}","gists_url":"https://api.github.com/users/mlr-org/gists{/gist_id}","starred_url":"https://api.github.com/users/mlr-org/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mlr-org/subscriptions","organizations_url":"https://api.github.com/users/mlr-org/orgs","repos_url":"https://api.github.com/users/mlr-org/repos","events_url":"https://api.github.com/users/mlr-org/events{/privacy}","received_events_url":"https://api.github.com/users/mlr-org/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/mlr-org/mlr","description":"mlr: Machine Learning in R ","fork":false,"url":"https://api.github.com/repos/mlr-org/mlr","forks_url":"https://api.github.com/repos/mlr-org/mlr/forks","keys_url":"https://api.github.com/repos/mlr-org/mlr/keys{/key_id}","collaborators_url":"https://api.github.com/repos/mlr-org/mlr/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/mlr-org/mlr/teams","hooks_url":"https://api.github.com/repos/mlr-org/mlr/hooks","issue_events_url":"https://api.github.com/repos/mlr-org/mlr/issues/events{/number}","events_url":"https://api.github.com/repos/mlr-org/mlr/events","assignees_url":"https://api.github.com/repos/mlr-org/mlr/assignees{/user}","branches_url":"https://api.github.com/repos/mlr-org/mlr/branches{/branch}","tags_url":"https://api.github.com/repos/mlr-org/mlr/tags","blobs_url":"https://api.github.com/repos/mlr-org/mlr/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/mlr-org/mlr/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/mlr-org/mlr/git/refs{/sha}","trees_url":"https://api.github.com/repos/mlr-org/mlr/git/trees{/sha}","statuses_url":"https://api.github.com/repos/mlr-org/mlr/statuses/{sha}","languages_url":"https://api.github.com/repos/mlr-org/mlr/languages","stargazers_url":"https://api.github.com/repos/mlr-org/mlr/stargazers","contributors_url":"https://api.github.com/repos/mlr-org/mlr/contributors","subscribers_url":"https://api.github.com/repos/mlr-org/mlr/subscribers","subscription_url":"https://api.github.com/repos/mlr-org/mlr/subscription","commits_url":"https://api.github.com/repos/mlr-org/mlr/commits{/sha}","git_commits_url":"https://api.github.com/repos/mlr-org/mlr/git/commits{/sha}","comments_url":"https://api.github.com/repos/mlr-org/mlr/comments{/number}","issue_comment_url":"https://api.github.com/repos/mlr-org/mlr/issues/comments{/number}","contents_url":"https://api.github.com/repos/mlr-org/mlr/contents/{+path}","compare_url":"https://api.github.com/repos/mlr-org/mlr/compare/{base}...{head}","merges_url":"https://api.github.com/repos/mlr-org/mlr/merges","archive_url":"https://api.github.com/repos/mlr-org/mlr/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/mlr-org/mlr/downloads","issues_url":"https://api.github.com/repos/mlr-org/mlr/issues{/number}","pulls_url":"https://api.github.com/repos/mlr-org/mlr/pulls{/number}","milestones_url":"https://api.github.com/repos/mlr-org/mlr/milestones{/number}","notifications_url":"https://api.github.com/repos/mlr-org/mlr/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/mlr-org/mlr/labels{/name}","releases_url":"https://api.github.com/repos/mlr-org/mlr/releases{/id}","deployments_url":"https://api.github.com/repos/mlr-org/mlr/deployments","created_at":"2013-08-29T16:26:13Z","updated_at":"2016-04-16T08:31:56Z","pushed_at":"2016-04-18T16:49:58Z","git_url":"git://github.com/mlr-org/mlr.git","ssh_url":"git@github.com:mlr-org/mlr.git","clone_url":"https://github.com/mlr-org/mlr.git","svn_url":"https://github.com/mlr-org/mlr","homepage":null,"size":88669,"stargazers_count":350,"watchers_count":350,"language":"R","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":true,"forks_count":115,"mirror_url":null,"open_issues_count":152,"forks":115,"open_issues":152,"watchers":350,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/mlr-org/mlr/pulls/833"},"html":{"href":"https://github.com/mlr-org/mlr/pull/833"},"issue":{"href":"https://api.github.com/repos/mlr-org/mlr/issues/833"},"comments":{"href":"https://api.github.com/repos/mlr-org/mlr/issues/833/comments"},"review_comments":{"href":"https://api.github.com/repos/mlr-org/mlr/pulls/833/comments"},"review_comment":{"href":"https://api.github.com/repos/mlr-org/mlr/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/mlr-org/mlr/pulls/833/commits"},"statuses":{"href":"https://api.github.com/repos/mlr-org/mlr/statuses/becb59347ea3e4b0860b61cbd50d08b62dd540e6"}}}}